18
18
*/
19
19
20
20
/**
21
+ * Plugin extension subscribers, adding support for jQuery UI
22
+ * and the Validation Plugin.
21
23
* Initializes element type subscribers and subscriber functions
22
24
* provided by the jQuery UI framework adding jQuery UI support.
25
+ * Only subscribes if the needed elements (like tabs, slider, progressbar etc.)
26
+ * are actually available (in case you are using a custom jQuery UI build).
23
27
*
24
28
* @author David Luecke <daff@neyeon.de>
25
29
*/
26
30
( function ( $ )
27
31
{
28
- $ . dform . subscribe (
32
+ if ( $ . isFunction ( $ . fn . validate ) ) // Check if the validation plugin is available
29
33
{
34
+ $ . dform . subscribe (
35
+ {
36
+ /**
37
+ * Add a preprocessing subscriber that calls .validate() on the form,
38
+ * so that we can add rules to the input elements.
39
+ *
40
+ * @param options mixed All options that have been used for
41
+ * creating the current element.
42
+ * @param type string The type of the <strong>this</strong> element
43
+ */
44
+ "[pre]" : function ( options , type )
45
+ {
46
+ if ( type == "form" )
47
+ {
48
+ var defaults = { } ;
49
+ if ( $ ( this ) . hasClass ( "ui-widget" ) )
50
+ defaults = {
51
+ highlight : function ( input )
52
+ {
53
+ $ ( input ) . addClass ( "ui-state-highlight" ) ;
54
+ } ,
55
+ unhighlight : function ( input )
56
+ {
57
+ $ ( input ) . removeClass ( "ui-state-highlight" ) ;
58
+ }
59
+ } ;
60
+ $ ( this ) . validate ( defaults ) ;
61
+ }
62
+ } ,
63
+ /**
64
+ * Adds support for the jQuery validation rulesets.
65
+ *
66
+ * @param options object Options as specified in the rules parameter
67
+ * @param type string The type of the <strong>this</strong> element
68
+ */
69
+ "validate" : function ( options , type )
70
+ {
71
+ $ ( this ) . rules ( "add" , options ) ;
72
+ }
73
+ } ) ;
74
+ }
75
+
76
+ // TODO generalize initialization of ui elements (currently duplicated code)
77
+
78
+ if ( $ . isFunction ( $ . fn . progressbar ) )
79
+ {
80
+ $ . dform . subscribe ( "[type=progressbar]" ,
30
81
/**
31
82
* Returns a progressbar jQuery UI progressbar.
32
83
* @param options object All parameters for this type
33
84
*/
34
- "[type=progressbar]" : function ( options )
85
+ function ( options )
35
86
{
36
- // TODO html attributes
37
- return $ ( "<div>" ) . progressbar ( options ) ;
38
- } ,
87
+ var keys = $ . keyset ( $ . ui . progressbar . prototype . options ) ;
88
+ var ops = $ . withKeys ( options , keys ) ;
89
+ var attributes = $ . withoutKeys ( options , keys ) ;
90
+ return $ ( "<div>" ) . attr ( attributes ) . progressbar ( ops ) ;
91
+ } ) ;
92
+ }
93
+
94
+ if ( $ . isFunction ( $ . fn . slider ) )
95
+ {
96
+ $ . dform . subscribe ( "[type=slider]" ,
39
97
/**
40
98
* Returns a slider.
41
99
* @param options object All parameters for this type
42
100
*/
43
- "[type=slider]" : function ( options )
44
- {
45
- var slideroptions = [ "disabled" , "animate" , "max" , "min" , "orientation" ,
46
- "range" , "step" , "value" , "values" ] ;
47
- // TODO div html attributes
48
- return $ ( "<div>" ) . slider ( options ) ;
49
- } ,
50
- /**
51
- * Returns a styled button.
52
- * @param options object All parameters for this type
53
- */
54
- "[type=button]" : function ( options )
101
+ function ( options )
55
102
{
56
- // TODO jquery ui button
57
- } ,
58
- /**
59
- * Creates a container for jQuery UI tabs.
60
- * @param options object All parameters for this type
61
- */
62
- "[type=tabs]" : function ( options )
103
+ // Gets keys of the default options
104
+ var keys = $ . keyset ( $ . ui . slider . prototype . options ) ;
105
+ var ops = $ . withKeys ( options , keys ) ;
106
+ var attributes = $ . withoutKeys ( options , keys ) ;
107
+ return $ ( "<div>" ) . attr ( attributes ) . slider ( ops ) ;
108
+ } ) ;
109
+ }
110
+
111
+ if ( $ . isFunction ( $ . fn . tabs ) )
112
+ {
113
+ $ . dform . subscribe (
63
114
{
64
- // TODO div html attributes
65
- return $ ( "<div>" ) ;
66
- } ,
115
+ /**
116
+ * Creates a container for jQuery UI tabs.
117
+ * @param options object All parameters for this type
118
+ */
119
+ "[type=tabs]" : function ( options )
120
+ {
121
+ var keys = $ . keyset ( $ . ui . tabs . prototype . options ) ;
122
+ var ops = $ . withKeys ( options , keys ) ;
123
+ var attributes = $ . withoutKeys ( options , keys ) ;
124
+ return $ ( "<div>" ) . attr ( attributes ) ;
125
+ } ,
126
+ /**
127
+ * Adds tabs to a tab element.
128
+ *
129
+ * @param options object An object containing a tab with its unique id as a key
130
+ * and the tab options including an "elements" with all
131
+ * subelements as a value.
132
+ * @param type string The type of the <strong>this</strong> element
133
+ */
134
+ "tabs" : function ( options , type )
135
+ {
136
+ if ( type == "tabs" )
137
+ {
138
+ $ ( this ) . append ( "<ul>" ) ;
139
+ var scoper = $ ( this ) ;
140
+ $ . each ( options , function ( tabname , ops )
141
+ {
142
+ var tablink = $ ( "<a>" ) . attr (
143
+ {
144
+ "href" : "#" + tabname
145
+ } ) . html ( ops . title ) ;
146
+ var li = $ ( "<li>" ) . append ( tablink ) ;
147
+ var tabdiv = $ ( "<div>" ) . attr ( "id" , tabname ) ;
148
+ $ ( scoper ) . children ( "ul" ) . first ( ) . append ( li ) ;
149
+ $ ( scoper ) . append ( tabdiv ) ;
150
+
151
+ $ ( tabdiv ) . runAll ( ops ) ;
152
+ } ) ;
153
+ }
154
+ $ ( this ) . tabs ( ) ;
155
+ }
156
+ } ) ;
157
+ }
158
+
159
+ if ( $ . isFunction ( $ . fn . accordion ) )
160
+ {
161
+ $ . dform . subscribe ( "[type=accordion]" ,
67
162
/**
68
163
* Creates a container for the jQuery UI accordion.
69
164
* @param options object All parameters for this type
70
165
*/
71
- "[type=accordion]" : function ( options )
166
+ function ( options )
72
167
{
73
- // TODO div html attributes
74
- return $ ( "<div>" ) ;
75
- }
76
- } ) ;
168
+ var keys = $ . keyset ( $ . ui . accordion . prototype . options ) ;
169
+ var ops = $ . withKeys ( options , keys ) ;
170
+ var attributes = $ . withoutKeys ( options , keys ) ;
171
+ return $ ( "<div>" ) . attr ( attributes ) ;
172
+ } ) ;
173
+ }
77
174
78
- $ . dform . subscribe (
175
+ if ( $ . isFunction ( $ . fn . dialog ) )
79
176
{
177
+ $ . dform . subscribe ( "dialog" ,
80
178
/**
81
- * Type post processing subscriber that adds jQuery UI styling classes to
82
- * "text", "textarea", "password" and "fieldset" elements as well
83
- * as calling .button() on submit buttons.
84
- *
85
- * @param options mixed All options that have been used for
86
- * creating the current element.
87
- * @param type string The type of the <strong>this</strong> element
88
- */
89
- "type" : function ( options , type )
90
- {
91
- if ( $ ( this ) . parents ( "form" ) . hasClass ( "ui-widget" ) )
92
- {
93
- if ( type == "button" || type == "submit" )
94
- $ ( this ) . button ( ) ;
95
- if ( $ . inArray ( type , [ "text" , "textarea" , "password" ,
96
- "fieldset" ] ) != - 1 )
97
- $ ( this ) . addClass ( "ui-widget-content ui-corner-all" ) ;
98
- }
99
- } ,
100
- /**
101
- * Creates a dialog on form elements.
179
+ * Creates a dialog on form or fieldset elements.
102
180
*
103
181
* @param options object Options for creating the jQuery UI dialog
104
182
* @param type string The type of the <strong>this</strong> element
105
183
*/
106
- "dialog" : function ( options , type )
184
+ function ( options , type )
107
185
{
108
186
if ( type == "form" || type == "fieldset" )
109
187
$ ( this ) . dialog ( options ) ;
110
- } ,
188
+ } ) ;
189
+ }
190
+
191
+ if ( $ . isFunction ( $ . fn . resizable ) )
192
+ {
193
+ $ . dform . subscribe ( "resizable" ,
111
194
/**
112
195
* Makes the current element resizeable.
113
196
*
114
197
* @param options object Options for creating a jQuery UI resizable
115
198
* @param type string The type of the <strong>this</strong> element
116
199
*/
117
- "resizable" : function ( options , type )
200
+ function ( options , type )
118
201
{
119
202
$ ( this ) . resizable ( options ) ;
120
- $ ( this ) . resizable ( "enable" ) ;
121
- } ,
203
+ } ) ;
204
+ }
205
+
206
+ if ( $ . isFunction ( $ . fn . datepicker ) )
207
+ {
208
+ $ . dform . subscribe ( "datepicker" ,
122
209
/**
123
210
* Turns a text element into a datepicker
124
211
*
125
212
* @param options object Options for creating a jQuery UI datepicker
126
213
* @param type string The type of the <strong>this</strong> element
127
214
*/
128
- "datepicker" : function ( options , type )
215
+ function ( options , type )
129
216
{
130
217
if ( type == "text" )
131
218
$ ( this ) . datepicker ( options ) ;
132
- } ,
219
+ } ) ;
220
+ }
221
+
222
+ if ( $ . isFunction ( $ . fn . autocomplete ) )
223
+ {
224
+ $ . dform . subscribe ( "autocomplete" ,
133
225
/**
134
226
* Adds an autocomplete feature to a text element.
135
227
*
136
228
* @param options object Options for creating a jQuery UI autocomplete
137
229
* @param type string The type of the <strong>this</strong> element
138
230
*/
139
- "autocomplete" : function ( options , type )
231
+ function ( options , type )
140
232
{
141
233
if ( type == "text" )
142
- {
143
234
$ ( this ) . autocomplete ( options ) ;
144
- }
145
- } ,
146
- /**
147
- * Adds an autocomplete feature to a text element.
148
- *
149
- * @param options object An object containing a tabs unique id as a key
150
- * and the tab options including an "elements" containing the tabs
151
- * subelements as a value.
152
- * @param type string The type of the <strong>this</strong> element
153
- */
154
- "tabs" : function ( options , type )
235
+ } ) ;
236
+ }
237
+
238
+ $ . dform . subscribe ( "[post]" ,
239
+ /**
240
+ * Post processing subscriber that adds jQuery UI styling classes to
241
+ * "text", "textarea", "password" and "fieldset" elements as well
242
+ * as calling .button() on submit buttons.
243
+ *
244
+ * @param options mixed All options that have been used for
245
+ * creating the current element.
246
+ * @param type string The type of the <strong>this</strong> element
247
+ */
248
+ function ( options , type )
249
+ {
250
+ // TODO style validation plugin controls with jQuery UI classes
251
+ if ( $ ( this ) . parents ( "form" ) . hasClass ( "ui-widget" ) )
155
252
{
156
- if ( type == "tabs" )
157
- {
158
- $ ( this ) . append ( "<ul>" ) ;
159
- var scoper = $ ( this ) ;
160
- $ . each ( options , function ( tabname , ops )
161
- {
162
- ops . type == type ;
163
- var tablink = $ ( "<a>" ) . attr (
164
- {
165
- "href" : "#" + tabname
166
- } ) . html ( ops . title ) ;
167
- var li = $ ( "<li>" ) . append ( tablink ) ;
168
- var tabdiv = $ ( "<div>" ) . attr ( "id" , tabname ) ;
169
- $ ( scoper ) . children ( "ul" ) . first ( ) . append ( li ) ;
170
- $ ( scoper ) . append ( tabdiv ) ;
171
-
172
- $ ( tabdiv ) . runAll ( ops ) ;
173
- } ) ;
174
- }
175
- $ ( this ) . tabs ( ) ;
253
+ if ( ( type == "button" || type == "submit" ) && $ . isFunction ( $ . fn . button ) )
254
+ $ ( this ) . button ( ) ;
255
+ if ( $ . inArray ( type , [ "text" , "textarea" , "password" ,
256
+ "fieldset" ] ) != - 1 )
257
+ $ ( this ) . addClass ( "ui-widget-content ui-corner-all" ) ;
176
258
}
177
259
} ) ;
178
260
} ) ( jQuery ) ;
0 commit comments