PluginProbe
WP Directory Kit / 1.3.6
WP Directory Kit v1.3.6
1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 All 36 releases
wpdirectorykit / public / js / wdk_treefield_checkboxes / wdk_treefield_checkboxes.js

wdk_treefield_checkboxes.js in WP Directory Kit 1.3.6, at public/js/wdk_treefield_checkboxes/wdk_treefield_checkboxes.js

489 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1
2 /*
3 Item Name: Winter Treefield
4 Author: wpdirectorykit
5 Author URI: https://wpdirectorykit.com/
6 Version: 1.0
7 */
8
9 jQuery.fn.wdkTreefieldCheckboxes = function (options)
10 {
11 var defaults = {
12 ajax_url: null,
13 ajax_param: {},
14 text_search: 'Search term',
15 text_no_results: 'No results found',
16 per_page: 10,
17 offset: 0,
18 attribute_id: 'id',
19 attribute_value: 'value',
20 hide_fields: '',
21 language_id: null,
22 user_check: 'false',
23 selected: null,
24 skip_id: null,
25 empty_value: ' - ',
26 callback_selected: function(key) {
27 console.log('called callback: '+key);
28 }
29 };
30
31 var options = jQuery.extend(defaults, options);
32
33 var jqxhr;
34 var is_loading = false;
35 var request;
36 var search_enter_focus = false;
37
38 /* Public API */
39 /*
40 this.getCurrent = function()
41 {
42 return options.currElImg;
43 }
44
45 this.getIndex = function(){
46 return options.currIndex;
47 };
48 */
49
50 return this.each (function ()
51 {
52 options.obj = jQuery(this);
53
54 options.firstLoad=true;
55 options.endLoad=false;
56
57 options.currValue=options.empty_value;
58 if(options.obj.data('placehoder') != '') {
59 options.currValue = options.obj.data('placehoder');
60 } else if(options.obj.val() != ''){
61 options.currValue = options.obj.val();
62 }
63
64 generateHtml();
65
66 // First value/placeholder
67
68 // options.obj.parent().find('.btn-group:first button').html();
69
70 // Add loading indicator
71 options.obj.parent().find(".circle-loading-bar").addClass(options.progressBar);
72
73 // open scroll part
74 options.obj.parent().find('.btn-group:first button').on('click', function(event) {
75 var container = options.obj.parent().find('.list_container');
76
77 if( container.hasClass('win_visible') )
78 {
79 container.hide();
80 container.removeClass('win_visible');
81 container.parent().removeClass('win_open');
82 }
83 else
84 {
85 container.show();
86 container.parent().addClass('win_open');
87 container.addClass('win_visible');
88 if(options.firstLoad)
89 options.obj.parent().find('.list_scroll').scrollTop(0);
90 options.firstLoad=false;
91
92 jQuery(options.obj.parent().find('.search_term')).focus();
93 }
94
95 options.obj.parent().find('.list_scroll li').removeClass('selected');
96
97 return false;
98 }).on('keypress', function(event) {
99 var keyCode = event.keyCode || event.which;
100
101 if(keyCode == 13) // Enter
102 {
103 var container = options.obj.parent().find('.list_container');
104
105 if( container.hasClass('win_visible') )
106 {
107 container.hide();
108 container.removeClass('win_visible');
109 container.parent().removeClass('win_open');
110 }
111 else
112 {
113 container.show();
114 container.parent().addClass('win_open');
115 container.addClass('win_visible');
116 if(options.firstLoad)
117 options.obj.parent().find('.list_scroll').scrollTop(0);
118 options.firstLoad=false;
119
120 //options.obj.parent().find('.list_scroll li').first().addClass('selected');
121
122 search_enter_focus = true;
123 jQuery(options.obj.parent().find('.search_term')).focus();
124 }
125
126 return false;
127 }
128 })
129
130 // hide when click outside
131 jQuery(document).on('mouseup keyup', function (e)
132 {
133 var container = options.obj.parent().find('.wdk_dropdown_tree');
134 var container_hidder = options.obj.parent().find('.list_container');
135
136 if (!container.is(e.target) // if the target of the click isn't the container...
137 && container.has(e.target).length === 0) // ... nor a descendant of the container
138 {
139 container_hidder.hide();
140 container_hidder.parent().removeClass('win_open');
141 container_hidder.removeClass('win_visible');
142 }
143 });
144
145 // load first n values
146 loadMore();
147
148 // scroll
149 var scroll_container = options.obj.parent().find('.list_scroll');
150 var list_items = options.obj.parent().find('.list_items');
151
152 jQuery(scroll_container).on('scroll', function () {
153 if (jQuery(scroll_container).scrollTop() + jQuery(scroll_container).height() >= (jQuery(list_items).height() - 3)) {
154 if(!options.endLoad)
155 loadMore();
156 }
157 });
158
159 // keypress/typing event
160 jQuery(options.obj.parent().find('.search_term')).on('keyup', function(event) {
161 var keyCode = event.keyCode || event.which;
162
163 if(keyCode == 13) // Enter
164 {
165 if(options.obj.parent().find('.list_scroll li.selected').length > 0)
166 {
167 if(!search_enter_focus)
168 {
169 var elSel = options.obj.parent().find('.list_scroll li.selected');
170
171 options.obj.parent().find('button:first-child').html(elSel.html());
172 options.obj.val(elSel.attr('key'));
173
174 options.obj.parent().find('.list_container').hide();
175 options.obj.parent().find('.list_container').parent().removeClass('win_open');
176 options.obj.parent().find('.list_container').removeClass('win_visible');
177 options.obj.parent().find('.list_scroll li').removeClass('selected');
178
179 options.callback_selected(elSel.attr('key'));
180 }
181
182 return false;
183 }
184
185 if(search_enter_focus)
186 {
187 search_enter_focus = false;
188 return false;
189 }
190 }
191 else if(keyCode == 38) // Up
192 {
193 var elSel = options.obj.parent().find('.list_scroll li.selected');
194
195 if(elSel.length == 0)
196 {
197 options.obj.parent().find('.list_scroll li').first().addClass('selected');
198 }
199
200 if(!elSel.is(':first-child'))
201 {
202 elSel.prev().addClass('selected');
203 elSel.removeClass('selected');
204
205 var container = options.obj.parent().find('.list_scroll');
206 var scrollTo = elSel.prev();
207
208 // Calculating new position of scrollbar
209 var position = scrollTo.offset().top
210 - container.offset().top
211 + container.scrollTop();
212
213 // Setting the value of scrollbar
214 if(!isVisible(scrollTo))
215 container.scrollTop(position);
216 }
217 return false;
218 }
219 else if(keyCode == 40) // Down
220 {
221 var elSel = options.obj.parent().find('.list_scroll li.selected');
222
223 if(elSel.length == 0)
224 {
225 options.obj.parent().find('.list_scroll li').first().addClass('selected');
226 }
227
228 if(!elSel.is(':last-child'))
229 {
230 elSel.next().addClass('selected');
231 elSel.removeClass('selected');
232
233 var container = options.obj.parent().find('.list_scroll');
234 var scrollTo = elSel.next();
235
236 // Calculating new position of scrollbar
237 var position = scrollTo.offset().top
238 - container.offset().top
239 + container.scrollTop();
240
241 // Setting the value of scrollbar
242
243 if(!isVisible(scrollTo))
244 container.scrollTop(position);
245 }
246 return false;
247 }
248
249 options.endLoad=false;
250 options.offset=0;
251
252 //if(is_loading === false)
253 loadMore();
254
255 }).on('keypress', function(event) {
256 if ( event.which == 13 ) {
257 return false;
258 }
259 });
260
261 jQuery(options.obj.parent().find('span.input-group-addon')).on('click', function() {
262 options.endLoad=false;
263 options.offset=0;
264
265 if(is_loading === false)
266 loadMore();
267 });
268
269
270
271 return this;
272 });
273
274 function isVisible($el) {
275 var container = options.obj.parent().find('.list_scroll');
276
277 var winTop = container.scrollTop();
278 var winBottom = winTop + container.height();
279 var elTop = $el.offset().top-container.offset().top+winTop;
280 var elBottom = elTop + $el.height();
281
282 return ((elBottom <= winBottom) && (elTop >= winTop));
283 }
284
285 function loadMore()
286 {
287
288 var search_term_val = options.obj.parent().find('.search_term').val();
289
290 if(options.offset == 0) {
291 //options.obj.parent().find('ul').html('');
292 options.obj.parent().find('ul input:not(:checked)').parent().remove();
293 }
294
295 var param = { offset: options.offset,
296 per_page: options.per_page,
297 curr_id: options.obj.val(),
298 attribute_id: options.attribute_id,
299 attribute_value: options.attribute_value,
300 search_term: search_term_val,
301 language_id: options.language_id,
302 skip_id: options.skip_id,
303 user_check: options.user_check,
304 hide_fields: options.hide_fields,
305 selected: options.selected
306 };
307
308 jQuery.extend( param, options.ajax_param );
309
310 // Assign handlers immediately after making the request,
311 // and remember the jqxhr object for this request
312 if(jqxhr != null)
313 jqxhr.abort();
314
315 options.callback_selected(options.obj.val());
316
317 showSpinner();
318
319 is_loading=true;
320 jqxhr = jQuery.post( options.ajax_url, param, function(data) {
321 hideSpinner();
322 var list_items = options.obj.parent().find('ul');
323
324 if(data.success == false)
325 {
326 options.endLoad=true;
327 if(!options.obj.parent().find('ul li').length)
328 list_items.html('<span class="no_results">'+options.text_no_results+'</span>');
329
330 }
331 else
332 {
333 var selected_values = ','+options.selected+',';
334 jQuery.each( data.results, function( key, row ) {
335 if(key == 'lang_id') return true;
336 if(row.key !='') {
337 if(!options.obj.parent().find('ul li[key="'+row.key+'"]').length)
338 list_items.append("<li key='"+row.key+"'><input value='"+row.key+"' type='checkbox' name='' "+((selected_values.indexOf(','+row.key+',') !=-1) ? "checked='cheecked'" : "")+">"+row.value+"</li>");
339 } else {
340 if(!options.obj.parent().find('ul li').length)
341 list_items.append("<li key='"+row.key+"' class='all'>"+row.value+"</li>");
342 }
343 });
344
345 if(options.offset == 0)
346 options.obj.parent().find('.list_scroll').scrollTop(0);
347
348 if(data.results.length == 0)
349 options.endLoad=true;
350
351 if(options.offset == 0 && data.results.length == 0)
352 list_items.html('<span class="no_results">'+options.text_no_results+'</span>');
353
354 options.offset+=options.per_page;
355 resetElements();
356 }
357
358 options.selected = null;
359 is_loading=false;
360 })
361 .fail(function() {
362 hideSpinner();
363 is_loading=false;
364 });
365 }
366
367 function hideSpinner()
368 {
369 options.obj.parent().find('.loader-spiner').removeClass('fa-spinner');
370 options.obj.parent().find('.loader-spiner').removeClass('fa-spin');
371 options.obj.parent().find('.loader-spiner').addClass('fa-search');
372 }
373
374 function showSpinner()
375 {
376 options.obj.parent().find('.loader-spiner').addClass('fa-spinner');
377 options.obj.parent().find('.loader-spiner').addClass('fa-spin');
378 options.obj.parent().find('.loader-spiner').removeClass('fa-search');
379 }
380
381 function resetElements()
382 {
383 options.obj.parent().find("li *").off();
384
385 options.obj.parent().find("li.all").on('click', function() {
386 options.obj.parent().find("li input").prop('checked', false);
387 options.obj.parent().find('button:first-child').html(jQuery(this).text());
388 options.obj.val('');
389 });
390
391 options.obj.parent().find("li:not(.all)").off().on('click', function(e) {
392 var li = jQuery(this);
393 select_item(li);
394 });
395
396 options.obj.parent().find("li:not(.all) input").off().on('click', function(e) {
397 var li = jQuery(this).parent();
398 select_item(li);
399 });
400
401 }
402
403 function select_item(li) {
404
405 if(li.find('input').prop('checked')) {
406 li.find('input').prop('checked', false);
407 } else {
408 li.find('input').prop('checked', 'checked');
409 }
410
411 var text = '';
412 var values = '';
413
414 options.obj.parent().find("li input:checked").each(function(){
415 if(text !='')
416 text +=',';
417 if(values !='')
418 values +=',';
419 text += jQuery.trim(jQuery(this).parent().text());
420 values += jQuery(this).parent().attr('key');
421 });
422
423 if(text.length > 23) {
424 text = text.substring(0, 20)+'...';
425 }
426
427 if(text == '') {
428 text = options.obj.parent().find("li.all").text();
429 }
430
431 options.obj.parent().find('button:first-child').html(text);
432 options.obj.val(values);
433
434 options.callback_selected(li.attr('key'));
435 }
436
437 function generateHtml()
438 {
439 // hide input element
440 options.obj.css('display', 'none');
441
442 options.obj.before(
443 '<div class="wdk_dropdown_tree">'+
444 // showing value, always visible
445 '<div class="btn-group ">'+
446 '<button class="btn btn-default" type="button">'+
447 options.currValue+'&nbsp;'+
448 '</button>'+
449 '<button type="button" class="btn btn-default dropdown-toggle"> <i class="fa fa-angle-down"></i> </button>'+
450 '</div>'+
451 // hidden part with scroll and search
452 '<div class="list_container color-primary">'+
453 '<div class="list_scroll">'+
454 '<ul class="list_items">'+
455 // '<li key="key_1">test text adr 1</li>'+
456 '</ul>'+
457 '</div>'+
458 // search input and loading indicator
459 '<div class="input-group">'+
460 '<input type="text" class="form-control search_term" placeholder="'+options.text_search+'" aria-describedby="basic-addon2" autocomplete="off">'+
461 '<span class="input-group-addon"><i class="loader-spiner fa fa-spinner fa-spin"></i></span>'+
462 '</div>'+
463 '</div>'+
464 '</div>'
465 );
466 }
467
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489