angular-ui-select2-rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57910819d2a3e3dbb60b6ae53522e917d430f35d
4
- data.tar.gz: 3bcddc7f46b2b678457266afe5611a5c845469df
3
+ metadata.gz: a757547c35d66b0732c19bedc6a13087c124842f
4
+ data.tar.gz: c4d0e3ce67b105608615d24b5ef86078157b2768
5
5
  SHA512:
6
- metadata.gz: 8e4108bd2aab052bd980b709e73b0132dbb3f1258f04dcd0e5ede80b87ccaa87ba8dbcdd1ba814a1f7eea691506a327339858a67b08f1a1bde9a1537112e025f
7
- data.tar.gz: 3415c814a61365b9d6a20c92940fdbdbe65ac3ddfc908709b48f761aec8fff0bf351c77bf38990b490d3c84afc3bd6a07b66efced8ad93f4aa4af551ef93223e
6
+ metadata.gz: f3f47c308cde2c4387e19d2496068d9adf289652d313a176c2e5eb54004f332e15c6b1ed1dd66929813da983345928a1e53046cf8c8784d5e76246bc69ce15b9
7
+ data.tar.gz: 18b262da78b4281a0fb03443d3e865f94ca8cd73bc9178a00bba3d27f13181cc3ac330b9fc444372b9ad3217a28bac87fab1c0b0e4b06270c56206d9d56864ee
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Angular::Ui::Select2::Rails
1
+ # angular-ui-select2-rails
2
2
 
3
3
  [angular-ui-select2](https://github.com/angular-ui/ui-select2) packaged for Rails assets pipeline
4
4
 
@@ -13,13 +13,13 @@ gem 'angular-ui-select2-rails'
13
13
  Add the following directive to your Javascript manifest file (application.js):
14
14
 
15
15
  ```js
16
- //= require angular-ui-select2-rails
16
+ //= require angular-ui-select2
17
17
  ```
18
18
 
19
19
  Add the following directive to your CSS manifest file (application.css):
20
20
 
21
21
  ```css
22
- *= require angular-ui-select2-rails
22
+ *= require angular-ui-select2
23
23
  ```
24
24
 
25
25
 
@@ -2,7 +2,7 @@ module Angular
2
2
  module Ui
3
3
  module Select2
4
4
  module Rails
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
8
8
  end
@@ -11,7 +11,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
11
11
  angular.extend(options, uiSelect2Config);
12
12
  }
13
13
  return {
14
- require: '?ngModel',
14
+ require: 'ngModel',
15
15
  compile: function (tElm, tAttrs) {
16
16
  var watch,
17
17
  repeatOption,
@@ -33,6 +33,44 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
33
33
  // instance-specific options
34
34
  var opts = angular.extend({}, options, scope.$eval(attrs.uiSelect2));
35
35
 
36
+ /*
37
+ Convert from Select2 view-model to Angular view-model.
38
+ */
39
+ var convertToAngularModel = function(select2_data) {
40
+ var model;
41
+ if (opts.simple_tags) {
42
+ model = []
43
+ angular.forEach(select2_data, function(value, index) {
44
+ model.push(value.id)
45
+ })
46
+ } else {
47
+ model = select2_data
48
+ }
49
+ return model
50
+ }
51
+
52
+ /*
53
+ Convert from Angular view-model to Select2 view-model.
54
+ */
55
+ var convertToSelect2Model = function(angular_data) {
56
+ var model = []
57
+ if (!angular_data) {
58
+ return model;
59
+ }
60
+
61
+ if (opts.simple_tags) {
62
+ model = [];
63
+ angular.forEach(
64
+ angular_data,
65
+ function(value, index) {
66
+ model.push({'id': value, 'text': value});
67
+ })
68
+ } else {
69
+ model = angular_data;
70
+ }
71
+ return model
72
+ }
73
+
36
74
  if (isSelect) {
37
75
  // Use <select multiple> instead
38
76
  delete opts.multiple;
@@ -43,18 +81,22 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
43
81
 
44
82
  if (controller) {
45
83
  // Watch the model for programmatic changes
84
+ scope.$watch(tAttrs.ngModel, function(current, old) {
85
+ if (!current) {
86
+ return
87
+ }
88
+ if (current == old) {
89
+ return
90
+ }
91
+ controller.$render()
92
+ }, true)
46
93
  controller.$render = function () {
47
94
  if (isSelect) {
48
95
  elm.select2('val', controller.$viewValue);
49
96
  } else {
50
- if (isMultiple) {
51
- if (!controller.$viewValue) {
52
- elm.select2('data', []);
53
- } else if (angular.isArray(controller.$viewValue)) {
54
- elm.select2('data', controller.$viewValue);
55
- } else {
56
- elm.select2('val', controller.$viewValue);
57
- }
97
+ if (opts.multiple) {
98
+ elm.select2(
99
+ 'data', convertToSelect2Model(controller.$viewValue));
58
100
  } else {
59
101
  if (angular.isObject(controller.$viewValue)) {
60
102
  elm.select2('data', controller.$viewValue);
@@ -80,12 +122,26 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
80
122
  });
81
123
  }
82
124
 
125
+ // Update valid and dirty statuses
126
+ controller.$parsers.push(function (value) {
127
+ var div = elm.prev()
128
+ div
129
+ .toggleClass('ng-invalid', !controller.$valid)
130
+ .toggleClass('ng-valid', controller.$valid)
131
+ .toggleClass('ng-invalid-required', !controller.$valid)
132
+ .toggleClass('ng-valid-required', controller.$valid)
133
+ .toggleClass('ng-dirty', controller.$dirty)
134
+ .toggleClass('ng-pristine', controller.$pristine);
135
+ return value;
136
+ });
137
+
83
138
  if (!isSelect) {
84
139
  // Set the view and model value and update the angular template manually for the ajax/multiple select2.
85
140
  elm.bind("change", function () {
86
141
  if (scope.$$phase) return;
87
142
  scope.$apply(function () {
88
- angular.extend(controller.$viewValue, elm.select2('data'));
143
+ controller.$setViewValue(
144
+ convertToAngularModel(elm.select2('data')));
89
145
  });
90
146
  });
91
147
 
@@ -93,7 +149,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
93
149
  var initSelection = opts.initSelection;
94
150
  opts.initSelection = function (element, callback) {
95
151
  initSelection(element, function (value) {
96
- controller.$setViewValue(value);
152
+ controller.$setViewValue(convertToAngularModel(value));
97
153
  callback(value);
98
154
  });
99
155
  };
@@ -101,8 +157,16 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
101
157
  }
102
158
  }
103
159
 
160
+ elm.bind("$destroy", function() {
161
+ elm.select2("destroy");
162
+ });
163
+
104
164
  attrs.$observe('disabled', function (value) {
105
- elm.select2(value && 'disable' || 'enable');
165
+ elm.select2('enable', !value);
166
+ });
167
+
168
+ attrs.$observe('readonly', function (value) {
169
+ elm.select2('readonly', !!value);
106
170
  });
107
171
 
108
172
  if (attrs.ngMultiple) {
@@ -111,9 +175,6 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
111
175
  });
112
176
  }
113
177
 
114
- // Set initial value since Angular doesn't
115
- //elm.val(scope.$eval(attrs.ngModel));
116
-
117
178
  // Initialize the plugin late so that the injected DOM does not disrupt the template compiler
118
179
  $timeout(function () {
119
180
  elm.select2(opts);
@@ -125,7 +186,8 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
125
186
 
126
187
  // Not sure if I should just check for !isSelect OR if I should check for 'tags' key
127
188
  if (!opts.initSelection && !isSelect)
128
- controller.$setViewValue(elm.select2('data'));
189
+ controller.$setViewValue(
190
+ convertToAngularModel(elm.select2('data')));
129
191
  });
130
192
  };
131
193
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular-ui-select2-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tymon Tobolski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-16 00:00:00.000000000 Z
11
+ date: 2013-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jquery-rails