Skip to content

Commit 635c9f0

Browse files
author
David Luecke
committed
Reorg
1 parent 769aec2 commit 635c9f0

File tree

1 file changed

+176
-94
lines changed

1 file changed

+176
-94
lines changed

js/jquery.dform.ext.js

Lines changed: 176 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,161 +18,243 @@
1818
*/
1919

2020
/**
21+
* Plugin extension subscribers, adding support for jQuery UI
22+
* and the Validation Plugin.
2123
* Initializes element type subscribers and subscriber functions
2224
* 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).
2327
*
2428
* @author David Luecke <daff@neyeon.de>
2529
*/
2630
(function($)
2731
{
28-
$.dform.subscribe(
32+
if($.isFunction($.fn.validate)) // Check if the validation plugin is available
2933
{
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]",
3081
/**
3182
* Returns a progressbar jQuery UI progressbar.
3283
* @param options object All parameters for this type
3384
*/
34-
"[type=progressbar]" : function(options)
85+
function(options)
3586
{
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]",
3997
/**
4098
* Returns a slider.
4199
* @param options object All parameters for this type
42100
*/
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)
55102
{
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(
63114
{
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]",
67162
/**
68163
* Creates a container for the jQuery UI accordion.
69164
* @param options object All parameters for this type
70165
*/
71-
"[type=accordion]" : function(options)
166+
function(options)
72167
{
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+
}
77174

78-
$.dform.subscribe(
175+
if($.isFunction($.fn.dialog))
79176
{
177+
$.dform.subscribe("dialog",
80178
/**
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.
102180
*
103181
* @param options object Options for creating the jQuery UI dialog
104182
* @param type string The type of the <strong>this</strong> element
105183
*/
106-
"dialog" : function(options, type)
184+
function(options, type)
107185
{
108186
if (type == "form" || type == "fieldset")
109187
$(this).dialog(options);
110-
},
188+
});
189+
}
190+
191+
if($.isFunction($.fn.resizable))
192+
{
193+
$.dform.subscribe("resizable",
111194
/**
112195
* Makes the current element resizeable.
113196
*
114197
* @param options object Options for creating a jQuery UI resizable
115198
* @param type string The type of the <strong>this</strong> element
116199
*/
117-
"resizable" : function(options, type)
200+
function(options, type)
118201
{
119202
$(this).resizable(options);
120-
$(this).resizable("enable");
121-
},
203+
});
204+
}
205+
206+
if($.isFunction($.fn.datepicker))
207+
{
208+
$.dform.subscribe("datepicker",
122209
/**
123210
* Turns a text element into a datepicker
124211
*
125212
* @param options object Options for creating a jQuery UI datepicker
126213
* @param type string The type of the <strong>this</strong> element
127214
*/
128-
"datepicker" : function(options, type)
215+
function(options, type)
129216
{
130217
if (type == "text")
131218
$(this).datepicker(options);
132-
},
219+
});
220+
}
221+
222+
if($.isFunction($.fn.autocomplete))
223+
{
224+
$.dform.subscribe("autocomplete",
133225
/**
134226
* Adds an autocomplete feature to a text element.
135227
*
136228
* @param options object Options for creating a jQuery UI autocomplete
137229
* @param type string The type of the <strong>this</strong> element
138230
*/
139-
"autocomplete" : function(options, type)
231+
function(options, type)
140232
{
141233
if (type == "text")
142-
{
143234
$(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"))
155252
{
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");
176258
}
177259
});
178260
})(jQuery);

0 commit comments

Comments
 (0)