Skip to content

Commit 727a80a

Browse files
committed
Widget: Demo.
1 parent 237bbc5 commit 727a80a

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

demos/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
<dd><a href="show/index.html">Show</a></dd>
301301
<dt>Utilities</dt>
302302
<dd><a href="position/index.html">Position</a></dd>
303+
<dd><a href="widget/index.html">Widget</a></dd>
303304
<dt>About jQuery UI</dt>
304305
<dd><a href="http://jqueryui.com/docs/Getting_Started">Getting Started</a></dd>
305306
<dd><a href="http://jqueryui.com/docs/Upgrade_Guide">Upgrade Guide</a></dd>

demos/widget/default.html

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

demos/widget/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jQuery UI Widget Demo</title>
6+
<link rel="stylesheet" href="../demos.css">
7+
</head>
8+
<body>
9+
10+
<div class="demos-nav">
11+
<h4>Examples</h4>
12+
<ul>
13+
<li class="demo-config-on"><a href="default.html">Default functionality</a></li>
14+
</ul>
15+
</div>
16+
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)