-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyling.js
More file actions
184 lines (175 loc) · 6.62 KB
/
styling.js
File metadata and controls
184 lines (175 loc) · 6.62 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
var isMobileOrTablet = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
(function($) {
'use strict';
$.stylingSettings = {
chooseFileTxt : 'Choose file',
noFiletxt : 'No file chosen',
};
// defined in product.js
if (typeof product_fileButtonHtml != 'undefined')
$.stylingSettings.chooseFileTxt = product_fileButtonHtml;
if (typeof product_fileDefaultHtml != 'undefined')
$.stylingSettings.noFiletxt = product_fileDefaultHtml;
// defined in contact-form.js
if (typeof contact_fileButtonHtml != 'undefined')
$.stylingSettings.chooseFileTxt = contact_fileButtonHtml;
if (typeof contact_fileDefaultHtml != 'undefined')
$.stylingSettings.noFiletxt = contact_fileDefaultHtml;
$.fn.addStyling = function(settings) {
settings = $.extend({}, $.stylingSettings, settings);
this.each(function(){
if (this.tagName == 'SELECT' && !isMobileOrTablet){
if (!$(this).parent().hasClass('styled-select'))
$(this).addClass('wrapped').wrap('<div class="styled-select"></div>');
$(this).parent().find('dl').remove();
var options = $(this).find('option, optgroup');
if (!options.length)
return;
var optionsHTML = '',
longestTxt = '',
maxChars = 0;
options.each(function(i){
var val = $(this).val(),
optClass = 'option '+$.trim($(this).attr('class') || '')+($(this).is(':disabled') ? ' disabled' : '')+(!i ? ' first' : ''),
optContent = $($(this)).text();
if (this.nodeName == 'OPTGROUP'){
val = '';
optContent = $($(this)).attr('label');
optClass = 'optgroup '+$.trim($(this).attr('class') || '');
}
var data = $($(this)).data();
var dataHTML = '';
for (var i in data)
dataHTML += ' data-'+i+'="'+data[i]+'"';
optionsHTML += '<li data-val="'+val+'" class="'+optClass+'"'+dataHTML+'>'+optContent+'</li>';
var txt = $(this).text();
if (txt.length > maxChars) {
maxChars = txt.length;
longestTxt = $(this).text();
}
});
var newHTML = '<dl class="closed"><dt class="option"><span class="longest-txt">'+longestTxt+'</span><span class="selected_name">'+$(this).find('option:selected').text()+'</span><i class="toggle"></i></dt><dd><ul class="styled-options">';
newHTML += optionsHTML;
newHTML += '</ul></dd></dl>';
$(this).parent().append(newHTML);
// fieldset fix
if ($(this).closest('fieldset').length)
{
var resizeTimer;
var $container = $(this).closest('.styledSelect');
$container.css('max-width', $container.closest('fieldset').parent().innerWidth()+'px');
$(window).resize(function(){
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
$container.css('max-width', $container.closest('fieldset').parent().innerWidth()+'px');
}, 200);
});
}
}
else if (this.tagName == 'INPUT'){
var type = $(this).attr('type'),
cls = $(this).attr('class') || '';
if ( type == 'checkbox' || type == 'radio' || type == 'file'){
if (!$(this).parent().hasClass('styled-'+type)){
$(this).wrap('<span class="styled-'+type+' '+cls+'"></span>');
if (type == 'file'){
$(this).before('<span class="file-button">'+settings.chooseFileTxt+'</span><span class="file-name">'+settings.noFiletxt+'</span>');
}
}
if (type != 'file' && $(this).prop('checked'))
$(this).parent().addClass('checked');
}
}
if ($(this).is(':disabled'))
$(this).parent().addClass('disabled');
if (isMobileOrTablet)
$(this).parent().addClass('mobile');
});
}
// select events
$(document).on('click', function(e) {
if (!$(e.target).closest('.styled-select').length)
$('.styled-select dl').addClass('closed');
}).on('click', '.styled-select dt', function(){
var closed = $(this).parent().hasClass('closed');
$('.styled-select dl').addClass('closed');
if (closed){
$(this).parent().removeClass('closed');
var $ul = $(this).closest('.styled-select').find('ul');
if (!$ul.hasClass('prepared')){
var maxHeight = 3;
$ul.find('li:visible').each(function(i){
if (i > 9)
return false;
maxHeight += $(this).outerHeight();
});
$ul.css('max-height', maxHeight+'px').addClass('prepared');
}
try {
var viewPortPosition = this.getBoundingClientRect();
var toTop = viewPortPosition.top;
var toBottom = $(window).height() - viewPortPosition.bottom;
if (toTop > toBottom && toBottom < $ul.outerHeight())
$ul.addClass('above');
else
$ul.removeClass('above');
}catch(err){};
}
}).on('click', '.styled-select li.option', function(){
$(this).closest('dl').addClass('closed').find('.selected_name').html($(this).html());
if (!$(this).hasClass('dont-bubble')) {
$(this).closest('.styled-select').find('select').val($(this).attr('data-val')).change().click();
} else {
$(this).removeClass('dont-bubble');
}
}).on('change', 'select.wrapped', function(){
var val = $(this).val();
$(this).parent().find('li[data-val="'+val+'"]').addClass('dont-bubble').click();
}).on('mousewheel.styling DOMMouseScroll.styling', '.styled-options', function(e){
/*
* copied from jquery.chosen
*/
var delta, _ref1, _ref2;
delta = -((_ref1 = e.originalEvent) != null ? _ref1.wheelDelta : void 0) || ((_ref2 = e.originialEvent) != null ? _ref2.detail : void 0);
if (delta != null) {
e.preventDefault();
if (e.type === 'DOMMouseScroll') {
delta = delta * 40;
}
$(this).scrollTop(delta + $(this).scrollTop());
}
});
// checkbox/radio events
// in some cases click or change can be unbound, so bind both of them here
$(document).on('change click', '.styled-checkbox input, .styled-radio input', function(e){
// avoid double actions. click is automatically triggered before change
if (e.type == 'click') {
$(this).data('justclicked', true);
} else if (e.type == 'change' && $(this).data('justclicked')) {
$(this).data('justclicked', '');
return;
}
if ($(this).attr('type') == 'radio')
$('[type="radio"][name="'+$(this).attr('name')+'"]').each(function(){
$(this).parent().toggleClass('checked', $(this).prop('checked'));
});
$(this).parent().toggleClass('checked', $(this).prop('checked'));
});
// file events
$(document).on('change', '.styled-file input', function(){
var fileName = [];
for (var i in this.files)
if ($.isNumeric(i))
fileName.push(this.files[i].name);
fileName = fileName.join(', ');
$(this).siblings('.file-name').html(fileName);
});
// in some places uniform is called without pre-check
$.fn.uniform = function (settings) {
this.addStyling(settings);
}
$.uniform = {defaults:{},update:function(){}};
}(jQuery));
$(window).load(function(){
$('select, input').addStyling();
});