PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / assets / js / libraries / ddslick.js

ddslick.js in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at assets/js/libraries/ddslick.js

360 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 //Title: Custom DropDown plugin by PC
2 //Documentation: http://designwithpc.com/Plugins/ddslick
3 //Author: PC
4 //Website: http://designwithpc.com
5 //Twitter: http://twitter.com/chaudharyp
6
7 (function ($) {
8
9 $.fn.ddslick = function (method) {
10 if (methods[method]) {
11 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
12 } else if (typeof method === 'object' || !method) {
13 return methods.init.apply(this, arguments);
14 } else {
15 $.error('Method ' + method + ' does not exists.');
16 }
17 };
18
19 var methods = {},
20
21 //Set defauls for the control
22 defaults = {
23 data: [],
24 keepJSONItemsOnTop: false,
25 width: 260,
26 height: null,
27 background: "#eee",
28 selectText: "",
29 defaultSelectedIndex: null,
30 truncateDescription: true,
31 imagePosition: "left",
32 showSelectedHTML: true,
33 clickOffToClose: true,
34 embedCSS: true,
35 onSelected: function () { }
36 },
37
38 ddSelectHtml = '<div class="dd-select"><input class="dd-selected-value" type="hidden" /><a class="dd-selected"></a><span class="dd-pointer dd-pointer-down"></span></div>',
39 ddOptionsHtml = '<ul class="dd-options"></ul>',
40
41 //CSS for ddSlick
42 ddslickCSS = '<style id="css-ddslick" type="text/css">' +
43 '.dd-select{ border-radius:2px; border:solid 1px #ccc; position:relative; cursor:pointer;}' +
44 '.dd-desc { color:#aaa; display:block; overflow: hidden; font-weight:normal; line-height: 1.4em; }' +
45 '.dd-selected{ overflow:hidden; display:block; padding:10px; font-weight:bold;}' +
46 '.dd-pointer{ width:0; height:0; position:absolute; right:10px; top:50%; margin-top:-3px;}' +
47 '.dd-pointer-down{ border:solid 5px transparent; border-top:solid 5px #000; }' +
48 '.dd-pointer-up{border:solid 5px transparent !important; border-bottom:solid 5px #000 !important; margin-top:-8px;}' +
49 '.dd-options{ border:solid 1px #ccc; border-top:none; list-style:none; box-shadow:0px 1px 5px #ddd; display:none; position:absolute; z-index:2000; margin:0; padding:0;background:#fff; overflow:auto;}' +
50 '.dd-option{ padding:10px; display:block; border-bottom:solid 1px #ddd; overflow:hidden; text-decoration:none; color:#333; cursor:pointer;-webkit-transition: all 0.25s ease-in-out; -moz-transition: all 0.25s ease-in-out;-o-transition: all 0.25s ease-in-out;-ms-transition: all 0.25s ease-in-out; }' +
51 '.dd-options > li:last-child > .dd-option{ border-bottom:none;}' +
52 '.dd-option:hover{ background:#f3f3f3; color:#000;}' +
53 '.dd-selected-description-truncated { text-overflow: ellipsis; white-space:nowrap; }' +
54 '.dd-option-selected { background:#f6f6f6; }' +
55 '.dd-option-image, .dd-selected-image { vertical-align:middle; float:left; margin-right:5px; max-width:64px;}' +
56 '.dd-image-right { float:right; margin-right:15px; margin-left:5px;}' +
57 '.dd-container{ position:relative;}​ .dd-selected-text { font-weight:bold}​</style>';
58
59 //Public methods
60 methods.init = function (userOptions) {
61 //Preserve the original defaults by passing an empty object as the target
62 //The object is used to get global flags like embedCSS.
63 var options = $.extend({}, defaults, userOptions);
64
65 //CSS styles are only added once.
66 if ($('#css-ddslick').length <= 0 && options.embedCSS) {
67 $(ddslickCSS).appendTo('head');
68 }
69
70 //Apply on all selected elements
71 return this.each(function () {
72 //Preserve the original defaults by passing an empty object as the target
73 //The object is used to save drop-down's corresponding settings and data.
74 var options = $.extend({}, defaults, userOptions);
75
76 var obj = $(this),
77 data = obj.data('ddslick');
78 //If the plugin has not been initialized yet
79 if (!data) {
80
81 var ddSelect = [], ddJson = options.data;
82
83 //Get data from HTML select options
84 obj.find('option').each(function () {
85 var $this = $(this), thisData = $this.data();
86 ddSelect.push({
87 text: $.trim($this.text()),
88 value: $this.val(),
89 selected: $this.is(':selected'),
90 description: thisData.description,
91 imageSrc: thisData.imagesrc //keep it lowercase for HTML5 data-attributes
92 });
93 });
94
95 //Update Plugin data merging both HTML select data and JSON data for the dropdown
96 if (options.keepJSONItemsOnTop)
97 $.merge(options.data, ddSelect);
98 else options.data = $.merge(ddSelect, options.data);
99
100 //Replace HTML select with empty placeholder, keep the original
101 var original = obj, placeholder = $('<div').attr('id', obj.attr('id') + '-dd-placeholder');
102 obj.replaceWith(placeholder);
103 obj = placeholder;
104
105 //Add classes and append ddSelectHtml & ddOptionsHtml to the container
106 obj.addClass('dd-container').append(ddSelectHtml).append(ddOptionsHtml);
107
108 // Inherit name attribute from original element
109 obj.find("input.dd-selected-value")
110 .attr("id", $(original).attr("id"))
111 .attr("name", $(original).attr("name"));
112
113 //Get newly created ddOptions and ddSelect to manipulate
114 var ddSelect = obj.find('.dd-select'),
115 ddOptions = obj.find('.dd-options');
116
117 //Set widths
118 ddOptions.css({ width: options.width });
119 ddSelect.css({ width: options.width, background: options.background });
120 obj.css({ width: options.width });
121
122 //Set height
123 if (options.height != null)
124 ddOptions.css({ height: options.height, overflow: 'auto' });
125
126 //Add ddOptions to the container. Replace with template engine later.
127 $.each(options.data, function (index, item) {
128 if (item.selected) options.defaultSelectedIndex = index;
129 ddOptions.append('<li>' +
130 '<a class="dd-option">' +
131 (item.value ? ' <input class="dd-option-value" type="hidden" value="' + item.value + '" />' : '') +
132 (item.imageSrc ? ' <img class="dd-option-image' + (options.imagePosition == "right" ? ' dd-image-right' : '') + '" src="' + item.imageSrc + '" />' : '') +
133 (item.text ? ' <label class="dd-option-text">' + item.text + '</label>' : '') +
134 (item.description ? ' <small class="dd-option-description dd-desc">' + item.description + '</small>' : '') +
135 '</a>' +
136 '</li>');
137 });
138
139 //Save plugin data.
140 var pluginData = {
141 settings: options,
142 original: original,
143 selectedIndex: -1,
144 selectedItem: null,
145 selectedData: null
146 }
147 obj.data('ddslick', pluginData);
148
149 //Check if needs to show the select text, otherwise show selected or default selection
150 if (options.selectText.length > 0 && options.defaultSelectedIndex == null) {
151 obj.find('.dd-selected').html(options.selectText);
152 }
153 else {
154 var index = (options.defaultSelectedIndex != null && options.defaultSelectedIndex >= 0 && options.defaultSelectedIndex < options.data.length)
155 ? options.defaultSelectedIndex
156 : 0;
157 selectIndex(obj, index);
158 }
159
160 //EVENTS
161 //Displaying options
162 obj.find('.dd-select').on('click.ddslick', function () {
163 open(obj);
164 });
165
166 //Selecting an option
167 obj.find('.dd-option').on('click.ddslick', function () {
168 selectIndex(obj, $(this).closest('li').index());
169 });
170
171 //Click anywhere to close
172 if (options.clickOffToClose) {
173 ddOptions.addClass('dd-click-off-close');
174 obj.on('click.ddslick', function (e) { e.stopPropagation(); });
175 $('body').on('click', function () {
176 $('.dd-open').removeClass('dd-open');
177 $('.dd-click-off-close').slideUp(50).siblings('.dd-select').find('.dd-pointer').removeClass('dd-pointer-up');
178 });
179 }
180 }
181 });
182 };
183
184 //Public method to select an option by its index
185 methods.select = function (options) {
186 return this.each(function () {
187 if (options.index!==undefined)
188 selectIndex($(this), options.index);
189 if (options.id)
190 selectId($(this), options.id);
191 });
192 }
193
194 //Public method to open drop down
195 methods.open = function () {
196 return this.each(function () {
197 var $this = $(this),
198 pluginData = $this.data('ddslick');
199
200 //Check if plugin is initialized
201 if (pluginData)
202 open($this);
203 });
204 };
205
206 //Public method to close drop down
207 methods.close = function () {
208 return this.each(function () {
209 var $this = $(this),
210 pluginData = $this.data('ddslick');
211
212 //Check if plugin is initialized
213 if (pluginData)
214 close($this);
215 });
216 };
217
218 //Public method to destroy. Unbind all events and restore the original Html select/options
219 methods.destroy = function () {
220 return this.each(function () {
221 var $this = $(this),
222 pluginData = $this.data('ddslick');
223
224 //Check if already destroyed
225 if (pluginData) {
226 var originalElement = pluginData.original;
227 $this.removeData('ddslick').unbind('.ddslick').replaceWith(originalElement);
228 }
229 });
230 }
231
232 //Private: Select id
233 function selectId(obj, id) {
234
235 var index = obj.find(".dd-option-value[value= '" + id + "']").parents("li").prevAll().length;
236 selectIndex(obj, index);
237
238 }
239
240 //Private: Select index
241 function selectIndex(obj, index) {
242
243 //Get plugin data
244 var pluginData = obj.data('ddslick');
245
246 //Get required elements
247 var ddSelected = obj.find('.dd-selected'),
248 ddSelectedValue = ddSelected.siblings('.dd-selected-value'),
249 ddOptions = obj.find('.dd-options'),
250 ddPointer = ddSelected.siblings('.dd-pointer'),
251 selectedOption = obj.find('.dd-option').eq(index),
252 selectedLiItem = selectedOption.closest('li'),
253 settings = pluginData.settings,
254 selectedData = pluginData.settings.data[index];
255
256 //Highlight selected option
257 obj.find('.dd-option').removeClass('dd-option-selected');
258 selectedOption.addClass('dd-option-selected');
259
260 //Update or Set plugin data with new selection
261 pluginData.selectedIndex = index;
262 pluginData.selectedItem = selectedLiItem;
263 pluginData.selectedData = selectedData;
264
265 //If set to display to full html, add html
266 if (settings.showSelectedHTML) {
267 ddSelected.html(
268 (selectedData.imageSrc ? '<img class="dd-selected-image' + (settings.imagePosition == "right" ? ' dd-image-right' : '') + '" src="' + selectedData.imageSrc + '" />' : '') +
269 (selectedData.text ? '<label class="dd-selected-text">' + selectedData.text + '</label>' : '') +
270 (selectedData.description ? '<small class="dd-selected-description dd-desc' + (settings.truncateDescription ? ' dd-selected-description-truncated' : '') + '" >' + selectedData.description + '</small>' : '')
271 );
272
273 }
274 //Else only display text as selection
275 else ddSelected.html(selectedData.text);
276
277 //Updating selected option value
278 ddSelectedValue.val(selectedData.value);
279
280 //BONUS! Update the original element attribute with the new selection
281 pluginData.original.val(selectedData.value);
282 obj.data('ddslick', pluginData);
283
284 //Close options on selection
285 close(obj);
286
287 //Adjust appearence for selected option
288 adjustSelectedHeight(obj);
289
290 //Callback function on selection
291 if (typeof settings.onSelected == 'function') {
292 settings.onSelected.call(this, pluginData);
293 }
294 }
295
296 //Private: Close the drop down options
297 function open(obj) {
298
299 var $this = obj.find('.dd-select'),
300 ddOptions = $this.siblings('.dd-options'),
301 ddPointer = $this.find('.dd-pointer'),
302 wasOpen = ddOptions.is(':visible');
303
304 //Close all open options (multiple plugins) on the page
305 $('.dd-click-off-close').not(ddOptions).slideUp(50);
306 $('.dd-pointer').removeClass('dd-pointer-up');
307 $this.removeClass('dd-open');
308
309 if (wasOpen) {
310 ddOptions.slideUp('fast');
311 ddPointer.removeClass('dd-pointer-up');
312 $this.removeClass('dd-open');
313 }
314 else {
315 $this.addClass('dd-open');
316 ddOptions.slideDown('fast');
317 ddPointer.addClass('dd-pointer-up');
318 }
319
320 //Fix text height (i.e. display title in center), if there is no description
321 adjustOptionsHeight(obj);
322 }
323
324 //Private: Close the drop down options
325 function close(obj) {
326 //Close drop down and adjust pointer direction
327 obj.find('.dd-select').removeClass('dd-open');
328 obj.find('.dd-options').slideUp(50);
329 obj.find('.dd-pointer').removeClass('dd-pointer-up').removeClass('dd-pointer-up');
330 }
331
332 //Private: Adjust appearence for selected option (move title to middle), when no desripction
333 function adjustSelectedHeight(obj) {
334
335 //Get height of dd-selected
336 var lSHeight = obj.find('.dd-select').css('height');
337
338 //Check if there is selected description
339 var descriptionSelected = obj.find('.dd-selected-description');
340 var imgSelected = obj.find('.dd-selected-image');
341 if (descriptionSelected.length <= 0 && imgSelected.length > 0) {
342 obj.find('.dd-selected-text').css('lineHeight', lSHeight);
343 }
344 }
345
346 //Private: Adjust appearence for drop down options (move title to middle), when no desripction
347 function adjustOptionsHeight(obj) {
348 obj.find('.dd-option').each(function () {
349 var $this = $(this);
350 var lOHeight = $this.css('height');
351 var descriptionOption = $this.find('.dd-option-description');
352 var imgOption = obj.find('.dd-option-image');
353 if (descriptionOption.length <= 0 && imgOption.length > 0) {
354 $this.find('.dd-option-text').css('lineHeight', lOHeight);
355 }
356 });
357 }
358
359 })(jQuery);
360