PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.3
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / views / sp-framework / assets / js / spf-plugins.js

spf-plugins.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.3, at admin/views/sp-framework/assets/js/spf-plugins.js

2,458 lines 68.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 *
3 * jQuery Chosen AJAX Autocomplete Library
4 *
5 * https://github.com/meltingice/ajax-chosen
6 * https://github.com/michaelperrin/ajax-chosen
7 *
8 * MIT License
9 *
10 * Customized by SPF
11 *
12 */
13 ;(function ($) {
14 function SP_PCAjaxChosen (element, options) {
15 this.element = $(element)
16 this.options = options
17 this.init()
18 }
19
20 SP_PCAjaxChosen.prototype.init = function () {
21 this.element.chosen(this.options)
22 this.container = this.element.next('.chosen-container')
23 this.search_field = this.container.find('.chosen-search-input')
24 this.is_multiple = this.container.hasClass('chosen-container-multi')
25 this.is_typing = false
26 this.chosenXhr = null
27 this.events()
28 }
29
30 SP_PCAjaxChosen.prototype.events = function () {
31 var _this = this
32
33 this.search_field.on('compositionstart', function () {
34 _this.is_typing = true
35 })
36
37 this.search_field.on('compositionend', function () {
38 _this.is_typing = false
39 _this.update_list()
40 })
41
42 this.search_field.on('keyup', function () {
43 _this.update_list()
44 })
45
46 this.search_field.on('focus', function () {
47 _this.search_field_focused()
48 })
49 }
50
51 SP_PCAjaxChosen.prototype.search_field_focused = function () {
52 this.search_welcome_message()
53 if (this.options.min_length === 0 && this.search_field.val().length === 0) {
54 this.update_list()
55 }
56 }
57
58 SP_PCAjaxChosen.prototype.search_welcome_message = function () {
59 var value = $.trim(this.search_field.val())
60 var results = this.container.find('.chosen-results')
61
62 if (results.children().length === 0 && value.length === 0) {
63 results.html(
64 '<li class="no-results">' +
65 this.options.typing_text.replace(
66 '%s',
67 this.options.min_length - value.length
68 ) +
69 '</li>'
70 )
71 }
72 }
73
74 SP_PCAjaxChosen.prototype.update_list = function () {
75 var _this = this
76
77 this.search_welcome_message()
78
79 if (this.is_typing) {
80 return
81 }
82
83 var value = $.trim(this.search_field.val())
84 var message =
85 value.length < this.options.min_length
86 ? this.options.typing_text.replace(
87 '%s',
88 this.options.min_length - value.length
89 )
90 : this.options.searching_text
91
92 this.container.find('.no-results').text(message)
93
94 if (value === this.search_field.data('prevVal')) {
95 return
96 }
97
98 this.search_field.data('prevVal', value)
99
100 if (this.timer) {
101 clearTimeout(this.timer)
102 }
103
104 if (value.length < this.options.min_length) {
105 return
106 }
107
108 this.timer = setTimeout(function () {
109 if (_this.chosenXhr) {
110 _this.chosenXhr.abort()
111 }
112
113 _this.options.data['term'] = value
114
115 _this.chosenXhr = window.wp.ajax
116 .post('spf-chosen', _this.options.data)
117 .done(function (response) {
118 _this.show_results(response)
119 })
120 .fail(function (response) {
121 _this.container.find('.no-results').text(response.error)
122 })
123 }, this.options.type_delay)
124 }
125
126 SP_PCAjaxChosen.prototype.show_results = function (items) {
127 var _this = this
128
129 if (this.is_typing || items === null) {
130 return
131 }
132
133 if (items.length === 0) {
134 this.element.data().chosen.no_results_clear()
135 this.element.data().chosen.no_results(this.search_field.val())
136 return
137 }
138
139 var selected_values = []
140
141 this.element.find('option').each(function () {
142 if ($(this).is(':selected')) {
143 selected_values.push($(this).val() + '-' + $(this).text())
144 } else {
145 if ($(this).attr('value').length) {
146 $(this).remove()
147 }
148 }
149 })
150
151 $.each(items, function (i, item) {
152 if ($.inArray(item.value + '-' + item.text, selected_values) === -1) {
153 $('<option />')
154 .attr('value', item.value)
155 .html(item.text)
156 .appendTo(_this.element)
157 }
158 })
159
160 var value_before_trigger = this.search_field.val()
161 var width_before_trigger = this.search_field.innerWidth()
162
163 this.element.trigger('chosen:updated')
164
165 if (this.is_multiple) {
166 var $hidden_select = this.element.parent().find('.spf-hidden-select')
167 var $hidden_value = $hidden_select.val() || []
168
169 this.element.SP_PCChosenOrder($hidden_value, true)
170
171 this.search_field.css('width', width_before_trigger)
172 }
173
174 this.search_field.val(value_before_trigger)
175
176 if (this.chosenXhr.done !== null) {
177 this.chosenXhr.done(items)
178 }
179 }
180
181 $.fn.SP_PCAjaxChosen = function (chosenOptions) {
182 return this.each(function () {
183 new SP_PCAjaxChosen(this, chosenOptions)
184 })
185 }
186 })(jQuery) // Chosen Order v1.2.1
187 // This plugin allows you to handle the order of the selection for Chosen multiple <select> dropdowns
188 // Full source at https://github.com/tristanjahier/chosen-order
189 // Copyright (c) 2013 - Tristan Jahier, http://tristan-jahier.fr
190 ;(function () {
191 var $,
192 SP_PCAbstractChosenOrder,
193 _ref,
194 __indexOf =
195 [].indexOf ||
196 function (item) {
197 for (var i = 0, l = this.length; i < l; i++) {
198 if (i in this && this[i] === item) return i
199 }
200 return -1
201 },
202 __hasProp = {}.hasOwnProperty,
203 __extends = function (child, parent) {
204 for (var key in parent) {
205 if (__hasProp.call(parent, key)) child[key] = parent[key]
206 }
207 function ctor () {
208 this.constructor = child
209 }
210 ctor.prototype = parent.prototype
211 child.prototype = new ctor()
212 child.__super__ = parent.prototype
213 return child
214 }
215
216 SP_PCAbstractChosenOrder = (function () {
217 function SP_PCAbstractChosenOrder () {}
218
219 SP_PCAbstractChosenOrder.insertAt = function (node, index, parentNode) {
220 return parentNode.insertBefore(
221 node,
222 parentNode.children[index].nextSibling
223 )
224 }
225
226 SP_PCAbstractChosenOrder.getFlattenedOptionsAndGroups = function (select) {
227 var flattened_options,
228 opt,
229 options,
230 sub_opt,
231 sub_options,
232 _i,
233 _j,
234 _len,
235 _len1
236 options = Array.prototype.filter.call(select.childNodes, function (o) {
237 var _ref
238 return (
239 (_ref = o.nodeName.toUpperCase()) === 'OPTION' || _ref === 'OPTGROUP'
240 )
241 })
242 flattened_options = []
243 for (_i = 0, _len = options.length; _i < _len; _i++) {
244 opt = options[_i]
245 flattened_options.push(opt)
246 if (opt.nodeName.toUpperCase() === 'OPTGROUP') {
247 sub_options = Array.prototype.filter.call(opt.childNodes, function (
248 o
249 ) {
250 return o.nodeName.toUpperCase() === 'OPTION'
251 })
252 for (_j = 0, _len1 = sub_options.length; _j < _len1; _j++) {
253 sub_opt = sub_options[_j]
254 flattened_options.push(sub_opt)
255 }
256 }
257 }
258 return flattened_options
259 }
260
261 SP_PCAbstractChosenOrder.isValidMultipleSelectElement = function (element) {
262 return (
263 element !== null &&
264 typeof element !== 'undefined' &&
265 element.nodeName === 'SELECT' &&
266 element.multiple
267 )
268 }
269
270 SP_PCAbstractChosenOrder.getChosenUIContainer = function (select) {
271 if (select.id !== '') {
272 return document.getElementById(select.id.replace(/-/g, '_') + '_chosen')
273 } else {
274 return this.searchChosenUIContainer(select)
275 }
276 }
277
278 SP_PCAbstractChosenOrder.isChosenified = function (select) {
279 return this.getChosenUIContainer(select) != null
280 }
281
282 SP_PCAbstractChosenOrder.forceSelection = function (select, selection) {
283 var i, opt, options, _ref
284 options = this.getFlattenedOptionsAndGroups(select)
285 i = 0
286 while (i < options.length) {
287 opt = options[i]
288 if (
289 ((_ref = opt.getAttribute('value')),
290 __indexOf.call(selection, _ref) >= 0)
291 ) {
292 opt.selected = true
293 opt.setAttribute('selected', '')
294 } else {
295 opt.selected = false
296 opt.removeAttribute('selected')
297 }
298 i++
299 }
300 return this.triggerEvent(select, 'chosen:updated')
301 }
302
303 SP_PCAbstractChosenOrder.SP_PCChosenOrder = function (
304 select,
305 order,
306 force
307 ) {
308 var chosen_choices,
309 chosen_options,
310 chosen_ui,
311 i,
312 j,
313 opt,
314 opt_val,
315 option,
316 options,
317 rel,
318 relAttributeName,
319 _i,
320 _j,
321 _len,
322 _len1,
323 _results
324 if (this.getDOMElement != null) {
325 select = this.getDOMElement(select)
326 }
327 if (!this.isValidMultipleSelectElement(select)) {
328 return
329 }
330 chosen_ui = this.getChosenUIContainer(select)
331 if (chosen_ui == null) {
332 return
333 }
334 if (order instanceof Array) {
335 order = order.map(Function.prototype.call, String.prototype.trim)
336 options = this.getFlattenedOptionsAndGroups(select)
337 if (force != null && force === true) {
338 this.forceSelection(select, order)
339 }
340 _results = []
341 for (i = _i = 0, _len = order.length; _i < _len; i = ++_i) {
342 opt_val = order[i]
343 rel = null
344 for (j = _j = 0, _len1 = options.length; _j < _len1; j = ++_j) {
345 opt = options[j]
346 if (opt.value === opt_val) {
347 rel = j
348 }
349 }
350 chosen_options = chosen_ui.querySelectorAll('.search-choice')
351 relAttributeName = this.relAttributeName
352 option = Array.prototype.filter.call(chosen_options, function (o) {
353 return (
354 o.querySelector(
355 'a.search-choice-close[' + relAttributeName + '="' + rel + '"]'
356 ) != null
357 )
358 })[0]
359 if (option == null) {
360 continue
361 }
362 chosen_choices = chosen_ui.querySelector('ul.chosen-choices')
363 _results.push(
364 this.insertAt(
365 option,
366 i,
367 chosen_ui.querySelector('ul.chosen-choices')
368 )
369 )
370 }
371 return _results
372 } else {
373 return
374 }
375 }
376
377 return SP_PCAbstractChosenOrder
378 })()
379
380 $ = jQuery
381
382 $.fn.extend({
383 SP_PCChosenOrder: function (order, force) {
384 return _SP_PCChosenOrder.SP_PCChosenOrder(this, order, force)
385 }
386 })
387
388 this._SP_PCChosenOrder = (function (_super) {
389 __extends(_SP_PCChosenOrder, _super)
390
391 function _SP_PCChosenOrder () {
392 _ref = _SP_PCChosenOrder.__super__.constructor.apply(this, arguments)
393 return _ref
394 }
395
396 _SP_PCChosenOrder.relAttributeName = 'data-option-array-index'
397
398 _SP_PCChosenOrder.isjQueryObject = function (obj) {
399 return (
400 typeof jQuery !== 'undefined' &&
401 jQuery !== null &&
402 obj instanceof jQuery
403 )
404 }
405
406 _SP_PCChosenOrder.getDOMElement = function (element) {
407 if (this.isjQueryObject(element)) {
408 return element.get(0)
409 } else {
410 return element
411 }
412 }
413
414 _SP_PCChosenOrder.searchChosenUIContainer = function (element) {
415 if ($(element).data('chosen') != null) {
416 return $(element).data('chosen').container[0]
417 } else {
418 return $(element)
419 .next('.chosen-container.chosen-container-multi')
420 .get(0)
421 }
422 }
423
424 _SP_PCChosenOrder.triggerEvent = function (target, event_name) {
425 return $(target).trigger(event_name)
426 }
427
428 return _SP_PCChosenOrder
429 })(SP_PCAbstractChosenOrder)
430 }.call(this))
431 ;(function () {
432 var $,
433 AbstractChosen,
434 Chosen,
435 SelectParser,
436 bind = function (fn, me) {
437 return function () {
438 return fn.apply(me, arguments)
439 }
440 },
441 extend = function (child, parent) {
442 for (var key in parent) {
443 if (hasProp.call(parent, key)) child[key] = parent[key]
444 }
445 function ctor () {
446 this.constructor = child
447 }
448 ctor.prototype = parent.prototype
449 child.prototype = new ctor()
450 child.__super__ = parent.prototype
451 return child
452 },
453 hasProp = {}.hasOwnProperty
454
455 SelectParser = (function () {
456 function SelectParser () {
457 this.options_index = 0
458 this.parsed = []
459 }
460
461 SelectParser.prototype.add_node = function (child) {
462 if (child.nodeName.toUpperCase() === 'OPTGROUP') {
463 return this.add_group(child)
464 } else {
465 return this.add_option(child)
466 }
467 }
468
469 SelectParser.prototype.add_group = function (group) {
470 var group_position, i, len, option, ref, results1
471 group_position = this.parsed.length
472 this.parsed.push({
473 array_index: group_position,
474 group: true,
475 label: group.label,
476 title: group.title ? group.title : void 0,
477 children: 0,
478 disabled: group.disabled,
479 classes: group.className
480 })
481 ref = group.childNodes
482 results1 = []
483 for (i = 0, len = ref.length; i < len; i++) {
484 option = ref[i]
485 results1.push(this.add_option(option, group_position, group.disabled))
486 }
487 return results1
488 }
489
490 SelectParser.prototype.add_option = function (
491 option,
492 group_position,
493 group_disabled
494 ) {
495 if (option.nodeName.toUpperCase() === 'OPTION') {
496 if (option.text !== '') {
497 if (group_position != null) {
498 this.parsed[group_position].children += 1
499 }
500 this.parsed.push({
501 array_index: this.parsed.length,
502 options_index: this.options_index,
503 value: option.value,
504 text: option.text,
505 html: option.innerHTML,
506 title: option.title ? option.title : void 0,
507 selected: option.selected,
508 disabled:
509 group_disabled === true ? group_disabled : option.disabled,
510 group_array_index: group_position,
511 group_label:
512 group_position != null ? this.parsed[group_position].label : null,
513 classes: option.className,
514 style: option.style.cssText
515 })
516 } else {
517 this.parsed.push({
518 array_index: this.parsed.length,
519 options_index: this.options_index,
520 empty: true
521 })
522 }
523 return (this.options_index += 1)
524 }
525 }
526
527 return SelectParser
528 })()
529
530 SelectParser.select_to_array = function (select) {
531 var child, i, len, parser, ref
532 parser = new SelectParser()
533 ref = select.childNodes
534 for (i = 0, len = ref.length; i < len; i++) {
535 child = ref[i]
536 parser.add_node(child)
537 }
538 return parser.parsed
539 }
540
541 AbstractChosen = (function () {
542 function AbstractChosen (form_field, options1) {
543 this.form_field = form_field
544 this.options = options1 != null ? options1 : {}
545 this.label_click_handler = bind(this.label_click_handler, this)
546 if (!AbstractChosen.browser_is_supported()) {
547 return
548 }
549 this.is_multiple = this.form_field.multiple
550 this.set_default_text()
551 this.set_default_values()
552 this.setup()
553 this.set_up_html()
554 this.register_observers()
555 this.on_ready()
556 }
557
558 AbstractChosen.prototype.set_default_values = function () {
559 this.click_test_action = (function (_this) {
560 return function (evt) {
561 return _this.test_active_click(evt)
562 }
563 })(this)
564 this.activate_action = (function (_this) {
565 return function (evt) {
566 return _this.activate_field(evt)
567 }
568 })(this)
569 this.active_field = false
570 this.mouse_on_container = false
571 this.results_showing = false
572 this.result_highlighted = null
573 this.is_rtl =
574 this.options.rtl || /\bchosen-rtl\b/.test(this.form_field.className)
575 this.allow_single_deselect =
576 this.options.allow_single_deselect != null &&
577 this.form_field.options[0] != null &&
578 this.form_field.options[0].text === ''
579 ? this.options.allow_single_deselect
580 : false
581 this.disable_search_threshold = this.options.disable_search_threshold || 0
582 this.disable_search = this.options.disable_search || false
583 this.enable_split_word_search =
584 this.options.enable_split_word_search != null
585 ? this.options.enable_split_word_search
586 : true
587 this.group_search =
588 this.options.group_search != null ? this.options.group_search : true
589 this.search_contains = this.options.search_contains || false
590 this.single_backstroke_delete =
591 this.options.single_backstroke_delete != null
592 ? this.options.single_backstroke_delete
593 : true
594 this.max_selected_options = this.options.max_selected_options || Infinity
595 this.inherit_select_classes = this.options.inherit_select_classes || false
596 this.display_selected_options =
597 this.options.display_selected_options != null
598 ? this.options.display_selected_options
599 : true
600 this.display_disabled_options =
601 this.options.display_disabled_options != null
602 ? this.options.display_disabled_options
603 : true
604 this.include_group_label_in_selected =
605 this.options.include_group_label_in_selected || false
606 this.max_shown_results =
607 this.options.max_shown_results || Number.POSITIVE_INFINITY
608 this.case_sensitive_search = this.options.case_sensitive_search || false
609 return (this.hide_results_on_select =
610 this.options.hide_results_on_select != null
611 ? this.options.hide_results_on_select
612 : true)
613 }
614
615 AbstractChosen.prototype.set_default_text = function () {
616 if (this.form_field.getAttribute('data-placeholder')) {
617 this.default_text = this.form_field.getAttribute('data-placeholder')
618 } else if (this.is_multiple) {
619 this.default_text =
620 this.options.placeholder_text_multiple ||
621 this.options.placeholder_text ||
622 AbstractChosen.default_multiple_text
623 } else {
624 this.default_text =
625 this.options.placeholder_text_single ||
626 this.options.placeholder_text ||
627 AbstractChosen.default_single_text
628 }
629 this.default_text = this.escape_html(this.default_text)
630 return (this.results_none_found =
631 this.form_field.getAttribute('data-no_results_text') ||
632 this.options.no_results_text ||
633 AbstractChosen.default_no_result_text)
634 }
635
636 AbstractChosen.prototype.choice_label = function (item) {
637 if (this.include_group_label_in_selected && item.group_label != null) {
638 return (
639 "<b class='group-name'>" +
640 this.escape_html(item.group_label) +
641 '</b>' +
642 item.html
643 )
644 } else {
645 return item.html
646 }
647 }
648
649 AbstractChosen.prototype.mouse_enter = function () {
650 return (this.mouse_on_container = true)
651 }
652
653 AbstractChosen.prototype.mouse_leave = function () {
654 return (this.mouse_on_container = false)
655 }
656
657 AbstractChosen.prototype.input_focus = function (evt) {
658 if (this.is_multiple) {
659 if (!this.active_field) {
660 return setTimeout(
661 (function (_this) {
662 return function () {
663 return _this.container_mousedown()
664 }
665 })(this),
666 50
667 )
668 }
669 } else {
670 if (!this.active_field) {
671 return this.activate_field()
672 }
673 }
674 }
675
676 AbstractChosen.prototype.input_blur = function (evt) {
677 if (!this.mouse_on_container) {
678 this.active_field = false
679 return setTimeout(
680 (function (_this) {
681 return function () {
682 return _this.blur_test()
683 }
684 })(this),
685 100
686 )
687 }
688 }
689
690 AbstractChosen.prototype.label_click_handler = function (evt) {
691 if (this.is_multiple) {
692 return this.container_mousedown(evt)
693 } else {
694 return this.activate_field()
695 }
696 }
697
698 AbstractChosen.prototype.results_option_build = function (options) {
699 var content, data, data_content, i, len, ref, shown_results
700 content = ''
701 shown_results = 0
702 ref = this.results_data
703 for (i = 0, len = ref.length; i < len; i++) {
704 data = ref[i]
705 data_content = ''
706 if (data.group) {
707 data_content = this.result_add_group(data)
708 } else {
709 data_content = this.result_add_option(data)
710 }
711 if (data_content !== '') {
712 shown_results++
713 content += data_content
714 }
715 if (options != null ? options.first : void 0) {
716 if (data.selected && this.is_multiple) {
717 this.choice_build(data)
718 } else if (data.selected && !this.is_multiple) {
719 this.single_set_selected_text(this.choice_label(data))
720 }
721 }
722 if (shown_results >= this.max_shown_results) {
723 break
724 }
725 }
726 return content
727 }
728
729 AbstractChosen.prototype.result_add_option = function (option) {
730 var classes, option_el
731 if (!option.search_match) {
732 return ''
733 }
734 if (!this.include_option_in_results(option)) {
735 return ''
736 }
737 classes = []
738 if (!option.disabled && !(option.selected && this.is_multiple)) {
739 classes.push('active-result')
740 }
741 if (option.disabled && !(option.selected && this.is_multiple)) {
742 classes.push('disabled-result')
743 }
744 if (option.selected) {
745 classes.push('result-selected')
746 }
747 if (option.group_array_index != null) {
748 classes.push('group-option')
749 }
750 if (option.classes !== '') {
751 classes.push(option.classes)
752 }
753 option_el = document.createElement('li')
754 option_el.className = classes.join(' ')
755 if (option.style) {
756 option_el.style.cssText = option.style
757 }
758 option_el.setAttribute('data-option-array-index', option.array_index)
759 option_el.innerHTML = option.highlighted_html || option.html
760 if (option.title) {
761 option_el.title = option.title
762 }
763 return this.outerHTML(option_el)
764 }
765
766 AbstractChosen.prototype.result_add_group = function (group) {
767 var classes, group_el
768 if (!(group.search_match || group.group_match)) {
769 return ''
770 }
771 if (!(group.active_options > 0)) {
772 return ''
773 }
774 classes = []
775 classes.push('group-result')
776 if (group.classes) {
777 classes.push(group.classes)
778 }
779 group_el = document.createElement('li')
780 group_el.className = classes.join(' ')
781 group_el.innerHTML =
782 group.highlighted_html || this.escape_html(group.label)
783 if (group.title) {
784 group_el.title = group.title
785 }
786 return this.outerHTML(group_el)
787 }
788
789 AbstractChosen.prototype.results_update_field = function () {
790 this.set_default_text()
791 if (!this.is_multiple) {
792 this.results_reset_cleanup()
793 }
794 this.result_clear_highlight()
795 this.results_build()
796 if (this.results_showing) {
797 return this.winnow_results()
798 }
799 }
800
801 AbstractChosen.prototype.reset_single_select_options = function () {
802 var i, len, ref, result, results1
803 ref = this.results_data
804 results1 = []
805 for (i = 0, len = ref.length; i < len; i++) {
806 result = ref[i]
807 if (result.selected) {
808 results1.push((result.selected = false))
809 } else {
810 results1.push(void 0)
811 }
812 }
813 return results1
814 }
815
816 AbstractChosen.prototype.results_toggle = function () {
817 if (this.results_showing) {
818 return this.results_hide()
819 } else {
820 return this.results_show()
821 }
822 }
823
824 AbstractChosen.prototype.results_search = function (evt) {
825 if (this.results_showing) {
826 return this.winnow_results()
827 } else {
828 return this.results_show()
829 }
830 }
831
832 AbstractChosen.prototype.winnow_results = function (options) {
833 var escapedQuery,
834 fix,
835 i,
836 len,
837 option,
838 prefix,
839 query,
840 ref,
841 regex,
842 results,
843 results_group,
844 search_match,
845 startpos,
846 suffix,
847 text
848 this.no_results_clear()
849 results = 0
850 query = this.get_search_text()
851 escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
852 regex = this.get_search_regex(escapedQuery)
853 ref = this.results_data
854 for (i = 0, len = ref.length; i < len; i++) {
855 option = ref[i]
856 option.search_match = false
857 results_group = null
858 search_match = null
859 option.highlighted_html = ''
860 if (this.include_option_in_results(option)) {
861 if (option.group) {
862 option.group_match = false
863 option.active_options = 0
864 }
865 if (
866 option.group_array_index != null &&
867 this.results_data[option.group_array_index]
868 ) {
869 results_group = this.results_data[option.group_array_index]
870 if (
871 results_group.active_options === 0 &&
872 results_group.search_match
873 ) {
874 results += 1
875 }
876 results_group.active_options += 1
877 }
878 text = option.group ? option.label : option.text
879 if (!(option.group && !this.group_search)) {
880 search_match = this.search_string_match(text, regex)
881 option.search_match = search_match != null
882 if (option.search_match && !option.group) {
883 results += 1
884 }
885 if (option.search_match) {
886 if (query.length) {
887 startpos = search_match.index
888 prefix = text.slice(0, startpos)
889 fix = text.slice(startpos, startpos + query.length)
890 suffix = text.slice(startpos + query.length)
891 option.highlighted_html =
892 this.escape_html(prefix) +
893 '<em>' +
894 this.escape_html(fix) +
895 '</em>' +
896 this.escape_html(suffix)
897 }
898 if (results_group != null) {
899 results_group.group_match = true
900 }
901 } else if (
902 option.group_array_index != null &&
903 this.results_data[option.group_array_index].search_match
904 ) {
905 option.search_match = true
906 }
907 }
908 }
909 }
910 this.result_clear_highlight()
911 if (results < 1 && query.length) {
912 this.update_results_content('')
913 return this.no_results(query)
914 } else {
915 this.update_results_content(this.results_option_build())
916 if (!(options != null ? options.skip_highlight : void 0)) {
917 return this.winnow_results_set_highlight()
918 }
919 }
920 }
921
922 AbstractChosen.prototype.get_search_regex = function (
923 escaped_search_string
924 ) {
925 var regex_flag, regex_string
926 regex_string = this.search_contains
927 ? escaped_search_string
928 : '(^|\\s|\\b)' + escaped_search_string + '[^\\s]*'
929 if (!(this.enable_split_word_search || this.search_contains)) {
930 regex_string = '^' + regex_string
931 }
932 regex_flag = this.case_sensitive_search ? '' : 'i'
933 return new RegExp(regex_string, regex_flag)
934 }
935
936 AbstractChosen.prototype.search_string_match = function (
937 search_string,
938 regex
939 ) {
940 var match
941 match = regex.exec(search_string)
942 if (!this.search_contains && (match != null ? match[1] : void 0)) {
943 match.index += 1
944 }
945 return match
946 }
947
948 AbstractChosen.prototype.choices_count = function () {
949 var i, len, option, ref
950 if (this.selected_option_count != null) {
951 return this.selected_option_count
952 }
953 this.selected_option_count = 0
954 ref = this.form_field.options
955 for (i = 0, len = ref.length; i < len; i++) {
956 option = ref[i]
957 if (option.selected) {
958 this.selected_option_count += 1
959 }
960 }
961 return this.selected_option_count
962 }
963
964 AbstractChosen.prototype.choices_click = function (evt) {
965 evt.preventDefault()
966 this.activate_field()
967 if (!(this.results_showing || this.is_disabled)) {
968 return this.results_show()
969 }
970 }
971
972 AbstractChosen.prototype.keydown_checker = function (evt) {
973 var ref, stroke
974 stroke = (ref = evt.which) != null ? ref : evt.keyCode
975 this.search_field_scale()
976 if (stroke !== 8 && this.pending_backstroke) {
977 this.clear_backstroke()
978 }
979 switch (stroke) {
980 case 8:
981 this.backstroke_length = this.get_search_field_value().length
982 break
983 case 9:
984 if (this.results_showing && !this.is_multiple) {
985 this.result_select(evt)
986 }
987 this.mouse_on_container = false
988 break
989 case 13:
990 if (this.results_showing) {
991 evt.preventDefault()
992 }
993 break
994 case 27:
995 if (this.results_showing) {
996 evt.preventDefault()
997 }
998 break
999 case 32:
1000 if (this.disable_search) {
1001 evt.preventDefault()
1002 }
1003 break
1004 case 38:
1005 evt.preventDefault()
1006 this.keyup_arrow()
1007 break
1008 case 40:
1009 evt.preventDefault()
1010 this.keydown_arrow()
1011 break
1012 }
1013 }
1014
1015 AbstractChosen.prototype.keyup_checker = function (evt) {
1016 var ref, stroke
1017 stroke = (ref = evt.which) != null ? ref : evt.keyCode
1018 this.search_field_scale()
1019 switch (stroke) {
1020 case 8:
1021 if (
1022 this.is_multiple &&
1023 this.backstroke_length < 1 &&
1024 this.choices_count() > 0
1025 ) {
1026 this.keydown_backstroke()
1027 } else if (!this.pending_backstroke) {
1028 this.result_clear_highlight()
1029 this.results_search()
1030 }
1031 break
1032 case 13:
1033 evt.preventDefault()
1034 if (this.results_showing) {
1035 this.result_select(evt)
1036 }
1037 break
1038 case 27:
1039 if (this.results_showing) {
1040 this.results_hide()
1041 }
1042 break
1043 case 9:
1044 case 16:
1045 case 17:
1046 case 18:
1047 case 38:
1048 case 40:
1049 case 91:
1050 break
1051 default:
1052 this.results_search()
1053 break
1054 }
1055 }
1056
1057 AbstractChosen.prototype.clipboard_event_checker = function (evt) {
1058 if (this.is_disabled) {
1059 return
1060 }
1061 return setTimeout(
1062 (function (_this) {
1063 return function () {
1064 return _this.results_search()
1065 }
1066 })(this),
1067 50
1068 )
1069 }
1070
1071 AbstractChosen.prototype.container_width = function () {
1072 if (this.options.width != null) {
1073 return this.options.width
1074 } else {
1075 return this.form_field.offsetWidth + 'px'
1076 }
1077 }
1078
1079 AbstractChosen.prototype.include_option_in_results = function (option) {
1080 if (
1081 this.is_multiple &&
1082 !this.display_selected_options && option.selected
1083 ) {
1084 return false
1085 }
1086 if (!this.display_disabled_options && option.disabled) {
1087 return false
1088 }
1089 if (option.empty) {
1090 return false
1091 }
1092 return true
1093 }
1094
1095 AbstractChosen.prototype.search_results_touchstart = function (evt) {
1096 this.touch_started = true
1097 return this.search_results_mouseover(evt)
1098 }
1099
1100 AbstractChosen.prototype.search_results_touchmove = function (evt) {
1101 this.touch_started = false
1102 return this.search_results_mouseout(evt)
1103 }
1104
1105 AbstractChosen.prototype.search_results_touchend = function (evt) {
1106 if (this.touch_started) {
1107 return this.search_results_mouseup(evt)
1108 }
1109 }
1110
1111 AbstractChosen.prototype.outerHTML = function (element) {
1112 var tmp
1113 if (element.outerHTML) {
1114 return element.outerHTML
1115 }
1116 tmp = document.createElement('div')
1117 tmp.appendChild(element)
1118 return tmp.innerHTML
1119 }
1120
1121 AbstractChosen.prototype.get_single_html = function () {
1122 return (
1123 '<a class="chosen-single chosen-default">\n <span>' +
1124 this.default_text +
1125 '</span>\n <div><b></b></div>\n</a>\n<div class="chosen-drop">\n <div class="chosen-search">\n <input class="chosen-search-input" type="text" autocomplete="off" />\n </div>\n <ul class="chosen-results"></ul>\n</div>'
1126 )
1127 }
1128
1129 AbstractChosen.prototype.get_multi_html = function () {
1130 return (
1131 '<ul class="chosen-choices">\n <li class="search-field">\n <input class="chosen-search-input" type="text" autocomplete="off" value="' +
1132 this.default_text +
1133 '" />\n </li>\n</ul>\n<div class="chosen-drop">\n <ul class="chosen-results"></ul>\n</div>'
1134 )
1135 }
1136
1137 AbstractChosen.prototype.get_no_results_html = function (terms) {
1138 return (
1139 '<li class="no-results">\n ' +
1140 this.results_none_found +
1141 ' <span>' +
1142 this.escape_html(terms) +
1143 '</span>\n</li>'
1144 )
1145 }
1146
1147 AbstractChosen.browser_is_supported = function () {
1148 if ('Microsoft Internet Explorer' === window.navigator.appName) {
1149 return document.documentMode >= 8
1150 }
1151 if (
1152 /iP(od|hone)/i.test(window.navigator.userAgent) ||
1153 /IEMobile/i.test(window.navigator.userAgent) ||
1154 /Windows Phone/i.test(window.navigator.userAgent) ||
1155 /BlackBerry/i.test(window.navigator.userAgent) ||
1156 /BB10/i.test(window.navigator.userAgent) ||
1157 /Android.*Mobile/i.test(window.navigator.userAgent)
1158 ) {
1159 return false
1160 }
1161 return true
1162 }
1163
1164 AbstractChosen.default_multiple_text = 'Select Some Options'
1165
1166 AbstractChosen.default_single_text = 'Select an Option'
1167
1168 AbstractChosen.default_no_result_text = 'No results match'
1169
1170 return AbstractChosen
1171 })()
1172
1173 $ = jQuery
1174
1175 $.fn.extend({
1176 chosen: function (options) {
1177 if (!AbstractChosen.browser_is_supported()) {
1178 return this
1179 }
1180 return this.each(function (input_field) {
1181 var $this, chosen
1182 $this = $(this)
1183 chosen = $this.data('chosen')
1184 if (options === 'destroy') {
1185 if (chosen instanceof Chosen) {
1186 chosen.destroy()
1187 }
1188 return
1189 }
1190 if (!(chosen instanceof Chosen)) {
1191 $this.data('chosen', new Chosen(this, options))
1192 }
1193 })
1194 }
1195 })
1196
1197 Chosen = (function (superClass) {
1198 extend(Chosen, superClass)
1199
1200 function Chosen () {
1201 return Chosen.__super__.constructor.apply(this, arguments)
1202 }
1203
1204 Chosen.prototype.setup = function () {
1205 this.form_field_jq = $(this.form_field)
1206 return (this.current_selectedIndex = this.form_field.selectedIndex)
1207 }
1208
1209 Chosen.prototype.set_up_html = function () {
1210 var container_classes, container_props
1211 container_classes = ['chosen-container']
1212 container_classes.push(
1213 'chosen-container-' + (this.is_multiple ? 'multi' : 'single')
1214 )
1215 if (this.inherit_select_classes && this.form_field.className) {
1216 container_classes.push(this.form_field.className)
1217 }
1218 if (this.is_rtl) {
1219 container_classes.push('chosen-rtl')
1220 }
1221 container_props = {
1222 class: container_classes.join(' '),
1223 title: this.form_field.title
1224 }
1225 if (this.form_field.id.length) {
1226 container_props.id =
1227 this.form_field.id.replace(/[^\w]/g, '_') + '_chosen'
1228 }
1229 this.container = $('<div />', container_props)
1230 this.container.width(this.container_width())
1231 if (this.is_multiple) {
1232 this.container.html(this.get_multi_html())
1233 } else {
1234 this.container.html(this.get_single_html())
1235 }
1236 this.form_field_jq.hide().after(this.container)
1237 this.dropdown = this.container.find('div.chosen-drop').first()
1238 this.search_field = this.container.find('input').first()
1239 this.search_results = this.container.find('ul.chosen-results').first()
1240 this.search_field_scale()
1241 this.search_no_results = this.container.find('li.no-results').first()
1242 if (this.is_multiple) {
1243 this.search_choices = this.container.find('ul.chosen-choices').first()
1244 this.search_container = this.container.find('li.search-field').first()
1245 } else {
1246 this.search_container = this.container.find('div.chosen-search').first()
1247 this.selected_item = this.container.find('.chosen-single').first()
1248 }
1249 this.results_build()
1250 this.set_tab_index()
1251 return this.set_label_behavior()
1252 }
1253
1254 Chosen.prototype.on_ready = function () {
1255 return this.form_field_jq.trigger('chosen:ready', {
1256 chosen: this
1257 })
1258 }
1259
1260 Chosen.prototype.register_observers = function () {
1261 this.container.on(
1262 'touchstart.chosen',
1263 (function (_this) {
1264 return function (evt) {
1265 _this.container_mousedown(evt)
1266 }
1267 })(this)
1268 )
1269 this.container.on(
1270 'touchend.chosen',
1271 (function (_this) {
1272 return function (evt) {
1273 _this.container_mouseup(evt)
1274 }
1275 })(this)
1276 )
1277 this.container.on(
1278 'mousedown.chosen',
1279 (function (_this) {
1280 return function (evt) {
1281 _this.container_mousedown(evt)
1282 }
1283 })(this)
1284 )
1285 this.container.on(
1286 'mouseup.chosen',
1287 (function (_this) {
1288 return function (evt) {
1289 _this.container_mouseup(evt)
1290 }
1291 })(this)
1292 )
1293 this.container.on(
1294 'mouseenter.chosen',
1295 (function (_this) {
1296 return function (evt) {
1297 _this.mouse_enter(evt)
1298 }
1299 })(this)
1300 )
1301 this.container.on(
1302 'mouseleave.chosen',
1303 (function (_this) {
1304 return function (evt) {
1305 _this.mouse_leave(evt)
1306 }
1307 })(this)
1308 )
1309 this.search_results.on(
1310 'mouseup.chosen',
1311 (function (_this) {
1312 return function (evt) {
1313 _this.search_results_mouseup(evt)
1314 }
1315 })(this)
1316 )
1317 this.search_results.on(
1318 'mouseover.chosen',
1319 (function (_this) {
1320 return function (evt) {
1321 _this.search_results_mouseover(evt)
1322 }
1323 })(this)
1324 )
1325 this.search_results.on(
1326 'mouseout.chosen',
1327 (function (_this) {
1328 return function (evt) {
1329 _this.search_results_mouseout(evt)
1330 }
1331 })(this)
1332 )
1333 this.search_results.on(
1334 'mousewheel.chosen DOMMouseScroll.chosen',
1335 (function (_this) {
1336 return function (evt) {
1337 _this.search_results_mousewheel(evt)
1338 }
1339 })(this)
1340 )
1341 this.search_results.on(
1342 'touchstart.chosen',
1343 (function (_this) {
1344 return function (evt) {
1345 _this.search_results_touchstart(evt)
1346 }
1347 })(this)
1348 )
1349 this.search_results.on(
1350 'touchmove.chosen',
1351 (function (_this) {
1352 return function (evt) {
1353 _this.search_results_touchmove(evt)
1354 }
1355 })(this)
1356 )
1357 this.search_results.on(
1358 'touchend.chosen',
1359 (function (_this) {
1360 return function (evt) {
1361 _this.search_results_touchend(evt)
1362 }
1363 })(this)
1364 )
1365 this.form_field_jq.on(
1366 'chosen:updated.chosen',
1367 (function (_this) {
1368 return function (evt) {
1369 _this.results_update_field(evt)
1370 }
1371 })(this)
1372 )
1373 this.form_field_jq.on(
1374 'chosen:activate.chosen',
1375 (function (_this) {
1376 return function (evt) {
1377 _this.activate_field(evt)
1378 }
1379 })(this)
1380 )
1381 this.form_field_jq.on(
1382 'chosen:open.chosen',
1383 (function (_this) {
1384 return function (evt) {
1385 _this.container_mousedown(evt)
1386 }
1387 })(this)
1388 )
1389 this.form_field_jq.on(
1390 'chosen:close.chosen',
1391 (function (_this) {
1392 return function (evt) {
1393 _this.close_field(evt)
1394 }
1395 })(this)
1396 )
1397 this.search_field.on(
1398 'blur.chosen',
1399 (function (_this) {
1400 return function (evt) {
1401 _this.input_blur(evt)
1402 }
1403 })(this)
1404 )
1405 this.search_field.on(
1406 'keyup.chosen',
1407 (function (_this) {
1408 return function (evt) {
1409 _this.keyup_checker(evt)
1410 }
1411 })(this)
1412 )
1413 this.search_field.on(
1414 'keydown.chosen',
1415 (function (_this) {
1416 return function (evt) {
1417 _this.keydown_checker(evt)
1418 }
1419 })(this)
1420 )
1421 this.search_field.on(
1422 'focus.chosen',
1423 (function (_this) {
1424 return function (evt) {
1425 _this.input_focus(evt)
1426 }
1427 })(this)
1428 )
1429 this.search_field.on(
1430 'cut.chosen',
1431 (function (_this) {
1432 return function (evt) {
1433 _this.clipboard_event_checker(evt)
1434 }
1435 })(this)
1436 )
1437 this.search_field.on(
1438 'paste.chosen',
1439 (function (_this) {
1440 return function (evt) {
1441 _this.clipboard_event_checker(evt)
1442 }
1443 })(this)
1444 )
1445 if (this.is_multiple) {
1446 return this.search_choices.on(
1447 'click.chosen',
1448 (function (_this) {
1449 return function (evt) {
1450 _this.choices_click(evt)
1451 }
1452 })(this)
1453 )
1454 } else {
1455 return this.container.on('click.chosen', function (evt) {
1456 evt.preventDefault()
1457 })
1458 }
1459 }
1460
1461 Chosen.prototype.destroy = function () {
1462 $(this.container[0].ownerDocument).off(
1463 'click.chosen',
1464 this.click_test_action
1465 )
1466 if (this.form_field_label.length > 0) {
1467 this.form_field_label.off('click.chosen')
1468 }
1469 if (this.search_field[0].tabIndex) {
1470 this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex
1471 }
1472 this.container.remove()
1473 this.form_field_jq.removeData('chosen')
1474 return this.form_field_jq.show()
1475 }
1476
1477 Chosen.prototype.search_field_disabled = function () {
1478 this.is_disabled =
1479 this.form_field.disabled ||
1480 this.form_field_jq.parents('fieldset').is(':disabled')
1481 this.container.toggleClass('chosen-disabled', this.is_disabled)
1482 this.search_field[0].disabled = this.is_disabled
1483 if (!this.is_multiple) {
1484 this.selected_item.off('focus.chosen', this.activate_field)
1485 }
1486 if (this.is_disabled) {
1487 return this.close_field()
1488 } else if (!this.is_multiple) {
1489 return this.selected_item.on('focus.chosen', this.activate_field)
1490 }
1491 }
1492
1493 Chosen.prototype.container_mousedown = function (evt) {
1494 var ref
1495 if (this.is_disabled) {
1496 return
1497 }
1498 if (
1499 evt &&
1500 ((ref = evt.type) === 'mousedown' || ref === 'touchstart') &&
1501 !this.results_showing
1502 ) {
1503 evt.preventDefault()
1504 }
1505 if (!(evt != null && $(evt.target).hasClass('search-choice-close'))) {
1506 if (!this.active_field) {
1507 if (this.is_multiple) {
1508 this.search_field.val('')
1509 }
1510 $(this.container[0].ownerDocument).on(
1511 'click.chosen',
1512 this.click_test_action
1513 )
1514 this.results_show()
1515 } else if (
1516 !this.is_multiple &&
1517 evt &&
1518 ($(evt.target)[0] === this.selected_item[0] ||
1519 $(evt.target).parents('a.chosen-single').length)
1520 ) {
1521 evt.preventDefault()
1522 this.results_toggle()
1523 }
1524 return this.activate_field()
1525 }
1526 }
1527
1528 Chosen.prototype.container_mouseup = function (evt) {
1529 if (evt.target.nodeName === 'ABBR' && !this.is_disabled) {
1530 return this.results_reset(evt)
1531 }
1532 }
1533
1534 Chosen.prototype.search_results_mousewheel = function (evt) {
1535 var delta
1536 if (evt.originalEvent) {
1537 delta =
1538 evt.originalEvent.deltaY ||
1539 -evt.originalEvent.wheelDelta ||
1540 evt.originalEvent.detail
1541 }
1542 if (delta != null) {
1543 evt.preventDefault()
1544 if (evt.type === 'DOMMouseScroll') {
1545 delta = delta * 40
1546 }
1547 return this.search_results.scrollTop(
1548 delta + this.search_results.scrollTop()
1549 )
1550 }
1551 }
1552
1553 Chosen.prototype.blur_test = function (evt) {
1554 if (
1555 !this.active_field &&
1556 this.container.hasClass('chosen-container-active')
1557 ) {
1558 return this.close_field()
1559 }
1560 }
1561
1562 Chosen.prototype.close_field = function () {
1563 $(this.container[0].ownerDocument).off(
1564 'click.chosen',
1565 this.click_test_action
1566 )
1567 this.active_field = false
1568 this.results_hide()
1569 this.container.removeClass('chosen-container-active')
1570 this.clear_backstroke()
1571 this.show_search_field_default()
1572 this.search_field_scale()
1573 return this.search_field.blur()
1574 }
1575
1576 Chosen.prototype.activate_field = function () {
1577 if (this.is_disabled) {
1578 return
1579 }
1580 this.container.addClass('chosen-container-active')
1581 this.active_field = true
1582 this.search_field.val(this.search_field.val())
1583 return this.search_field.focus()
1584 }
1585
1586 Chosen.prototype.test_active_click = function (evt) {
1587 var active_container
1588 active_container = $(evt.target).closest('.chosen-container')
1589 if (
1590 active_container.length &&
1591 this.container[0] === active_container[0]
1592 ) {
1593 return (this.active_field = true)
1594 } else {
1595 return this.close_field()
1596 }
1597 }
1598
1599 Chosen.prototype.results_build = function () {
1600 this.parsing = true
1601 this.selected_option_count = null
1602 this.results_data = SelectParser.select_to_array(this.form_field)
1603 if (this.is_multiple) {
1604 this.search_choices.find('li.search-choice').remove()
1605 } else {
1606 this.single_set_selected_text()
1607 if (
1608 this.disable_search ||
1609 this.form_field.options.length <= this.disable_search_threshold
1610 ) {
1611 this.search_field[0].readOnly = true
1612 this.container.addClass('chosen-container-single-nosearch')
1613 } else {
1614 this.search_field[0].readOnly = false
1615 this.container.removeClass('chosen-container-single-nosearch')
1616 }
1617 }
1618 this.update_results_content(
1619 this.results_option_build({
1620 first: true
1621 })
1622 )
1623 this.search_field_disabled()
1624 this.show_search_field_default()
1625 this.search_field_scale()
1626 return (this.parsing = false)
1627 }
1628
1629 Chosen.prototype.result_do_highlight = function (el) {
1630 var high_bottom, high_top, maxHeight, visible_bottom, visible_top
1631 if (el.length) {
1632 this.result_clear_highlight()
1633 this.result_highlight = el
1634 this.result_highlight.addClass('highlighted')
1635 maxHeight = parseInt(this.search_results.css('maxHeight'), 10)
1636 visible_top = this.search_results.scrollTop()
1637 visible_bottom = maxHeight + visible_top
1638 high_top =
1639 this.result_highlight.position().top + this.search_results.scrollTop()
1640 high_bottom = high_top + this.result_highlight.outerHeight()
1641 if (high_bottom >= visible_bottom) {
1642 return this.search_results.scrollTop(
1643 high_bottom - maxHeight > 0 ? high_bottom - maxHeight : 0
1644 )
1645 } else if (high_top < visible_top) {
1646 return this.search_results.scrollTop(high_top)
1647 }
1648 }
1649 }
1650
1651 Chosen.prototype.result_clear_highlight = function () {
1652 if (this.result_highlight) {
1653 this.result_highlight.removeClass('highlighted')
1654 }
1655 return (this.result_highlight = null)
1656 }
1657
1658 Chosen.prototype.results_show = function () {
1659 if (
1660 this.is_multiple &&
1661 this.max_selected_options <= this.choices_count()
1662 ) {
1663 this.form_field_jq.trigger('chosen:maxselected', {
1664 chosen: this
1665 })
1666 return false
1667 }
1668 this.container.addClass('chosen-with-drop')
1669 this.results_showing = true
1670 this.search_field.focus()
1671 this.search_field.val(this.get_search_field_value())
1672 this.winnow_results()
1673 return this.form_field_jq.trigger('chosen:showing_dropdown', {
1674 chosen: this
1675 })
1676 }
1677
1678 Chosen.prototype.update_results_content = function (content) {
1679 return this.search_results.html(content)
1680 }
1681
1682 Chosen.prototype.results_hide = function () {
1683 if (this.results_showing) {
1684 this.result_clear_highlight()
1685 this.container.removeClass('chosen-with-drop')
1686 this.form_field_jq.trigger('chosen:hiding_dropdown', {
1687 chosen: this
1688 })
1689 }
1690 return (this.results_showing = false)
1691 }
1692
1693 Chosen.prototype.set_tab_index = function (el) {
1694 var ti
1695 if (this.form_field.tabIndex) {
1696 ti = this.form_field.tabIndex
1697 this.form_field.tabIndex = -1
1698 return (this.search_field[0].tabIndex = ti)
1699 }
1700 }
1701
1702 Chosen.prototype.set_label_behavior = function () {
1703 this.form_field_label = this.form_field_jq.parents('label')
1704 if (!this.form_field_label.length && this.form_field.id.length) {
1705 this.form_field_label = $("label[for='" + this.form_field.id + "']")
1706 }
1707 if (this.form_field_label.length > 0) {
1708 return this.form_field_label.on(
1709 'click.chosen',
1710 this.label_click_handler
1711 )
1712 }
1713 }
1714
1715 Chosen.prototype.show_search_field_default = function () {
1716 if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
1717 this.search_field.val(this.default_text)
1718 return this.search_field.addClass('default')
1719 } else {
1720 this.search_field.val('')
1721 return this.search_field.removeClass('default')
1722 }
1723 }
1724
1725 Chosen.prototype.search_results_mouseup = function (evt) {
1726 var target
1727 target = $(evt.target).hasClass('active-result')
1728 ? $(evt.target)
1729 : $(evt.target)
1730 .parents('.active-result')
1731 .first()
1732 if (target.length) {
1733 this.result_highlight = target
1734 this.result_select(evt)
1735 return this.search_field.focus()
1736 }
1737 }
1738
1739 Chosen.prototype.search_results_mouseover = function (evt) {
1740 var target
1741 target = $(evt.target).hasClass('active-result')
1742 ? $(evt.target)
1743 : $(evt.target)
1744 .parents('.active-result')
1745 .first()
1746 if (target) {
1747 return this.result_do_highlight(target)
1748 }
1749 }
1750
1751 Chosen.prototype.search_results_mouseout = function (evt) {
1752 if (
1753 $(evt.target).hasClass('active-result') ||
1754 $(evt.target)
1755 .parents('.active-result')
1756 .first()
1757 ) {
1758 return this.result_clear_highlight()
1759 }
1760 }
1761
1762 Chosen.prototype.choice_build = function (item) {
1763 var choice, close_link
1764 choice = $('<li />', {
1765 class: 'search-choice'
1766 }).html('<span>' + this.choice_label(item) + '</span>')
1767 if (item.disabled) {
1768 choice.addClass('search-choice-disabled')
1769 } else {
1770 close_link = $('<a />', {
1771 class: 'search-choice-close',
1772 'data-option-array-index': item.array_index
1773 })
1774 close_link.on(
1775 'click.chosen',
1776 (function (_this) {
1777 return function (evt) {
1778 return _this.choice_destroy_link_click(evt)
1779 }
1780 })(this)
1781 )
1782 choice.append(close_link)
1783 }
1784 return this.search_container.before(choice)
1785 }
1786
1787 Chosen.prototype.choice_destroy_link_click = function (evt) {
1788 evt.preventDefault()
1789 evt.stopPropagation()
1790 if (!this.is_disabled) {
1791 return this.choice_destroy($(evt.target))
1792 }
1793 }
1794
1795 Chosen.prototype.choice_destroy = function (link) {
1796 if (
1797 this.result_deselect(link[0].getAttribute('data-option-array-index'))
1798 ) {
1799 if (this.active_field) {
1800 this.search_field.focus()
1801 } else {
1802 this.show_search_field_default()
1803 }
1804 if (
1805 this.is_multiple &&
1806 this.choices_count() > 0 &&
1807 this.get_search_field_value().length < 1
1808 ) {
1809 this.results_hide()
1810 }
1811 link
1812 .parents('li')
1813 .first()
1814 .remove()
1815 return this.search_field_scale()
1816 }
1817 }
1818
1819 Chosen.prototype.results_reset = function () {
1820 this.reset_single_select_options()
1821 this.form_field.options[0].selected = true
1822 this.single_set_selected_text()
1823 this.show_search_field_default()
1824 this.results_reset_cleanup()
1825 this.trigger_form_field_change()
1826 if (this.active_field) {
1827 return this.results_hide()
1828 }
1829 }
1830
1831 Chosen.prototype.results_reset_cleanup = function () {
1832 this.current_selectedIndex = this.form_field.selectedIndex
1833 return this.selected_item.find('abbr').remove()
1834 }
1835
1836 Chosen.prototype.result_select = function (evt) {
1837 var high, item
1838 if (this.result_highlight) {
1839 high = this.result_highlight
1840 this.result_clear_highlight()
1841 if (
1842 this.is_multiple &&
1843 this.max_selected_options <= this.choices_count()
1844 ) {
1845 this.form_field_jq.trigger('chosen:maxselected', {
1846 chosen: this
1847 })
1848 return false
1849 }
1850 if (this.is_multiple) {
1851 high.removeClass('active-result')
1852 } else {
1853 this.reset_single_select_options()
1854 }
1855 high.addClass('result-selected')
1856 item = this.results_data[
1857 high[0].getAttribute('data-option-array-index')
1858 ]
1859 item.selected = true
1860 this.form_field.options[item.options_index].selected = true
1861 this.selected_option_count = null
1862 if (this.is_multiple) {
1863 this.choice_build(item)
1864 } else {
1865 this.single_set_selected_text(this.choice_label(item))
1866 }
1867 if (
1868 this.is_multiple &&
1869 (!this.hide_results_on_select || evt.metaKey || evt.ctrlKey)
1870 ) {
1871 if (evt.metaKey || evt.ctrlKey) {
1872 this.winnow_results({
1873 skip_highlight: true
1874 })
1875 } else {
1876 this.search_field.val('')
1877 this.winnow_results()
1878 }
1879 } else {
1880 this.results_hide()
1881 this.show_search_field_default()
1882 }
1883 if (
1884 this.is_multiple ||
1885 this.form_field.selectedIndex !== this.current_selectedIndex
1886 ) {
1887 this.trigger_form_field_change({
1888 selected: this.form_field.options[item.options_index].value
1889 })
1890 }
1891 this.current_selectedIndex = this.form_field.selectedIndex
1892 evt.preventDefault()
1893 return this.search_field_scale()
1894 }
1895 }
1896
1897 Chosen.prototype.single_set_selected_text = function (text) {
1898 if (text == null) {
1899 text = this.default_text
1900 }
1901 if (text === this.default_text) {
1902 this.selected_item.addClass('chosen-default')
1903 } else {
1904 this.single_deselect_control_build()
1905 this.selected_item.removeClass('chosen-default')
1906 }
1907 return this.selected_item.find('span').html(text)
1908 }
1909
1910 Chosen.prototype.result_deselect = function (pos) {
1911 var result_data
1912 result_data = this.results_data[pos]
1913 if (!this.form_field.options[result_data.options_index].disabled) {
1914 result_data.selected = false
1915 this.form_field.options[result_data.options_index].selected = false
1916 this.selected_option_count = null
1917 this.result_clear_highlight()
1918 if (this.results_showing) {
1919 this.winnow_results()
1920 }
1921 this.trigger_form_field_change({
1922 deselected: this.form_field.options[result_data.options_index].value
1923 })
1924 this.search_field_scale()
1925 return true
1926 } else {
1927 return false
1928 }
1929 }
1930
1931 Chosen.prototype.single_deselect_control_build = function () {
1932 if (!this.allow_single_deselect) {
1933 return
1934 }
1935 if (!this.selected_item.find('abbr').length) {
1936 this.selected_item
1937 .find('span')
1938 .first()
1939 .after('<abbr class="search-choice-close"></abbr>')
1940 }
1941 return this.selected_item.addClass('chosen-single-with-deselect')
1942 }
1943
1944 Chosen.prototype.get_search_field_value = function () {
1945 return this.search_field.val()
1946 }
1947
1948 Chosen.prototype.get_search_text = function () {
1949 return $.trim(this.get_search_field_value())
1950 }
1951
1952 Chosen.prototype.escape_html = function (text) {
1953 return $('<div/>')
1954 .text(text)
1955 .html()
1956 }
1957
1958 Chosen.prototype.winnow_results_set_highlight = function () {
1959 var do_high, selected_results
1960 selected_results = !this.is_multiple
1961 ? this.search_results.find('.result-selected.active-result')
1962 : []
1963 do_high = selected_results.length
1964 ? selected_results.first()
1965 : this.search_results.find('.active-result').first()
1966 if (do_high != null) {
1967 return this.result_do_highlight(do_high)
1968 }
1969 }
1970
1971 Chosen.prototype.no_results = function (terms) {
1972 var no_results_html
1973 no_results_html = this.get_no_results_html(terms)
1974 this.search_results.append(no_results_html)
1975 return this.form_field_jq.trigger('chosen:no_results', {
1976 chosen: this
1977 })
1978 }
1979
1980 Chosen.prototype.no_results_clear = function () {
1981 return this.search_results.find('.no-results').remove()
1982 }
1983
1984 Chosen.prototype.keydown_arrow = function () {
1985 var next_sib
1986 if (this.results_showing && this.result_highlight) {
1987 next_sib = this.result_highlight.nextAll('li.active-result').first()
1988 if (next_sib) {
1989 return this.result_do_highlight(next_sib)
1990 }
1991 } else {
1992 return this.results_show()
1993 }
1994 }
1995
1996 Chosen.prototype.keyup_arrow = function () {
1997 var prev_sibs
1998 if (!this.results_showing && !this.is_multiple) {
1999 return this.results_show()
2000 } else if (this.result_highlight) {
2001 prev_sibs = this.result_highlight.prevAll('li.active-result')
2002 if (prev_sibs.length) {
2003 return this.result_do_highlight(prev_sibs.first())
2004 } else {
2005 if (this.choices_count() > 0) {
2006 this.results_hide()
2007 }
2008 return this.result_clear_highlight()
2009 }
2010 }
2011 }
2012
2013 Chosen.prototype.keydown_backstroke = function () {
2014 var next_available_destroy
2015 if (this.pending_backstroke) {
2016 this.choice_destroy(this.pending_backstroke.find('a').first())
2017 return this.clear_backstroke()
2018 } else {
2019 next_available_destroy = this.search_container
2020 .siblings('li.search-choice')
2021 .last()
2022 if (
2023 next_available_destroy.length &&
2024 !next_available_destroy.hasClass('search-choice-disabled')
2025 ) {
2026 this.pending_backstroke = next_available_destroy
2027 if (this.single_backstroke_delete) {
2028 return this.keydown_backstroke()
2029 } else {
2030 return this.pending_backstroke.addClass('search-choice-focus')
2031 }
2032 }
2033 }
2034 }
2035
2036 Chosen.prototype.clear_backstroke = function () {
2037 if (this.pending_backstroke) {
2038 this.pending_backstroke.removeClass('search-choice-focus')
2039 }
2040 return (this.pending_backstroke = null)
2041 }
2042
2043 Chosen.prototype.search_field_scale = function () {
2044 var div, i, len, style, style_block, styles, width
2045 if (!this.is_multiple) {
2046 return
2047 }
2048 style_block = {
2049 position: 'absolute',
2050 left: '-1000px',
2051 top: '-1000px',
2052 display: 'none',
2053 whiteSpace: 'pre'
2054 }
2055 styles = [
2056 'fontSize',
2057 'fontStyle',
2058 'fontWeight',
2059 'fontFamily',
2060 'lineHeight',
2061 'textTransform',
2062 'letterSpacing'
2063 ]
2064 for (i = 0, len = styles.length; i < len; i++) {
2065 style = styles[i]
2066 style_block[style] = this.search_field.css(style)
2067 }
2068 div = $('<div />').css(style_block)
2069 div.text(this.get_search_field_value())
2070 $('body').append(div)
2071 width = div.width() + 25
2072 div.remove()
2073 if (this.container.is(':visible')) {
2074 width = Math.min(this.container.outerWidth() - 10, width)
2075 }
2076 return this.search_field.width(width)
2077 }
2078
2079 Chosen.prototype.trigger_form_field_change = function (extra) {
2080 this.form_field_jq.trigger('input', extra)
2081 return this.form_field_jq.trigger('change', extra)
2082 }
2083
2084 return Chosen
2085 })(AbstractChosen)
2086 }.call(this))
2087 /**
2088 *
2089 * jQuery Interdependencies library
2090 * http://miohtama.github.com/jquery-interdependencies/
2091 * Copyright 2012-2013 Mikko Ohtamaa, others
2092 *
2093 * Modifyed by SPF.
2094 *
2095 */
2096 ;(function ($) {
2097 'use strict'
2098
2099 function Rule (controller, condition, value) {
2100 this.init(controller, condition, value)
2101 }
2102
2103 $.extend(Rule.prototype, {
2104 init: function (controller, condition, value) {
2105 this.controller = controller
2106 this.condition = condition
2107 this.value = value
2108 this.rules = []
2109 this.controls = []
2110 },
2111
2112 evalCondition: function (context, control, condition, val1, val2) {
2113 if (condition == '==') {
2114 return this.checkBoolean(val1) == this.checkBoolean(val2)
2115 } else if (condition == '!=') {
2116 return this.checkBoolean(val1) != this.checkBoolean(val2)
2117 } else if (condition == '>=') {
2118 return Number(val2) >= Number(val1)
2119 } else if (condition == '<=') {
2120 return Number(val2) <= Number(val1)
2121 } else if (condition == '>') {
2122 return Number(val2) > Number(val1)
2123 } else if (condition == '<') {
2124 return Number(val2) < Number(val1)
2125 } else if (condition == '()') {
2126 return window[val1](context, control, val2)
2127 } else if (condition == 'any') {
2128 if ($.isArray(val2)) {
2129 for (var i = val2.length - 1; i >= 0; i--) {
2130 if ($.inArray(val2[i], val1.split(',')) !== -1) {
2131 return true
2132 }
2133 }
2134 } else {
2135 if ($.inArray(val2, val1.split(',')) !== -1) {
2136 return true
2137 }
2138 }
2139 } else if (condition == 'not-any') {
2140 if ($.isArray(val2)) {
2141 for (var i = val2.length - 1; i >= 0; i--) {
2142 if ($.inArray(val2[i], val1.split(',')) == -1) {
2143 return true
2144 }
2145 }
2146 } else {
2147 if ($.inArray(val2, val1.split(',')) == -1) {
2148 return true
2149 }
2150 }
2151 }
2152
2153 return false
2154 },
2155
2156 checkBoolean: function (value) {
2157 switch (value) {
2158 case true:
2159 case 'true':
2160 case 1:
2161 case '1':
2162 value = true
2163 break
2164
2165 case null:
2166 case false:
2167 case 'false':
2168 case 0:
2169 case '0':
2170 value = false
2171 break
2172 }
2173
2174 return value
2175 },
2176
2177 checkCondition: function (context) {
2178 if (!this.condition) {
2179 return true
2180 }
2181
2182 var control = context.find(this.controller)
2183
2184 var control_value = this.getControlValue(context, control)
2185
2186 if (control_value === undefined) {
2187 return false
2188 }
2189
2190 control_value = this.normalizeValue(control, this.value, control_value)
2191
2192 return this.evalCondition(
2193 context,
2194 control,
2195 this.condition,
2196 this.value,
2197 control_value
2198 )
2199 },
2200
2201 normalizeValue: function (control, baseValue, control_value) {
2202 if (typeof baseValue == 'number') {
2203 return parseFloat(control_value)
2204 }
2205
2206 return control_value
2207 },
2208
2209 getControlValue: function (context, control) {
2210 if (
2211 control.length > 1 &&
2212 (control.attr('type') == 'radio' || control.attr('type') == 'checkbox')
2213 ) {
2214 return control
2215 .filter(':checked')
2216 .map(function () {
2217 return this.value
2218 })
2219 .get()
2220 } else if (
2221 control.attr('type') == 'checkbox' ||
2222 control.attr('type') == 'radio'
2223 ) {
2224 return control.is(':checked')
2225 }
2226
2227 return control.val()
2228 },
2229
2230 createRule: function (controller, condition, value) {
2231 var rule = new Rule(controller, condition, value)
2232 this.rules.push(rule)
2233 return rule
2234 },
2235
2236 include: function (input) {
2237 this.controls.push(input)
2238 },
2239
2240 applyRule: function (context, enforced) {
2241 var result
2242
2243 if (typeof enforced == 'undefined') {
2244 result = this.checkCondition(context)
2245 } else {
2246 result = enforced
2247 }
2248
2249 var controls = $.map(this.controls, function (elem, idx) {
2250 return context.find(elem)
2251 })
2252
2253 if (result) {
2254 $(controls).each(function () {
2255 $(this).removeClass('hidden')
2256 })
2257
2258 $(this.rules).each(function () {
2259 this.applyRule(context)
2260 })
2261 } else {
2262 $(controls).each(function () {
2263 $(this).addClass('hidden')
2264 })
2265
2266 $(this.rules).each(function () {
2267 this.applyRule(context, false)
2268 })
2269 }
2270 }
2271 })
2272
2273 function Ruleset () {
2274 this.rules = []
2275 }
2276
2277 $.extend(Ruleset.prototype, {
2278 createRule: function (controller, condition, value) {
2279 var rule = new Rule(controller, condition, value)
2280 this.rules.push(rule)
2281 return rule
2282 },
2283
2284 applyRules: function (context) {
2285 $(this.rules).each(function () {
2286 this.applyRule(context)
2287 })
2288 }
2289 })
2290
2291 $.spf_deps = {
2292 createRuleset: function () {
2293 return new Ruleset()
2294 },
2295
2296 enable: function (selection, ruleset, depends) {
2297 selection.on('change keyup', function (elem) {
2298 var depend_id =
2299 elem.target.getAttribute('data-depend-id') ||
2300 elem.target.getAttribute('data-sub-depend-id')
2301
2302 if (depends.indexOf(depend_id) !== -1) {
2303 ruleset.applyRules(selection)
2304 }
2305 })
2306
2307 ruleset.applyRules(selection)
2308
2309 return true
2310 }
2311 }
2312 })(jQuery)
2313 /**
2314 *
2315 * jQuery serializeObject
2316 *
2317 * @copyright 2014, macek <paulmacek@gmail.com>
2318 * @link https://github.com/macek/jquery-serialize-object
2319 * @license BSD
2320 * @version 2.5.0
2321 *
2322 * Customized by SPF
2323 *
2324 */
2325 ;(function (root, factory) {
2326 // AMD
2327 if (typeof define === 'function' && define.amd) {
2328 define(['exports', 'jquery'], function (exports, $) {
2329 return factory(exports, $)
2330 })
2331 }
2332
2333 // CommonJS
2334 else if (typeof exports !== 'undefined') {
2335 var $ = require('jquery')
2336 factory(exports, $)
2337 }
2338
2339 // Browser
2340 else {
2341 factory(root, root.jQuery || root.Zepto || root.ender || root.$)
2342 }
2343 })(this, function (exports, $) {
2344 //
2345 // SPF: Added custom patterns for spesific validate
2346 //
2347 var patterns = {
2348 validate: /^(?!(_nonce|_pseudo))[a-zA-Z0-9_-]*(?:\[(?:\d*|(?!(_nonce|_pseudo))[a-zA-Z0-9_-]+)\])*$/i,
2349 key: /[a-zA-Z0-9_-]+|(?=\[\])/g,
2350 named: /^[a-zA-Z0-9_-]+$/,
2351 push: /^$/,
2352 fixed: /^\d+$/
2353 }
2354
2355 function FormSerializer (helper, $form) {
2356 // private variables
2357 var data = {},
2358 pushes = {}
2359
2360 // private API
2361 function build (base, key, value) {
2362 base[key] = value
2363 return base
2364 }
2365
2366 function makeObject (root, value) {
2367 var keys = root.match(patterns.key),
2368 k
2369
2370 // nest, nest, ..., nest
2371 while ((k = keys.pop()) !== undefined) {
2372 // foo[]
2373 if (patterns.push.test(k)) {
2374 var idx = incrementPush(root.replace(/\[\]$/, ''))
2375 value = build([], idx, value)
2376 }
2377
2378 // foo[n]
2379 else if (patterns.fixed.test(k)) {
2380 value = build([], k, value)
2381 }
2382
2383 // foo; foo[bar]
2384 else if (patterns.named.test(k)) {
2385 value = build({}, k, value)
2386 }
2387 }
2388
2389 return value
2390 }
2391
2392 function incrementPush (key) {
2393 if (pushes[key] === undefined) {
2394 pushes[key] = 0
2395 }
2396 return pushes[key]++
2397 }
2398
2399 function addPair (pair) {
2400 if (!patterns.validate.test(pair.name)) return this
2401 var obj = makeObject(pair.name, pair.value)
2402 data = helper.extend(true, data, obj)
2403 return this
2404 }
2405
2406 function addPairs (pairs) {
2407 if (!helper.isArray(pairs)) {
2408 throw new Error('formSerializer.addPairs expects an Array')
2409 }
2410 for (var i = 0, len = pairs.length; i < len; i++) {
2411 this.addPair(pairs[i])
2412 }
2413 return this
2414 }
2415
2416 function serialize () {
2417 return data
2418 }
2419
2420 function serializeJSON () {
2421 return JSON.stringify(serialize())
2422 }
2423
2424 // public API
2425 this.addPair = addPair
2426 this.addPairs = addPairs
2427 this.serialize = serialize
2428 this.serializeJSON = serializeJSON
2429 }
2430
2431 FormSerializer.patterns = patterns
2432
2433 FormSerializer.serializeObject = function serializeObject () {
2434 return new FormSerializer($, this)
2435 .addPairs(this.serializeArray())
2436 .serialize()
2437 }
2438
2439 FormSerializer.serializeJSON = function serializeJSON () {
2440 return new FormSerializer($, this)
2441 .addPairs(this.serializeArray())
2442 .serializeJSON()
2443 }
2444
2445 //
2446 // SPF: Renamed function names for avoid conflicts
2447 //
2448
2449 if (typeof $.fn !== 'undefined') {
2450 $.fn.serializeObjectSP_PC = FormSerializer.serializeObject
2451 $.fn.serializeJSONSP_PC = FormSerializer.serializeJSON
2452 }
2453
2454 exports.FormSerializer = FormSerializer
2455
2456 return FormSerializer
2457 })
2458