select2.amd.full.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. define('select2/utils',[], function () {
  2. var Utils = {};
  3. Utils.Extend = function (ChildClass, SuperClass) {
  4. var __hasProp = {}.hasOwnProperty;
  5. function BaseConstructor () {
  6. this.constructor = ChildClass;
  7. }
  8. for (var key in SuperClass) {
  9. if (__hasProp.call(SuperClass, key)) {
  10. ChildClass[key] = SuperClass[key];
  11. }
  12. }
  13. BaseConstructor.prototype = SuperClass.prototype;
  14. ChildClass.prototype = new BaseConstructor();
  15. ChildClass.__super__ = SuperClass.prototype;
  16. return ChildClass;
  17. };
  18. function getMethods (theClass) {
  19. var proto = theClass.prototype;
  20. var methods = [];
  21. for (var methodName in proto) {
  22. var m = proto[methodName];
  23. if (typeof m !== 'function') {
  24. continue;
  25. }
  26. methods.push(methodName);
  27. }
  28. return methods;
  29. }
  30. Utils.Decorate = function (SuperClass, DecoratorClass) {
  31. var decoratedMethods = getMethods(DecoratorClass);
  32. var superMethods = getMethods(SuperClass);
  33. function DecoratedClass () {
  34. var unshift = Array.prototype.unshift;
  35. var argCount = DecoratorClass.prototype.constructor.length;
  36. var calledConstructor = SuperClass.prototype.constructor;
  37. if (argCount > 0) {
  38. unshift.call(arguments, SuperClass.prototype.constructor);
  39. calledConstructor = DecoratorClass.prototype.constructor;
  40. }
  41. calledConstructor.apply(this, arguments);
  42. }
  43. DecoratorClass.displayName = SuperClass.displayName;
  44. function ctr () {
  45. this.constructor = DecoratedClass;
  46. }
  47. DecoratedClass.prototype = new ctr();
  48. for (var m = 0; m < superMethods.length; m++) {
  49. var superMethod = superMethods[m];
  50. DecoratedClass.prototype[superMethod] =
  51. SuperClass.prototype[superMethod];
  52. }
  53. var calledMethod = function (methodName) {
  54. // Stub out the original method if it's not decorating an actual method
  55. var originalMethod = function () {};
  56. if (methodName in DecoratedClass.prototype) {
  57. originalMethod = DecoratedClass.prototype[methodName];
  58. }
  59. var decoratedMethod = DecoratorClass.prototype[methodName];
  60. return function () {
  61. var unshift = Array.prototype.unshift;
  62. unshift.call(arguments, originalMethod);
  63. return decoratedMethod.apply(this, arguments);
  64. };
  65. };
  66. for (var d = 0; d < decoratedMethods.length; d++) {
  67. var decoratedMethod = decoratedMethods[d];
  68. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  69. }
  70. return DecoratedClass;
  71. };
  72. var Observable = function () {
  73. this.listeners = {};
  74. };
  75. Observable.prototype.on = function (event, callback) {
  76. if (event in this.listeners) {
  77. this.listeners[event].push(callback);
  78. } else {
  79. this.listeners[event] = [callback];
  80. }
  81. };
  82. Observable.prototype.trigger = function (event) {
  83. var slice = Array.prototype.slice;
  84. if (event in this.listeners) {
  85. this.invoke(this.listeners[event], slice.call(arguments, 1));
  86. }
  87. if ('*' in this.listeners) {
  88. this.invoke(this.listeners['*'], arguments);
  89. }
  90. };
  91. Observable.prototype.invoke = function (listeners, params) {
  92. for (var i = 0, len = listeners.length; i < len; i++) {
  93. listeners[i].apply(this, params);
  94. }
  95. };
  96. Utils.Observable = Observable;
  97. return Utils;
  98. });
  99. define('select2/results',[
  100. './utils'
  101. ], function (Utils) {
  102. function Results ($element, options, dataAdapter) {
  103. this.$element = $element;
  104. this.data = dataAdapter;
  105. Results.__super__.constructor.call(this);
  106. }
  107. Utils.Extend(Results, Utils.Observable);
  108. Results.prototype.render = function () {
  109. var $results = $(
  110. '<ul class="options"></ul>'
  111. );
  112. this.$results = $results;
  113. return $results;
  114. };
  115. Results.prototype.clear = function () {
  116. this.$results.empty();
  117. };
  118. Results.prototype.append = function (data) {
  119. var $options = [];
  120. for (var d = 0; d < data.length; d++) {
  121. var item = data[d];
  122. var $option = this.option(item);
  123. $options.push($option);
  124. }
  125. this.$results.append($options);
  126. };
  127. Results.prototype.setClasses = function () {
  128. var self = this;
  129. this.data.current(function (selected) {
  130. var selectedIds = $.map(selected, function (s) {
  131. return s.id.toString();
  132. });
  133. self.$results.find('.option.selected').removeClass('selected');
  134. var $options = self.$results.find('.option');
  135. $options.each(function () {
  136. var $option = $(this);
  137. var item = $option.data('data');
  138. if (selectedIds.indexOf(item.id.toString()) > -1) {
  139. $option.addClass('selected');
  140. }
  141. });
  142. });
  143. };
  144. Results.prototype.option = function (data) {
  145. var $option = $(
  146. '<li class="option highlightable selectable"></li>'
  147. );
  148. if (data.children && data.children.length > 0) {
  149. $option.addClass('group').removeClass('highlightable selectable');
  150. var $label = $('<strong class="group-label"></strong>');
  151. $label.html(data.text);
  152. var $children = [];
  153. for (var c = 0; c < data.children.length; c++) {
  154. var child = data.children[c];
  155. var $child = this.option(child);
  156. $children.push($child);
  157. }
  158. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  159. $childrenContainer.append($children);
  160. $option.append($label);
  161. $option.append($childrenContainer);
  162. } else {
  163. $option.html(data.text);
  164. }
  165. if (data.disabled) {
  166. $option.removeClass('selectable highlightable').addClass('disabled');
  167. }
  168. $option.data('data', data);
  169. return $option;
  170. };
  171. Results.prototype.bind = function (container, $container) {
  172. var self = this;
  173. this.on('results:all', function (data) {
  174. self.clear();
  175. self.append(data);
  176. self.setClasses();
  177. });
  178. this.on('results:append', function (data) {
  179. self.append(data);
  180. self.setClasses();
  181. });
  182. this.$results.on('mouseup', '.option.selectable', function (evt) {
  183. var $this = $(this);
  184. var data = $this.data('data');
  185. if ($this.hasClass('selected')) {
  186. self.trigger('unselected', {
  187. originalEvent: evt,
  188. data: data
  189. });
  190. self.setClasses();
  191. return;
  192. }
  193. self.trigger('selected', {
  194. originalEvent: evt,
  195. data: data
  196. });
  197. self.setClasses();
  198. });
  199. this.$results.on('mouseenter', '.option.highlightable', function (evt) {
  200. self.$results.find('.option.highlighted').removeClass('highlighted');
  201. $(this).addClass('highlighted');
  202. });
  203. this.$results.on('mouseleave', '.option', function (evt) {
  204. $(this).removeClass('highlighted');
  205. });
  206. };
  207. return Results;
  208. });
  209. define('select2/dropdown',[
  210. './utils'
  211. ], function (Utils) {
  212. function Dropdown ($element, options) {
  213. this.$element = $element;
  214. }
  215. Utils.Extend(Dropdown, Utils.Observable);
  216. Dropdown.prototype.render = function () {
  217. var $dropdown = $(
  218. '<span class="dropdown">' +
  219. '<span class="results"></span>' +
  220. '</span>'
  221. );
  222. return $dropdown;
  223. };
  224. return Dropdown;
  225. });
  226. define('select2/selection/single',[
  227. '../utils'
  228. ], function (Utils) {
  229. function SingleSelection ($element, options) {
  230. this.$element = $element;
  231. this.options = options;
  232. SingleSelection.__super__.constructor.call(this);
  233. }
  234. Utils.Extend(SingleSelection, Utils.Observable);
  235. SingleSelection.prototype.render = function () {
  236. var $selection = $(
  237. '<span class="single-select">' +
  238. '<span class="rendered-selection"></span>' +
  239. '</span>'
  240. );
  241. this.$selection = $selection;
  242. return $selection;
  243. };
  244. SingleSelection.prototype.bind = function (container, $container) {
  245. var self = this;
  246. this.$selection.on('mousedown', function (evt) {
  247. // Only respond to left clicks
  248. if (evt.which !== 1) {
  249. return;
  250. }
  251. self.trigger('toggle', {
  252. originalEvent: evt
  253. });
  254. });
  255. container.on('selection:update', function (params) {
  256. self.update(params.data);
  257. });
  258. };
  259. SingleSelection.prototype.clear = function () {
  260. this.$selection.find('.rendered-selection').empty();
  261. };
  262. SingleSelection.prototype.display = function (data) {
  263. return data.text;
  264. };
  265. SingleSelection.prototype.update = function (data) {
  266. if (data.length === 0) {
  267. this.clear();
  268. return;
  269. }
  270. var selection = data[0];
  271. var formatted = this.display(selection);
  272. this.$selection.find('.rendered-selection').html(formatted);
  273. };
  274. return SingleSelection;
  275. });
  276. define('select2/selection/multiple',[
  277. '../utils'
  278. ], function (Utils) {
  279. function MultipleSelection ($element, options) {
  280. this.$element = $element;
  281. this.options = options;
  282. MultipleSelection.__super__.constructor.call(this);
  283. }
  284. Utils.Extend(MultipleSelection, Utils.Observable);
  285. MultipleSelection.prototype.render = function () {
  286. var $selection = $(
  287. '<span class="multiple-select">' +
  288. '<ul class="rendered-selection"></ul>' +
  289. '</span>'
  290. );
  291. this.$selection = $selection;
  292. return $selection;
  293. };
  294. MultipleSelection.prototype.bind = function (container, $container) {
  295. var self = this;
  296. this.$selection.on('click', function (evt) {
  297. self.trigger('toggle', {
  298. originalEvent: evt
  299. });
  300. });
  301. container.on('selection:update', function (params) {
  302. self.update(params.data);
  303. });
  304. };
  305. MultipleSelection.prototype.clear = function () {
  306. this.$selection.find('.rendered-selection').empty();
  307. };
  308. MultipleSelection.prototype.display = function (data) {
  309. return data.text;
  310. };
  311. MultipleSelection.prototype.update = function (data) {
  312. this.clear();
  313. if (data.length === 0) {
  314. return;
  315. }
  316. var $selections = [];
  317. for (var d = 0; d < data.length; d++) {
  318. var selection = data[d];
  319. var formatted = this.display(selection);
  320. var $selection = $('<ul class="choice"></ul>');
  321. $selection.text(formatted);
  322. $selection.data('data', data);
  323. $selections.push($selection);
  324. }
  325. this.$selection.find('.rendered-selection').append($selections);
  326. };
  327. return MultipleSelection;
  328. });
  329. define('select2/data/base',[
  330. '../utils'
  331. ], function (Utils) {
  332. function BaseAdapter ($element, options) {
  333. BaseAdapter.__super__.constructor.call(this);
  334. }
  335. Utils.Extend(BaseAdapter, Utils.Observable);
  336. BaseAdapter.prototype.current = function (callback) {
  337. throw new Error('The `current` method must be defined in child classes.');
  338. };
  339. BaseAdapter.prototype.query = function (params, callback) {
  340. throw new Error('The `query` method must be defined in child classes.');
  341. };
  342. return BaseAdapter;
  343. });
  344. define('select2/data/select',[
  345. './base',
  346. '../utils',
  347. 'jquery'
  348. ], function (BaseAdapter, Utils, $) {
  349. function SelectAdapter ($element, options) {
  350. this.$element = $element;
  351. SelectAdapter.__super__.constructor.call(this);
  352. }
  353. Utils.Extend(SelectAdapter, BaseAdapter);
  354. SelectAdapter.prototype.current = function (callback) {
  355. var data = [];
  356. var self = this;
  357. this.$element.find(':selected').each(function () {
  358. var $option = $(this);
  359. var option = self.item($option);
  360. data.push(option);
  361. });
  362. callback(data);
  363. };
  364. SelectAdapter.prototype.select = function (data) {
  365. var self = this;
  366. if (this.$element.prop('multiple')) {
  367. this.current(function (currentData) {
  368. var val = [];
  369. data = [data];
  370. data.push.apply(data, currentData);
  371. for (var d = 0; d < data.length; d++) {
  372. id = data[d].id;
  373. if (val.indexOf(id) === -1) {
  374. val.push(id);
  375. }
  376. }
  377. self.$element.val(val);
  378. self.$element.trigger('change');
  379. });
  380. } else {
  381. var val = data.id;
  382. this.$element.val(val);
  383. this.$element.trigger('change');
  384. }
  385. };
  386. SelectAdapter.prototype.unselect = function (data) {
  387. var self = this;
  388. if (!this.$element.prop('multiple')) {
  389. return;
  390. }
  391. this.current(function (currentData) {
  392. var val = [];
  393. for (var d = 0; d < currentData.length; d++) {
  394. id = currentData[d].id;
  395. if (id !== data.id && val.indexOf(id) === -1) {
  396. val.push(id);
  397. }
  398. }
  399. self.$element.val(val);
  400. self.$element.trigger('change');
  401. });
  402. };
  403. SelectAdapter.prototype.bind = function (container, $container) {
  404. var self = this;
  405. container.on('select', function (params) {
  406. self.select(params.data);
  407. });
  408. container.on('unselect', function (params) {
  409. self.unselect(params.data);
  410. });
  411. };
  412. SelectAdapter.prototype.query = function (params, callback) {
  413. var data = [];
  414. var self = this;
  415. var $options = this.$element.children();
  416. $options.each(function () {
  417. var $option = $(this);
  418. if (!$option.is('option') && !$option.is('optgroup')) {
  419. return;
  420. }
  421. var option = self.item($option);
  422. var matches = self.matches(params, option);
  423. if (matches !== null) {
  424. data.push(matches);
  425. }
  426. });
  427. callback(data);
  428. };
  429. SelectAdapter.prototype.item = function ($option) {
  430. var data = $option.data('data');
  431. // If the data has already be generated, use it
  432. if (data == null) {
  433. if ($option.is('option')) {
  434. data = {
  435. id: $option.val(),
  436. text: $option.html(),
  437. disabled: $option.prop('disabled')
  438. };
  439. } else if ($option.is('optgroup')) {
  440. data = {
  441. id: -1,
  442. text: $option.attr('label'),
  443. children: []
  444. };
  445. var $children = $option.children('option');
  446. var children = [];
  447. for (var c = 0; c < $children.length; c++) {
  448. var $child = $($children[c]);
  449. var child = this.item($child);
  450. children.push(child);
  451. }
  452. data.children = children;
  453. }
  454. $option.data('data', data);
  455. }
  456. return data;
  457. };
  458. SelectAdapter.prototype.matches = function (params, data) {
  459. var match = $.extend(true, {}, data);
  460. if (data.children) {
  461. for (var c = data.children.length - 1; c >= 0; c--) {
  462. var child = data.children[c];
  463. var matches = this.matches(params, child);
  464. // If there wasn't a match, remove the object in the array
  465. if (matches === null) {
  466. match.children.splice(c, 1);
  467. }
  468. }
  469. if (match.children.length > 0) {
  470. return match;
  471. }
  472. }
  473. if ($.trim(params.term) === '') {
  474. return match;
  475. }
  476. if (data.text.indexOf(params.term) > -1) {
  477. return match;
  478. }
  479. return null;
  480. };
  481. return SelectAdapter;
  482. });
  483. define('select2/data/array',[
  484. './select',
  485. '../utils'
  486. ], function (SelectAdapter, Utils) {
  487. function ArrayAdapter ($element, options) {
  488. this.data = options.options.data;
  489. ArrayAdapter.__super__.constructor.call(this, $element, options);
  490. }
  491. Utils.Extend(ArrayAdapter, SelectAdapter);
  492. ArrayAdapter.prototype.select = function (data) {
  493. var self = this;
  494. this.$element.find('option').each(function () {
  495. var $option = $(this);
  496. var option = self.item($option);
  497. if (option.id == data.id.toString()) {
  498. $option.remove();
  499. }
  500. });
  501. var $option = this.option(data);
  502. this.$element.append($option);
  503. ArrayAdapter.__super__.select.call(this, data);
  504. };
  505. ArrayAdapter.prototype.option = function (data) {
  506. var $option = $('<option></option>');
  507. $option.text(data.text);
  508. $option.val(data.id);
  509. $option.data('data', data);
  510. return $option;
  511. };
  512. ArrayAdapter.prototype.query = function (params, callback) {
  513. var matches = [];
  514. var self = this;
  515. $.each(this.data, function () {
  516. var option = this;
  517. if (self.matches(params, option)) {
  518. matches.push(option);
  519. }
  520. });
  521. callback(matches);
  522. };
  523. return ArrayAdapter;
  524. });
  525. define('select2/data/ajax',[
  526. './array',
  527. '../utils',
  528. 'jquery'
  529. ], function (ArrayAdapter, Utils, $) {
  530. function AjaxAdapter ($element, options) {
  531. this.ajaxOptions = options.options.ajax;
  532. this.processResults = this.ajaxOptions.processResults ||
  533. function (results) {
  534. return results;
  535. };
  536. ArrayAdapter.__super__.constructor.call(this, $element, options);
  537. }
  538. Utils.Extend(AjaxAdapter, ArrayAdapter);
  539. AjaxAdapter.prototype.query = function (params, callback) {
  540. var matches = [];
  541. var self = this;
  542. var options = $.extend({
  543. type: 'GET'
  544. }, this.ajaxOptions);
  545. if (typeof options.url === 'function') {
  546. options.url = options.url(params);
  547. }
  548. if (typeof options.data === 'function') {
  549. options.data = options.data(params);
  550. }
  551. var $request = $.ajax(options);
  552. $request.success(function (data) {
  553. var results = self.processResults(data);
  554. callback(results);
  555. });
  556. };
  557. return AjaxAdapter;
  558. });
  559. define('select2/options',[
  560. './results',
  561. './dropdown',
  562. './selection/single',
  563. './selection/multiple',
  564. './data/select',
  565. './data/array',
  566. './data/ajax'
  567. ], function (ResultsList, Dropdown, SingleSelection, MultipleSelection,
  568. SelectData, ArrayData, AjaxData) {
  569. function Options (options) {
  570. this.options = options;
  571. if (options.ajax) {
  572. this.dataAdapter = this.dataAdapter || AjaxData;
  573. } else if (options.data) {
  574. this.dataAdapter = this.dataAdapter || ArrayData;
  575. } else {
  576. this.dataAdapter = this.dataAdapter || SelectData;
  577. }
  578. this.resultsAdapter = ResultsList;
  579. this.dropdownAdapter = options.dropdownAdapter || Dropdown;
  580. this.selectionAdapter = options.selectionAdapter;
  581. if (this.selectionAdapter == null) {
  582. if (this.options.multiple) {
  583. this.selectionAdapter = MultipleSelection;
  584. } else {
  585. this.selectionAdapter = SingleSelection;
  586. }
  587. }
  588. }
  589. return Options;
  590. });
  591. define('select2/core',[
  592. 'jquery',
  593. './options',
  594. './utils'
  595. ], function ($, Options, Utils) {
  596. var Select2 = function ($element, options) {
  597. this.$element = $element;
  598. options = options || {};
  599. options.multiple = options.multiple || $element.prop('multiple');
  600. this.options = new Options(options);
  601. Select2.__super__.constructor.call(this);
  602. // Set up containers and adapters
  603. this.data = new this.options.dataAdapter($element, this.options);
  604. var $container = this.render();
  605. this.$container = $container;
  606. $container.insertAfter(this.$element);
  607. $container.width($element.width());
  608. this.selection = new this.options.selectionAdapter($element, this.options);
  609. var $selectionContainer = $container.find('.selection');
  610. var $selection = this.selection.render();
  611. $selectionContainer.append($selection);
  612. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  613. var $dropdownContainer = $container.find('.dropdown-wrapper');
  614. var $dropdown = this.dropdown.render();
  615. $dropdownContainer.append($dropdown);
  616. this.results = new this.options.resultsAdapter(
  617. $element, this.options, this.data);
  618. var $resultsContainer = $dropdown.find('.results');
  619. var $results = this.results.render();
  620. $resultsContainer.append($results);
  621. // Bind events
  622. var self = this;
  623. this.data.bind(this, $container);
  624. this.selection.bind(this, $container);
  625. this.results.bind(this, $container);
  626. this.$element.on('change', function () {
  627. self.data.current(function (data) {
  628. self.trigger('selection:update', {
  629. data: data
  630. });
  631. });
  632. });
  633. this.selection.on('toggle', function () {
  634. self.toggleDropdown();
  635. });
  636. this.results.on('selected', function (params) {
  637. self.trigger('select', params);
  638. self.trigger('close');
  639. });
  640. this.results.on('unselected', function (params) {
  641. self.trigger('unselect', params);
  642. self.trigger('close');
  643. });
  644. this.on('open', function () {
  645. $container.addClass('open');
  646. });
  647. this.on('close', function () {
  648. $container.removeClass('open');
  649. });
  650. // Set the initial state
  651. this.data.current(function (initialData) {
  652. self.trigger('selection:update', {
  653. data: initialData
  654. });
  655. });
  656. this.data.query({}, function (data) {
  657. self.results.trigger('results:all', data);
  658. });
  659. // Hide the original select
  660. $element.hide();
  661. };
  662. Utils.Extend(Select2, Utils.Observable);
  663. Select2.prototype.toggleDropdown = function () {
  664. if (this.$container.hasClass('open')) {
  665. this.trigger('close');
  666. } else {
  667. this.trigger('open');
  668. }
  669. };
  670. Select2.prototype.render = function () {
  671. var $container = $(
  672. '<span class="select2 select2-container select2-theme-default">' +
  673. '<span class="selection"></span>' +
  674. '<span class="dropdown-wrapper"></span>' +
  675. '</span>'
  676. );
  677. return $container;
  678. };
  679. return Select2;
  680. });
  681. define('jquery.select2',[
  682. 'jquery',
  683. 'select2/core'
  684. ], function ($, Select2) {
  685. if ($.fn.select2 == null) {
  686. $.fn.select2 = function (options) {
  687. options = options || {};
  688. if (typeof options === 'object') {
  689. this.each(function () {
  690. var instance = new Select2($(this), options);
  691. });
  692. } else if (typeof options === 'string') {
  693. var instance = this.data('select2');
  694. instance[options](arguments.slice(1));
  695. } else {
  696. throw new Error('Invalid arguments for Select2: ' + options);
  697. }
  698. };
  699. }
  700. return Select2;
  701. });