/** * Enhanced Select2 Dropmenus * * @AJAX Mode - When in this mode, your value will be an object (or array of objects) of the data used by Select2 * This change is so that you do not have to do an additional query yourself on top of Select2's own query * @params [options] {object} The configuration options passed to $.fn.select2(). Refer to the documentation */ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelect2', ['uiSelect2Config', '$timeout', function (uiSelect2Config, $timeout) { var options = {}; if (uiSelect2Config) { angular.extend(options, uiSelect2Config); } return { require: 'ngModel', priority: 1, compile: function (tElm, tAttrs) { var watch, repeatOption, repeatAttr, isSelect = tElm.is('select'), isMultiple = angular.isDefined(tAttrs.multiple); // Enable watching of the options dataset if in use if (tElm.is('select')) { repeatOption = tElm.find('option[ng-repeat], option[data-ng-repeat]'); if (repeatOption.length) { repeatAttr = repeatOption.attr('ng-repeat') || repeatOption.attr('data-ng-repeat'); watch = jQuery.trim(repeatAttr.split('|')[0]).split(' ').pop(); } } return function (scope, elm, attrs, controller) { // instance-specific options var opts = angular.extend({}, options, scope.$eval(attrs.uiSelect2)); /* Convert from Select2 view-model to Angular view-model. */ var convertToAngularModel = function(select2_data) { var model; if (opts.simple_tags) { model = []; angular.forEach(select2_data, function(value, index) { model.push(value.id); }); } else { model = select2_data; } return model; }; /* Convert from Angular view-model to Select2 view-model. */ var convertToSelect2Model = function(angular_data) { var model = []; if (!angular_data) { return model; } if (opts.simple_tags) { model = []; angular.forEach( angular_data, function(value, index) { model.push({'id': value, 'text': value}); }); } else { model = angular_data; } return model; }; if (isSelect) { // Use