1/** 2 * Provides Select2 plugin for elements. 3 */ 4(function ($, window, document) { 5 6 String.prototype.extTrim = function (char) { 7 8 var trimRegex = new RegExp('^' + char + '+|' + char + '+$', "g"); 9 10 if (trimRegex.test(this)) { 11 return this.replace(trimRegex, ''); 12 } 13 14 return this.toString(); 15 }; 16 17 String.prototype.quote_trim = function () { 18 var trimRegex = new RegExp('^"(.*)?"$', "g"), 19 resultString = this; 20 21 while (trimRegex.test(resultString)) { 22 resultString = this.replace(trimRegex, '$1'); 23 } 24 25 return resultString.toString(); 26 }; 27 28 String.prototype.regExpReplace = function (rule, replaceValue, ruleFlags) { 29 ruleFlags = ruleFlags || 'g'; 30 var trimRegex = new RegExp(rule, ruleFlags), 31 resultString = this; 32 33 resultString = this.replace(trimRegex, replaceValue); 34 35 return resultString.toString(); 36 }; 37 38 String.prototype.trimDotes = function() { 39 return this.extTrim('\\.'); 40 } 41 42 Drupal.select2functions = Drupal.select2functions || {}; 43 44 /** 45 * @constructor 46 * @this {Drupal.Select2} 47 * @param {DOM} context The context. 48 */ 49 Drupal.Select2 = function(context) { 50 51 /** 52 * Current context. 53 * @public 54 */ 55 this.context = context; 56 57 this.contextSettings = null; 58 59 this.Defaults = Drupal.Select2.Defaults; 60 61 this.functionsScopesNames = [ 62 'Drupal.Select2.functionsScope', 63 'Drupal.select2functions' 64 ]; 65 66 function setSelect2Defaults () { 67 $.extend(true, $.fn.select2.defaults, Drupal.Select2.Defaults); 68 $.extend(true, $.fn.select2.defaults, Drupal.settings.select_2.default_settings); 69 } 70 71 setSelect2Defaults(); 72 73 }; 74 75 /** 76 * Default options for the Select2. 77 * @public 78 */ 79 Drupal.Select2.Defaults = Drupal.Select2.Defaults || { 80 'adaptContainerCssClass': function (className) { 81 if (!Drupal.Select2.Defaults.classesListForCopyFromElement 82 && !Drupal.Select2.Defaults.classesExcludedForCopy) { 83 return clazz; 84 } 85 switch (typeof Drupal.Select2.Defaults.classesListForCopyFromElement) { 86 case 'string': 87 if (className == Drupal.Select2.Defaults.classesListForCopyFromElement) { 88 return className; 89 } 90 break; 91 case 'object': 92 if ($.inArray(className, Drupal.Select2.Defaults.classesListForCopyFromElement) >= 0) { 93 return className; 94 } 95 break; 96 } 97 return false; 98 }, 99 'classesListForCopyFromElement': ['error'], 100 'width': 'copy', 101 'predefineExcludions': [ 102 '.tabledrag-hide select' 103 ], 104 }; 105 106 Drupal.Select2.prototype.attachBehaviors = function(element) { 107 $.each(Drupal.behaviors, function () { 108 if ($.isFunction(this.select2attach)) { 109 this.select2attach(element); 110 } 111 }); 112 } 113 114 Drupal.Select2.functionsScope = Drupal.Select2.functionsScope || {}; 115 116 Drupal.Select2.functionsScope.formatSelectionTaxonomyTermsItem = function (term) { 117 118 if (term.hover_title) { 119 return term.hover_title; 120 } 121 122 return term.text; 123 }; 124 125 Drupal.Select2.functionsScope.formatSelection_taxonomy_terms_item = Drupal.Select2.functionsScope.formatSelectionTaxonomyTermsItem; 126 127 Drupal.Select2.functionsScope.formatResultTaxonomyTermsItem = function (term) { 128 129 var attributes = ''; 130 var prefix = ''; 131 132 if (term.hover_title != undefined) { 133 attributes = 'title="' + term.hover_title + '" '; 134 prefix = '<span class="visible-on-hover">' + term.hover_title + '</span>'; 135 } 136 137 return '<span class="taxonomy_terms_item" ' + attributes + '>' + term.text + ' </span>' + prefix; 138 }; 139 140 Drupal.Select2.functionsScope.formatResult_taxonomy_terms_item = Drupal.Select2.functionsScope.formatResultTaxonomyTermsItem; 141 142 Drupal.Select2.functionsScope.acFormatResult = function (result) { 143 return result.text; 144 }; 145 146 Drupal.Select2.functionsScope.ac_format_result = Drupal.Select2.functionsScope.acFormatResult; 147 148 Drupal.Select2.functionsScope.acFielsFormatSelection = function (item) { 149 return item.text; 150 }; 151 152 Drupal.Select2.functionsScope.ac_fiels_FormatSelection = Drupal.Select2.functionsScope.acFielsFormatSelection; 153 154 Drupal.Select2.functionsScope.acS2InitSelecttion = function (element, callback) { 155 var def_values = $(element).select2('val'); 156 157 callback({ 158 id: def_values, 159 text: def_values 160 }); 161 162 }; 163 164 Drupal.Select2.functionsScope.entityReferenceInitSelecttion = function (element, callback) { 165 var def_values = $(element).select2('val'), 166 select2 = $(element).data('select2'), 167 select2Options = select2 ? select2.opts : false, 168 hideIds = select2Options ? select2Options.hideEntityIds : false, 169 comaReplacement = select2Options ? select2Options.comma_replacement : false;
170 171 if (typeof def_values == 'string') { 172 173 var label = def_values; 174 label = label.quote_trim().replace(/"{2,}/g, '"'); 175 176 if (hideIds) { 177 label = label.replace(/\([0-9]+\)$/g, ''); 178 } 179 180 if (comaReplacement) { 181 label = label.regExpReplace('{' + comaReplacement + '}', ','); 182 } 183 184 callback({ 185 id: def_values, 186 text: label 187 }); 188 } else if (typeof (def_values) == 'object') { 189 190 data = []; 191 for (var i = 0; i < def_values.length; i++) { 192 193 var label = def_values[i]; 194 label = label.quote_trim().replace(/"{2,}/g, '"'); 195 196 if (hideIds) { 197 label = label.replace(/\([0-9]+\)$/g, ''); 198 } 199 200 if (comaReplacement) { 201 label = label.regExpReplace('{' + comaReplacement + '}', ','); 202 } 203 204 data.push({ 205 id: def_values[i], 206 text: label 207 }); 208 } 209 callback(data); 210 } 211 212 }; 213 214 Drupal.Select2.functionsScope.ac_s2_init_selecttion = Drupal.Select2.functionsScope.acS2InitSelecttion; 215 216 Drupal.Select2.functionsScope.taxonomyTermRefAcS2InitSelecttion = function (element, callback) { 217 218 var def_values = $(element).select2('val'); 219 220 if (typeof def_values == 'string') { 221 222 var label = def_values; 223 label = label.quote_trim().replace(/{{;}}/g, ',').replace(/"{2,}/g, '"'); 224 225 callback({ 226 id: def_values, 227 text: label 228 }); 229 } else if (typeof (def_values) == 'object') { 230 231 data = []; 232 for (var i = 0; i < def_values.length; i++) { 233 234 var label = def_values[i]; 235 label = label.quote_trim().replace(/{{;}}/g, ',').replace(/"{2,}/g, '"'); 236 237 data.push({ 238 id: def_values[i], 239 text: label 240 }); 241 } 242 callback(data); 243 } 244 245 }; 246 247 Drupal.Select2.functionsScope.taxonomy_term_ref_ac_s2_init_selecttion = Drupal.Select2.functionsScope.taxonomyTermRefAcS2InitSelecttion; 248 249 Drupal.Select2.functionsScope.getAjaxObjectForAcElement = function (options) { 250 return { 251 url: function (term) { 252 if (options.path_is_absolute) { 253 return options.autocomplete_path + Drupal.encodePath(term); 254 } 255 return Drupal.settings.basePath + options.autocomplete_path + '/' + Drupal.encodePath(term); 256 }, 257 dataType: 'json', 258 quietMillis: 100, 259 results: function (data) { 260 // notice we return the value of more so Select2 knows if more results can be loaded 261 262 var results_out = []; 263 264 $.each(data, function (id, title) { 265 results_out.push({ 266 id: id, 267 text: title 268 }); 269 }); 270 271 return { 272 results: results_out 273 }; 274 }, 275 params: { 276 error: function (jqXHR, textStatus, errorThrown) { 277 if (textStatus == 'abort') { 278 279 } 280 } 281 } 282 }; 283 }; 284 285 Drupal.Select2.functionsScope.ac_element_get_ajax_object = Drupal.Select2.functionsScope.getAjaxObjectForAcElement; 286 287 Drupal.Select2.prototype.setContext = function(context, settings) { 288 this.context = context; 289 this.processElements(); 290 } 291 292 Drupal.Select2.prototype.processElements = function() { 293 this.markExcludedElements(); 294 this.attachSelect2(); 295 } 296 297 Drupal.Select2.prototype.markExcludedElements = function() { 298 299 if (!this.context) return; 300 301 $.each(this.Defaults.predefineExcludions, function(index, selector) { 302 $(selector, this.context).once('select2-predefined-excludions').addClass('no-select2'); 303 }) 304 305 if (Drupal.settings.select_2.excludes.by_selectors.length > 0) { 306 for (i = 0; i < Drupal.settings.select_2.excludes.by_selectors.length; ++i) { 307 $(Drupal.settings.select_2.excludes.by_selectors[i], this.context) 308 .once('select2-excluded-by-selectors').addClass('no-select2'); 309 } 310 } 311 312 if (Drupal.settings.select_2.excludes.by_class.length > 0) { 313 var byClassSelector = Drupal.settings.select_2.excludes.by_class.join(', .'); 314 byClassSelector = '.' + byClassSelector; 315 try { 316 $(byClassSelector, this.context).once('select2-excluded-by-classe
316s').addClass('no-select2'); 317 } catch (e) { 318 throw 'ERROR while setting exlution classes by classes list: ' + e.message; 319 } 320 } 321 } 322 323 Drupal.Select2.prototype.attachSelect2 = function() { 324 325 if (!this.context) return; 326 327 if (Drupal.settings.select_2.process_all_selects_on_page) { 328 $('select', this.context).once('select2-attach').atachSelect2(); 329 } 330 331 $('select.use-select-2, input[type="text"].use-select-2, input[type="hidden"].use-select-2', this.context) 332 .once('select2-attach').atachSelect2(); 333 } 334 335 Drupal.Select2.prototype.attachSelect2ToElement = function($element) { 336 337 if ($element.hasClass('no-select2')) return; 338 339 var self = this, 340 id = $element.attr('id'); 341 342 $element.id = id; 343 344 if (this.checkElementForExclusions($element)) { 345 $element.addClass('no-select2'); 346 return; 347 } 348 349 var options = Drupal.settings.select_2.elements[id] 350 ? Drupal.settings.select_2.elements[id] 351 : {}; 352 353 options = this.prepareElementOptions(options, $element); 354 355 $(document).trigger('select2.alterElementOptions', [$element, options]); 356 357 try { 358 $element.select2(options); 359 } catch (e) { 360 if (typeof window.console == "object" && typeof console.error == "function") { 361 console.error('Error: ' + e); 362 return; 363 } 364 } 365 366 Drupal.Select2Processor.attachBehaviors($element); 367 368 var select2Container = false; 369 370 if (options.events_hadlers) { 371 $.each(options.events_hadlers, function (eventName, handlerName) { 372 var handler = self.getObjectOrFunctionByName(handlerName); 373 if (handler && typeof handler == 'function') { 374 $element.on(eventName, handler); 375 } 376 }) 377 } 378 379 if ($element.data('select2') != undefined) { 380 if ($element.data('select2').$container != undefined) { 381 select2Container = $element.data('select2').$container; 382 } else if ($element.data('select2').container != undefined) { 383 select2Container = $element.data('select2').container; 384 } 385 } 386 387 if (select2Container) { 388 // need fix select2 container width 389 var stylesForFixWidth = ['element', 'copy'], 390 cur_width = select2Container.outerWidth(); 391 if (options.width && $.inArray(options.width, stylesForFixWidth) 392 && cur_width <= 6) { 393 select2Container.width('auto'); 394 } 395 } 396 397 if (select2Container && options.jqui_sortable && $.fn.sortable) { 398 select2Container.find("ul.select2-choices").sortable({ 399 containment: 'parent', 400 start: function () { 401 $element.select2("onSortStart"); 402 }, 403 update: function () { 404 $element.select2("onSortEnd"); 405 } 406 }); 407 } 408 409 }; 410 411 Drupal.Select2.prototype.searchFunctionInScope = function(functionName) { 412 413 var self = this, 414 func = false; 415 416 functionName = functionName.toString().trimDotes(); 417 418 if (functionName.indexOf('.') > -1) { 419 if (func = this.getObjectOrFunctionByName(functionName)) { 420 return func; 421 } 422 } 423 424 $.each(this.functionsScopesNames, function (index, scopeName) { 425 if (func = self.getObjectOrFunctionByName(scopeName + '.' + functionName)) { 426 return false; 427 } 428 }); 429 430 return func; 431 } 432 433 Drupal.Select2.prototype.getObjectPropertyByName = function (obj, prop) { 434 435 if (typeof obj === 'undefined') { 436 return false; 437 } 438 439 var _index = prop.indexOf('.'); 440 if (_index > -1) { 441 return this.getObjectPropertyByName(obj[prop.substring(0, _index)], prop.substr(_index + 1)); 442 } 443 444 return obj[prop]; 445 } 446 447 Drupal.Select2.prototype.getObjectOrFunctionByName = function (name) { 448 return this.getObjectPropertyByName(window, name); 449 } 450 451 Drupal.Select2.prototype.prepareElementOptions = function(options, $element) { 452 var self = this, 453 optionsForStringToFunctionConversion = [ 454 'data', 'ajax', 'query', 'formatResult', 'formatSelection', 'initSelection' 455 ], 456 elementTagName = $element.prop("tagName"); 457 458 $.each(optionsForStringToFunctionConversion, function (index, propertyName) { 459 if (options[propertyName] && typeof options[propertyName] == 'string') { 460 var func = self.searchFunctionInScope(options[propertyName]); 461 if (func) { 462 if (propertyName == 'ajax' && typeof func == 'function') { 463 options[propertyName] = func(options); 464 } else { 465 options[propertyName] = func; 466 } 467 } 468 } 469 }); 470 471 if (options.selectedOnNewLines != undefined && options.selectedOnNewLines) { 472 options.containerCssClass += ' one-option-per-line'; 473 } 474 475 if ($element.hasClass('filter-list')) { 476 options.allowClear = false;
477 } 478 479 if (options.allowClear || $('option[value=""]', $element).length > 0) { 480 if ($('option[value=""]', $element).length > 0) { 481 // Checking for empty option exist and set placeholder by its value if 482 // placeholder does not setted 483 if (options.placeholder == undefined) { 484 options.placeholder = $('option[value=""]', $element).text(); 485 } 486 // Clear empty option text 487 $('option[value=""]', $element).html(''); 488 } 489 if (options.placeholder == undefined && $element.attr('placeholder') == undefined) { 490 // If placeholder not defined set allowClear option to false 491 options.allowClear = false; 492 } else if (options.allowClear == undefined) { 493 options.allowClear = true; 494 } 495 } 496 497 if (options.allow_add_onfly) { 498 options.createSearchChoice = function (term, data) { 499 if ($(data).filter( 500 function () { 501 return this.text.localeCompare(term) === 0; 502 } 503 ).length === 0) { 504 return { 505 id: term, 506 text: term 507 }; 508 } 509 }; 510 } 511 512 if (options.taxonomy_ref_ac_allowed) { 513 options.createSearchChoice = function (term, data) { 514 if ($(data).filter( 515 function () { 516 //return this.text.localeCompare(term) === 0; 517 } 518 ).length === 0) { 519 return { 520 id: term, 521 text: term 522 }; 523 } 524 }; 525 } 526 527 if (options.comma_replacement) { 528 var cur_val = "" + $element.val(); 529 cur_val = cur_val.replace(/".*?"/g, function (match) { 530 return match.replace(/,/g, '{{;}}'); 531 }); 532 $element.val(cur_val); 533 } 534 535 if (elementTagName == 'SELECT') { 536 // disabled_options process 537 if (options.disabled_options) { 538 $.each(options.disabled_options, function (index, value) { 539 $('option[value="' + value + '"]', $element).prop('disabled', true); 540 }); 541 } 542 options.jqui_sortable = false; 543 } 544 545 return options; 546 } 547 548 /** 549 * Check element for matching exclusions rules. 550 * @public 551 * @param {jQuery object} $element The element that must be checked according to the rules exceptions. 552 * @return {Bool} true if element must be skipped and false otherwise. 553 */ 554 Drupal.Select2.prototype.checkElementForExclusions = function($element) { 555 if (!$element.id) return false; 556 557 var excludeIds = Drupal.settings.select_2.excludes.by_id.values; 558 559 if ($.inArray($element.id, excludeIds) >= 0) { 560 return true; 561 } else if (Drupal.settings.select_2.excludes.by_id.reg_exs.length > 0) { 562 // check by regexs for ids 563 for (i = 0; i < Drupal.settings.select_2.excludes.by_id.reg_exs.length; ++i) { 564 var regex = new RegExp(Drupal.settings.select_2.excludes.by_id.reg_exs[i], "ig"); 565 566 if (regex.test($element.id)) { 567 return true; 568 } 569 } 570 } 571 572 return false; 573 } 574 575 $.fn.atachSelect2 = function () { 576 return this.each(function (index) { 577 if (!Drupal.Select2Processor) return; 578 579 var $element = $(this); 580 581 Drupal.Select2Processor.attachSelect2ToElement($element); 582 583 }); 584 } 585 586 Drupal.behaviors.select2 = { 587 attach: function (context, settings) { 588 589 if (typeof ($.fn.select2) == 'undefined') return; 590 591 Drupal.Select2Processor = Drupal.Select2Processor || new Drupal.Select2(); 592 593 if (Drupal.settings.select_2.settings_updated) { 594 Drupal.Select2Processor.setContext(context); 595 } 596 else { 597 598 Drupal.settings.select_2.settings_updated = true; 599 600 var setting_update_url = Drupal.settings.basePath + 'select2/ajax/get_settings', 601 jqxhr = $.ajax(setting_update_url) 602 .done(function (data) { 603 //merging with element defined settings 604 try { 605 Drupal.settings.select_2.excludes = data[0].settings.select_2.excludes; 606 } catch (e) { 607 throw 'ERROR while updating settings for select2: ' + e.message; 608 } 609 }) 610 .fail(function () { 611 throw 'Select2 setting update ajax request failed.'; 612 }) 613 .always(function () { 614 Drupal.Select2Processor.setContext(context); 615 }); 616 } 617 618 } 619 }; 620 621})(jQuery, window, document);;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.