Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update angular-ui-select2 to version 0.0.5 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# angular-ui-select2-rails

[angular-ui-select2](https://github.com/angular-ui/ui-select2) packaged for Rails assets pipeline
[angular-ui-select2](https://github.com/angular-ui/ui-select2) v0.0.5 packaged for Rails assets pipeline

## Usage

Expand Down
2 changes: 1 addition & 1 deletion lib/angular/ui/select2/rails/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Angular
module Ui
module Select2
module Rails
VERSION = "0.1.1"
VERSION = "0.1.2"
end
end
end
Expand Down
65 changes: 41 additions & 24 deletions vendor/assets/javascripts/angular-ui-select2-original.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
}
return {
require: 'ngModel',
priority: 1,
compile: function (tElm, tAttrs) {
var watch,
repeatOption,
repeatAttr,
isSelect = tElm.is('select'),
isMultiple = (tAttrs.multiple !== undefined);
isMultiple = angular.isDefined(tAttrs.multiple);

// Enable watching of the options dataset if in use
if (tElm.is('select')) {
Expand All @@ -39,21 +40,21 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
var convertToAngularModel = function(select2_data) {
var model;
if (opts.simple_tags) {
model = []
model = [];
angular.forEach(select2_data, function(value, index) {
model.push(value.id)
})
model.push(value.id);
});
} else {
model = select2_data
model = select2_data;
}
return model
}
return model;
};

/*
Convert from Angular view-model to Select2 view-model.
*/
var convertToSelect2Model = function(angular_data) {
var model = []
var model = [];
if (!angular_data) {
return model;
}
Expand All @@ -64,12 +65,12 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
angular_data,
function(value, index) {
model.push({'id': value, 'text': value});
})
});
} else {
model = angular_data;
}
return model
}
return model;
};

if (isSelect) {
// Use <select multiple> instead
Expand All @@ -83,20 +84,24 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
// Watch the model for programmatic changes
scope.$watch(tAttrs.ngModel, function(current, old) {
if (!current) {
return
return;
}
if (current == old) {
return
if (current === old) {
return;
}
controller.$render()
}, true)
controller.$render();
}, true);
controller.$render = function () {
if (isSelect) {
elm.select2('val', controller.$viewValue);
} else {
if (opts.multiple) {
var viewValue = controller.$viewValue;
if (angular.isString(viewValue)) {
viewValue = viewValue.split(',');
}
elm.select2(
'data', convertToSelect2Model(controller.$viewValue));
'data', convertToSelect2Model(viewValue));
} else {
if (angular.isObject(controller.$viewValue)) {
elm.select2('data', controller.$viewValue);
Expand All @@ -112,19 +117,24 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
// Watch the options dataset for changes
if (watch) {
scope.$watch(watch, function (newVal, oldVal, scope) {
if (!newVal) return;
if (angular.equals(newVal, oldVal)) {
return;
}
// Delayed so that the options have time to be rendered
$timeout(function () {
elm.select2('val', controller.$viewValue);
// Refresh angular to remove the superfluous option
elm.trigger('change');
if(newVal && !oldVal && controller.$setPristine) {
controller.$setPristine(true);
}
});
});
}

// Update valid and dirty statuses
controller.$parsers.push(function (value) {
var div = elm.prev()
var div = elm.prev();
div
.toggleClass('ng-invalid', !controller.$valid)
.toggleClass('ng-valid', controller.$valid)
Expand All @@ -137,8 +147,12 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec

if (!isSelect) {
// Set the view and model value and update the angular template manually for the ajax/multiple select2.
elm.bind("change", function () {
if (scope.$$phase) return;
elm.bind("change", function (e) {
e.stopImmediatePropagation();

if (scope.$$phase || scope.$root.$$phase) {
return;
}
scope.$apply(function () {
controller.$setViewValue(
convertToAngularModel(elm.select2('data')));
Expand Down Expand Up @@ -171,6 +185,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec

if (attrs.ngMultiple) {
scope.$watch(attrs.ngMultiple, function(newVal) {
attrs.$set('multiple', !!newVal);
elm.select2(opts);
});
}
Expand All @@ -185,11 +200,13 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
controller.$render();

// Not sure if I should just check for !isSelect OR if I should check for 'tags' key
if (!opts.initSelection && !isSelect)
if (!opts.initSelection && !isSelect) {
controller.$setViewValue(
convertToAngularModel(elm.select2('data')));
convertToAngularModel(elm.select2('data'))
);
}
});
};
}
};
}]);
}]);