PluginProbe
WP Directory Kit / 1.2.6
WP Directory Kit v1.2.6
1.5.9 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 All 37 releases
wpdirectorykit / public / js / wdk_treefield / treefield.js

treefield.js in WP Directory Kit 1.2.6, at public/js/wdk_treefield/treefield.js

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