forked from keobrien/Fancy-Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fancy_forms.js
More file actions
186 lines (137 loc) · 5.24 KB
/
jquery.fancy_forms.js
File metadata and controls
186 lines (137 loc) · 5.24 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Project Name: Fancy Forms
URL: https://github.com/keobrien/Fancy-Forms
Author: Kevin O'Brien
Company: Clockwork Acive Media Systems
Company Site: clockwork.net
License: MIT
Copyright (C) 2012 Clockwork Active Media Systems
Version: 0.1
**************************************/
(function ($) {
"use strict";
// These locally scoped variables help minification by aliasing strings
var data_key = 'CW_fancy_forms',
event_suffix = '.CW_fancy_forms',
// Custom events
component_initialized = 'component_initialized' + event_suffix,
component_error = 'component_error' + event_suffix,
event_change = 'fancy_change' + event_suffix;
var methods = {
/* Selects
*****************/
_init_selects : function (settings) {
if($(this).hasClass('fancy_selects-active')) { return this; }
var $el = $(this),
options = $.extend({
content : null,
selected : $el.find('option:selected'),
wrap_class : 'fancy_selects-wrap',
// consider a private function
wrap_id : $el.attr('id') ? 'fancy_selects-' + $el.attr('id') : '',
wrap_tag : 'div',
visible_class : 'fancy_selects-visible',
visible_tag : 'div'
}, settings);
var visible_content = options.content || options.selected.html();
options.wrap_html = '<' + options.wrap_tag + ' class="' + options.wrap_class + '" id="' + options.wrap_id + '"></' + options.wrap_tag + '>';
options.visible_html = '<' + options.visible_tag + ' class="' + options.visible_class + '">' + visible_content + '</' + options.visible_tag + '>';
$el.data(data_key, options);
methods._make_fancy_select($el);
return this;
},
_make_fancy_select : function ($el) {
var options = $el.data(data_key);
$el.wrap(options.wrap_html).addClass('fancy_selects-active');
options.$wrap = $el.parent();
options.$visible = $(options.visible_html).appendTo(options.$wrap);
if ($.browser.msie && parseInt($.browser.version, 10) < 8) {
$('<div class="fancy_selects-fallback_button">ν</div>').appendTo(options.$wrap);
}
methods.apply_fancy_css($el, options.$wrap);
$el
.change(methods.update_selected_select)
.on(event_change, $wrap, methods.update_selected_select);
$el.trigger(component_initialized, $wrap, options);
},
update_selected_select : function (evt) {
var $el = $(evt.currentTarget),
options = $el.data(data_key);
options.selected = options.$wrap.find('option:selected');
options.$visible.html(options.selected.html());
},
/* Shared
*****************/
apply_fancy_css : function($el, $wrapper) {
$wrapper
.css({
'position' : 'relative',
'width' : $el.outerWidth() + 'px'
})
.addClass($el.attr('class'))
.attr('id', data_key + '-' + $el.attr('id' ));
$el.css({
'opacity' : 0,
'min-height' : '100%',
'min-width' : '100%',
'position' : 'absolute',
'top' : 0,
'left' : 0,
'z-index' : 2,
'cursor' : 'pointer'
});
},
/* Checkboxes
*****************/
_init_checks : function (settings) {
var $el = $(this),
options = $.extend({
selected_class : 'fancy_checkbox-checked',
wrap_class : 'fancy_checkbox-wrap',
wrap_id : $el.attr('id') ? 'fancy_checkbox-' + $el.attr('id') : '',
wrap_tag : 'div'
}, settings);
options.wrap_html = '<' + options.wrap_tag + ' class="' + options.wrap_class + '" id="' + options.wrap_id + '"></' + options.wrap_tag + '>';
$el.data(data_key, options);
methods._make_fancy_checks($el);
return this;
},
_make_fancy_checks : function ($el) {
var options = $el.data(data_key);
$el
.wrap(options.wrap_html)
.change(methods.update_selected_check)
.each(methods.update_selected_check)
.trigger(component_initialized, options)
.on(event_change, $wrap, methods.update_selected_select);
methods.apply_fancy_css($el, $el.parent());
},
update_selected_check : function (evt) {
var options = evt ? $(evt.currentTarget).data(data_key) : $(this).data(data_key),
$el = $(this),
checked = $el.attr('checked');
if(checked === 'checked') {
$el.parent().addClass(options.selected_class);
}else {
$el.parent().removeClass(options.selected_class);
}
$el.trigger(event_change, options);
}
};
$.fn.cw_fancy_selects = function (method) {
if (methods[method] && method.charAt(0) !== '_') {
return $(this).map(function(i, val) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); });
} else if (typeof method === 'object' || !method) {
var args = arguments;
return $(this).map(function(i, val) { return methods._init_selects.apply(this, args); });
}
};
$.fn.cw_fancy_checks = function (method) {
if (methods[method] && method.charAt(0) !== '_') {
return $(this).map(function(i, val) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); });
} else if (typeof method === 'object' || !method) {
var args = arguments;
return $(this).map(function(i, val) { return methods._init_checks.apply(this, args); });
}
};
}(jQuery));