Skip to content

Commit 842d335

Browse files
author
daffl
committed
Tests update and refactoring
1 parent c0b3a67 commit 842d335

File tree

4 files changed

+251
-358
lines changed

4 files changed

+251
-358
lines changed

src/dform.core.js

+176-178
Original file line numberDiff line numberDiff line change
@@ -24,195 +24,193 @@
2424
}
2525
};
2626

27-
$.dform.addType(
28-
{
29-
container : _element("<div>"),
30-
text : _element('<input type="text" />'),
31-
password : _element('<input type="password" />'),
32-
submit : _element('<input type="submit" />'),
33-
reset : _element('<input type="reset" />'),
34-
hidden : _element('<input type="hidden" />'),
35-
radio : _element('<input type="radio" />'),
36-
checkbox : _element('<input type="checkbox" />'),
37-
file : _element('<input type="file" />'),
38-
number : _element('<input type="number" />'),
39-
url : _element('<input type="url" />'),
40-
tel : _element('<input type="tel" />'),
41-
email : _element('<input type="email" />'),
42-
checkboxes : _element("<div>", ["name"]),
43-
radiobuttons : _element("<div>", ["name"])
44-
});
27+
$.dform.addType({
28+
container : _element("<div>"),
29+
text : _element('<input type="text" />'),
30+
password : _element('<input type="password" />'),
31+
submit : _element('<input type="submit" />'),
32+
reset : _element('<input type="reset" />'),
33+
hidden : _element('<input type="hidden" />'),
34+
radio : _element('<input type="radio" />'),
35+
checkbox : _element('<input type="checkbox" />'),
36+
file : _element('<input type="file" />'),
37+
number : _element('<input type="number" />'),
38+
url : _element('<input type="url" />'),
39+
tel : _element('<input type="tel" />'),
40+
email : _element('<input type="email" />'),
41+
checkboxes : _element("<div>", ["name"]),
42+
radiobuttons : _element("<div>", ["name"])
43+
});
4544

46-
$.dform.subscribe(
47-
{
48-
/**
49-
* Adds a class to the current element.
50-
* Ovverrides the default behaviour which would be replacing the class attribute.
51-
*
52-
* @param options A list of whitespace separated classnames
53-
* @param type The type of the *this* element
54-
*/
55-
"class" : function (options, type) {
56-
this.addClass(options);
57-
},
58-
59-
/**
60-
* Sets html content of the current element
61-
*
62-
* @param options The html content to set as a string
63-
* @param type The type of the *this* element
64-
*/
65-
html : _html,
66-
67-
/**
68-
* Recursively appends subelements to the current form element.
69-
*
70-
* @param options Either an object with key value pairs
71-
* where the key is the element name and the value the
72-
* subelement options or an array of objects where each object
73-
* is the options for a subelement
74-
* @param type The type of the *this* element
75-
*/
76-
elements : _html,
45+
$.dform.subscribe({
46+
/**
47+
* Adds a class to the current element.
48+
* Ovverrides the default behaviour which would be replacing the class attribute.
49+
*
50+
* @param options A list of whitespace separated classnames
51+
* @param type The type of the *this* element
52+
*/
53+
"class" : function (options, type) {
54+
this.addClass(options);
55+
},
7756

78-
/**
79-
* Sets the value of the current element.
80-
*
81-
* @param options The value to set
82-
* @param type The type of the *this* element
83-
*/
84-
value : function (options) {
85-
this.val(options);
86-
},
57+
/**
58+
* Sets html content of the current element
59+
*
60+
* @param options The html content to set as a string
61+
* @param type The type of the *this* element
62+
*/
63+
"html" : _html,
8764

88-
/**
89-
* Set CSS styles for the current element
90-
*
91-
* @param options The Styles to set
92-
* @param type The type of the *this* element
93-
*/
94-
css : function (options) {
95-
this.css(options);
96-
},
65+
/**
66+
* Recursively appends subelements to the current form element.
67+
*
68+
* @param options Either an object with key value pairs
69+
* where the key is the element name and the value the
70+
* subelement options or an array of objects where each object
71+
* is the options for a subelement
72+
* @param type The type of the *this* element
73+
*/
74+
"elements" : _html,
9775

98-
/**
99-
* Adds options to select type elements or radio and checkbox list elements.
100-
*
101-
* @param options A key value pair where the key is the
102-
* option value and the value the options text or the settings for the element.
103-
* @param type The type of the *this* element
104-
*/
105-
options : function (options, type) {
106-
var self = this;
107-
if (type === "select" || type === "optgroup") // Options for select elements
108-
{
109-
each(options, function (value, content) {
110-
var option = { type : 'option', value : value };
111-
if (typeof (content) === "string") {
112-
option.html = content;
113-
}
114-
if (typeof (content) === "object") {
115-
option = $.extend(option, content);
116-
}
117-
self.dform('append', option);
118-
});
119-
}
120-
else if (type === "checkboxes" || type === "radiobuttons") {
121-
// Options for checkbox and radiobutton lists
122-
each(options, function (value, content) {
123-
var boxoptions = ((type === "radiobuttons") ? { "type" : "radio" } : { "type" : "checkbox" });
124-
if (typeof(content) === "string") {
125-
boxoptions["caption"] = content;
126-
} else {
127-
$.extend(boxoptions, content);
128-
}
129-
boxoptions["value"] = value;
130-
self.dform('append', boxoptions);
131-
});
132-
}
133-
},
76+
/**
77+
* Sets the value of the current element.
78+
*
79+
* @param options The value to set
80+
* @param type The type of the *this* element
81+
*/
82+
"value" : function (options) {
83+
this.val(options);
84+
},
13485

135-
/**
136-
* Adds caption to elements.
137-
*
138-
* Depending on the element type the following elements will
139-
* be used:
140-
* - A legend for <fieldset> elements
141-
* - A <label> next to <radio> or <checkbox> elements
142-
* - A <label> before any other element
143-
*
144-
* @param options A string for the caption or the options for the
145-
* @param type The type of the *this* element
146-
*/
147-
caption : function (options, type) {
148-
var ops = {};
149-
if (typeof (options) === "string") {
150-
ops["html"] = options;
151-
} else {
152-
$.extend(ops, options);
153-
}
86+
/**
87+
* Set CSS styles for the current element
88+
*
89+
* @param options The Styles to set
90+
* @param type The type of the *this* element
91+
*/
92+
"css" : function (options) {
93+
this.css(options);
94+
},
15495

155-
if (type == "fieldset") {
156-
// Labels for fieldsets are legend
157-
ops.type = "legend";
158-
this.dform('append', ops);
159-
} else {
160-
ops.type = "label";
161-
if (this.attr("id")) {
162-
ops["for"] = this.attr("id");
96+
/**
97+
* Adds options to select type elements or radio and checkbox list elements.
98+
*
99+
* @param options A key value pair where the key is the
100+
* option value and the value the options text or the settings for the element.
101+
* @param type The type of the *this* element
102+
*/
103+
"options" : function (options, type) {
104+
var self = this;
105+
if (type === "select" || type === "optgroup") // Options for select elements
106+
{
107+
each(options, function (value, content) {
108+
var option = { type : 'option', value : value };
109+
if (typeof (content) === "string") {
110+
option.html = content;
111+
}
112+
if (typeof (content) === "object") {
113+
option = $.extend(option, content);
163114
}
164-
var label = $($.dform.createElement(ops));
165-
if (type === "checkbox" || type === "radio") {
166-
this.parent().append($(label));
115+
self.dform('append', option);
116+
});
117+
}
118+
else if (type === "checkboxes" || type === "radiobuttons") {
119+
// Options for checkbox and radiobutton lists
120+
each(options, function (value, content) {
121+
var boxoptions = ((type === "radiobuttons") ? { "type" : "radio" } : { "type" : "checkbox" });
122+
if (typeof(content) === "string") {
123+
boxoptions["caption"] = content;
167124
} else {
168-
label.insertBefore(this);
125+
$.extend(boxoptions, content);
169126
}
170-
label.dform('run', ops);
171-
}
172-
},
127+
boxoptions["value"] = value;
128+
self.dform('append', boxoptions);
129+
});
130+
}
131+
},
173132

174-
/**
175-
* The subscriber for the type parameter.
176-
* Although the type parameter is used to get the correct element
177-
* type it is just treated as a simple subscriber otherwise.
178-
* Since every element needs a type
179-
* parameter feel free to add other type subscribers to do
180-
* any processing between [pre] and [post].
181-
*
182-
* This subscriber adds the auto generated classes according
183-
* to the type prefix in $.dform.options.prefix.
184-
*
185-
* @param options The name of the type
186-
* @param type The type of the *this* element
187-
*/
188-
type : function (options, type) {
189-
if ($.dform.options.prefix) {
190-
this.addClass($.dform.options.prefix + type);
191-
}
192-
},
193-
/**
194-
* Retrieves JSON data from a URL and creates a sub form.
195-
*
196-
* @param options
197-
* @param type
198-
*/
199-
url : function (options, type) {
200-
// TODO this.buildForm(options);
201-
},
133+
/**
134+
* Adds caption to elements.
135+
*
136+
* Depending on the element type the following elements will
137+
* be used:
138+
* - A legend for <fieldset> elements
139+
* - A <label> next to <radio> or <checkbox> elements
140+
* - A <label> before any other element
141+
*
142+
* @param options A string for the caption or the options for the
143+
* @param type The type of the *this* element
144+
*/
145+
"caption" : function (options, type) {
146+
var ops = {};
147+
if (typeof (options) === "string") {
148+
ops["html"] = options;
149+
} else {
150+
$.extend(ops, options);
151+
}
202152

203-
/**
204-
* Post processing function, that will run whenever all other subscribers are finished.
205-
*
206-
* @param options All options that have been used for
207-
* @param type The type of the *this* element
208-
*/
209-
"[post]" : function (options, type) {
210-
if (type === "checkboxes" || type === "radiobuttons") {
211-
var boxtype = ((type === "checkboxes") ? "checkbox" : "radio");
212-
this.children("[type=" + boxtype + "]").each(function () {
213-
$(this).attr("name", options.name);
214-
});
153+
if (type == "fieldset") {
154+
// Labels for fieldsets are legend
155+
ops.type = "legend";
156+
this.dform('append', ops);
157+
} else {
158+
ops.type = "label";
159+
if (this.attr("id")) {
160+
ops["for"] = this.attr("id");
161+
}
162+
var label = $($.dform.createElement(ops));
163+
if (type === "checkbox" || type === "radio") {
164+
this.parent().append($(label));
165+
} else {
166+
label.insertBefore(this);
215167
}
168+
label.dform('run', ops);
169+
}
170+
},
171+
172+
/**
173+
* The subscriber for the type parameter.
174+
* Although the type parameter is used to get the correct element
175+
* type it is just treated as a simple subscriber otherwise.
176+
* Since every element needs a type
177+
* parameter feel free to add other type subscribers to do
178+
* any processing between [pre] and [post].
179+
*
180+
* This subscriber adds the auto generated classes according
181+
* to the type prefix in $.dform.options.prefix.
182+
*
183+
* @param options The name of the type
184+
* @param type The type of the *this* element
185+
*/
186+
"type" : function (options, type) {
187+
if ($.dform.options.prefix) {
188+
this.addClass($.dform.options.prefix + type);
189+
}
190+
},
191+
/**
192+
* Retrieves JSON data from a URL and creates a sub form.
193+
*
194+
* @param options
195+
* @param type
196+
*/
197+
"url" : function (options, type) {
198+
// TODO this.buildForm(options);
199+
},
200+
201+
/**
202+
* Post processing function, that will run whenever all other subscribers are finished.
203+
*
204+
* @param options All options that have been used for
205+
* @param type The type of the *this* element
206+
*/
207+
"[post]" : function (options, type) {
208+
if (type === "checkboxes" || type === "radiobuttons") {
209+
var boxtype = ((type === "checkboxes") ? "checkbox" : "radio");
210+
this.children("[type=" + boxtype + "]").each(function () {
211+
$(this).attr("name", options.name);
212+
});
216213
}
217-
});
214+
}
215+
});
218216
})(jQuery);

0 commit comments

Comments
 (0)