1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Widget - Default functionality</title>
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
<script src="../../jquery-1.5.1.js"></script>
<script src="../../ui/jquery.ui.core.js"></script>
<script src="../../ui/jquery.ui.widget.js"></script>
<script src="../../ui/jquery.ui.position.js"></script>
<link rel="stylesheet" href="../demos.css">
<style>
/* this could go into custom.inlineedit.css */
/* make it display:block to replace the block element that's being edited */
.inlineedit-input { display: block; }
/* this would need a few more styles to make it look decent */
</style>
<script>
$(function() {
// this defines a new widget, in the "custom" namespace
$.widget( "custom.inlineedit", {
// default options
options: {
submitOnBlur: true
},
// this is the constructor
_create: function() {
// basic event binding to this.element
this._bind({
// string as value is mapped to instance method
click: "start"
});
// creating a new element to show later
this.input = $( "<input>" ).addClass("inlineedit-input").hide().insertAfter( this.element );
// with events on input, here functions that to do event-specific checks
this._bind( this.input, {
blur: function( event ) {
// ignore blur event if already hidden
if (!this.input.is(":visible")) {
return;
}
if ( this.options.submitOnBlur ) {
this.submit( event )
} else {
this.cancel( event );
}
},
keyup: function( event ) {
// using $.ui.keyCode to map keyboard input to the right action
if ( event.keyCode === $.ui.keyCode.ENTER || event.keyCode === $.ui.keyCode.NUMPAD_ENTER ) {
this.submit( event );
} else if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
this.cancel( event );
}
}
});
},
// a public method
start: function( event ) {
this.element.hide();
this.input.val( this.element.text() ).show().focus();
// trigger a custom event when something changes
this._trigger("start", event );
},
// another custom method
_hide: function( event ) {
this.input.hide();
this.element.show();
},
submit: function( event ) {
var newValue = this.input.val(),
ui = {
value: newValue
};
// trigger an event, cancel the default action when event handler returns false
if ( this._trigger( "submit", event, ui ) !== false ) {
this.element.text( newValue );
}
this._hide();
},
cancel: function( event ) {
this._hide();
// trigger an event when something changes
this._trigger( "cancel", event );
}
});
// this is how we can use our custom widget, just like any jQuery plugin
$( ".demo h1" ).inlineedit();
$( ".demo p" ).inlineedit({
// configure an option
submitOnBlur: false
});
$( ".demo button" ).click( function() {
// call a public method
$( ".demo :custom-inlineedit" ).inlineedit( "start" );
});
// widget's create a custom selector
// triggered events can be used with regular bind, just prepend name
$( ".demo :custom-inlineedit" ).bind( "inlineeditstart inlineeditsubmit inlineeditcancel" , function( event, ui ) {
$( "<div></div>" ).text( "edit event " + event.type ).appendTo(".demo");
});
});
</script>
</head>
<body>
<div class="demo">
<div>
<h1>This is an editable header</h1>
<p>And an editable paragraph</p>
<button>Start editing</button>
</div>
</div><!-- End demo -->
<div class="demo-description">
<p>This demo shows a simple custom widget built using the widget factory (jquery.ui.widget.js).</p>
<p>The header is set to change the element on blur, the paragraph only changes when you submit with Enter.</p>
<p><a href="http://wiki.jqueryui.com/w/page/12138135/Widget-factory">For more details on the widget factory, visit the jQuery UI planning wiki.</a></p>
</div><!-- End demo-description -->
</body>
</html>
|