-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoption-list.js
More file actions
131 lines (113 loc) · 3.47 KB
/
option-list.js
File metadata and controls
131 lines (113 loc) · 3.47 KB
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
128
129
130
131
// option list widget
//
// Given a widget created using the widget factory, this widget will display a list of form elements that can be used to
// set the widget's options at runtime
//
// options:
//
// widget: a widget instance
( function( $, undefined ) {
$.widget( "todons.optionlist", $.mobile.widget, {
options: {
widget: null
},
_setOption: function( key, value ) {
switch ( key ) {
case "widget":
return this._setWidget( value );
default:
return $.Widget.prototype._setOption.call( this, key, value );
}
},
_setWidget: function( value ) {
if ( value !== null ) {
var self = this;
this.options.widget = value;
this.element.append( $( "<h2>" + value.namespace + "." + value.widgetName + "</h2>" ) );
$.each ( $[ value.namespace ][ value.widgetName ].prototype.options, function( key ) {
self._createOption( value.namespace, value.widgetName, value.element, key );
});
this.element.trigger( "create" );
}
},
destroy: function() {
this.element.empty();
$.Widget.prototype.destroy.call( this );
},
_createOption: function( ns, widgetType, theWidget, key ) {
var optionsList = this.element, entry,
self = this,
id = "todons-optionlist-option-" + ( $.todons.optionlist.optionId++ ) + "-" + key;
function makeSetter() {
switch( typeof $[ ns ][ widgetType ].prototype.options[ key ] ) {
case "boolean":
return {
html: $( "<input/>", {
type: "checkbox",
checked: theWidget[ widgetType ]( "option", key ),
id: id
}),
getValue: function( elem ) { return elem.is( ":checked" ); }
};
case "integer":
return {
html: $( "<input/>", {
type: "number",
value: theWidget[ widgetType ]( "option", key ),
id: id
}),
getValue: function( elem ) { return elem.val(); }
};
default:
return {
html: $( "<input/>", {
type: "text",
value: theWidget[ widgetType ]( "option", key ),
id: id
}),
getValue: function( elem ) { return elem.val(); }
};
}
}
entry = makeSetter();
$( "<div/>" )
.append( $( "<label/>", { "for": id } ).text( key ) )
.append( entry.html )
.appendTo( optionsList )
.fieldcontain();
entry.html.bind( "change", function( e ) {
theWidget[ widgetType ]( "option", key, entry.getValue( entry.html ) );
self.element.triggerHandler( "optionChanged" );
});
}
});
// "duck typing" a widget - thanks, gnarf! :)
// Basically, check if an element has any object data-items which which contain a function under the key "widget"
// and a string under the key "widgetName"
//
// Returns either false or the list of widgets associated with the element
$.todons.optionlist.widgetsFromElement = function( elem ) {
var ret = [];
$.each( $.data( elem ), function( key, value ) {
if ( value && typeof value === "object" ) {
var hasWidgetFunction = false, hasWidgetName = false;
$.each( value, function( innerKey, innerValue ) {
if ( innerKey === "widgetName" ) {
hasWidgetName = true;
}
else
if ( innerKey === "widget" && typeof innerValue === "function" ) {
hasWidgetFunction = true;
}
if ( hasWidgetFunction && hasWidgetName ) {
ret.push( value );
return false;
}
});
}
});
return (ret.length === 0 ? false : ret);
};
// monotonically increasing counter for option labels
$.todons.optionlist.optionId = 0;
})( jQuery );