-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.fancy_forms.js
More file actions
148 lines (125 loc) · 4.87 KB
/
jquery.fancy_forms.js
File metadata and controls
148 lines (125 loc) · 4.87 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
/*
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.2
**************************************/
(function ($) {
"use strict";
// These locally scoped variables help minification by aliasing strings
var data_key = 'CW_fancy_forms',
event_suffix = '.CW_fancy_forms',
class_prefix = 'fancy-',
// Custom events
component_initialized = 'component_initialized' + event_suffix,
$elements = $(), // Used to filter propHooks to plugin elements only
$radios = $(); // Used to make the radio updater faster
var methods = {
_init_el : function (settings) {
if($(this).hasClass(class_prefix + 'active')) { return this; }
var $el = $(this),
type = $el.prop("tagName").toLowerCase(),
type = type === 'select' ? type : $el.attr('type'),
options = $.extend({
// Settings for selects, checkboxes and radios
selected_class : class_prefix + type + '-checked',
disabled_class : class_prefix + type + '-disabled',
wrap_class : class_prefix + type +'-wrap',
wrap_id : $el.attr('id') ? class_prefix + type +'-' + $el.attr('id') : '',
wrap_tag : 'div',
type : type
}, settings);
if( options.wrap_id !== '') {
options.wrap_html = '<' + options.wrap_tag + ' class="' + options.wrap_class + '" id="' + options.wrap_id + '"></' + options.wrap_tag + '>';
}
else {
options.wrap_html = '<' + options.wrap_tag + ' class="' + options.wrap_class + '"></' + options.wrap_tag + '>';
}
$elements = $elements.add(this);
if(type === 'select') {
// Settings specific to selects
options = $.extend({
selected : $el.find('option:selected'),
visible_class : class_prefix + type +'-visible',
fallback : '<div class="'+ class_prefix + type +'-fallback_button"></div>',
visible_tag : 'div'
}, options);
options.visible_html = '<' + options.visible_tag + ' class="' + options.visible_class + '">' + options.selected.html() + '</' + options.visible_tag + '>';
}else {
// Makes sure change event is fired when jQuery change is called on element
$.propHooks.checked = {
set: function(el, value) {
if ($elements.filter(el).length !== 0) {
el.checked = value;
$(el).trigger('change');
}
}
};
if(type === 'radio') {
$radios = $radios.add(this);
}
}
$el.data(data_key, options);
methods._make.apply(this);
return this;
},
_make : function (evt) {
var $el = $(this),
options = evt ? $(evt.currentTarget).data(data_key) : $(this).data(data_key);
options.$wrap = $el.wrap(options.wrap_html).parent()
.css({
'width' : $el.outerWidth() + 'px',
'height' : $el.outerHeight() + 'px'
})
if($el.attr('class')) {
options.$wrap.addClass(class_prefix + $el.attr('class'))
}
if( $el.attr('id' ) ) {
options.$wrap.attr('id', class_prefix + $el.attr('id' ));
}
if(options.type === 'select') {
options.$visible = $(options.visible_html).appendTo(options.$wrap);
if ($.browser.msie && parseInt($.browser.version, 10) < 8) {
$(options.fallback).appendTo(options.$wrap);
}
}else if(options.type === 'radio') {
// Need to check all since checking one in a group doesn't fire change on the element that looses checked
$el.change(function(){ $radios.each(function(){ methods.update.apply(this) }); });
}
$el
.change(methods.update)
.each(methods.update)
.trigger(component_initialized, options.$wrap, options)
.addClass(class_prefix +'active');
return this;
},
/* usage: jQuery( element ).cw_fancy_forms("update") */
update : function (evt) {
var $el = $(this),
options = evt ? $(evt.currentTarget).data(data_key) : $(this).data(data_key),
checked = $el.prop('checked'),
disabled = $el.prop('disabled');
if((options.type === 'select') && options.$visible) {
options.selected = options.$wrap.find('option:selected');
options.$visible.html(options.selected.html());
}else {
checked === true ? $el.parent().addClass(options.selected_class) : $el.parent().removeClass(options.selected_class);
}
if(disabled) {
$el.parent().addClass(options.disabled_class);
}
}
};
$.fn.cw_fancy_forms = 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_el.apply(this, args); });
}
};
}(jQuery));