summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimur Sufiev <tsufiev@mirantis.com>2015-06-15 13:27:03 -0700
committerTimur Sufiev <tsufiev@mirantis.com>2015-06-24 10:06:10 -0700
commitdf533b33c112d0eb9fc9371914f7ba2ccae048d8 (patch)
tree5257a104aa3cd5c87e6f073227169bcf607a48b9
parentbcc69f218d73409d5396a84b7e5f1f91b8ad15b1 (diff)
Fix changing the key in dictionary widget
Co-Authored-By: Vlad Okhrimenko <vokhrimenko@mirantis.com> Closes-Bug: #1449450 Change-Id: I74db5ec953ba5d1b657c90ce9f7a4d8fc286b63f
Notes
Notes (review): Verified+2: Jenkins Code-Review+2: Timur Sufiev <tsufiev@mirantis.com> Workflow+1: Timur Sufiev <tsufiev@mirantis.com> Submitted-by: Jenkins Submitted-at: Thu, 25 Jun 2015 11:34:53 +0000 Reviewed-on: https://review.openstack.org/191962 Project: stackforge/merlin Branch: refs/heads/master
-rw-r--r--merlin/static/merlin/js/merlin.field.models.js38
-rw-r--r--merlin/static/merlin/templates/fields/dictionary.html10
2 files changed, 27 insertions, 21 deletions
diff --git a/merlin/static/merlin/js/merlin.field.models.js b/merlin/static/merlin/js/merlin.field.models.js
index c492d75..289f515 100644
--- a/merlin/static/merlin/js/merlin.field.models.js
+++ b/merlin/static/merlin/js/merlin.field.models.js
@@ -185,19 +185,31 @@
185 var dictionaryModel = Barricade.MutableObject.extend({ 185 var dictionaryModel = Barricade.MutableObject.extend({
186 create: function(json, parameters) { 186 create: function(json, parameters) {
187 var self = Barricade.MutableObject.create.call(this, json, parameters), 187 var self = Barricade.MutableObject.create.call(this, json, parameters),
188 _items = {}, 188 _items = [],
189 _elClass = self._elementClass, 189 _elClass = self._elementClass,
190 baseKey = utils.getMeta(_elClass, 'baseKey') || 'key', 190 baseKey = utils.getMeta(_elClass, 'baseKey') || 'key',
191 baseName = utils.getMeta(_elClass, 'baseName') || utils.makeTitle(baseKey); 191 baseName = utils.getMeta(_elClass, 'baseName') || utils.makeTitle(baseKey);
192 192
193 modelMixin.call(self, 'dictionary'); 193 modelMixin.call(self, 'dictionary');
194 194
195 function makeCacheWrapper(container, key) {
196 var value = container.getByID(key);
197 value.keyValue = function () {
198 if ( arguments.length ) {
199 value.setID(arguments[0]);
200 } else {
201 return value.getID();
202 }
203 };
204 return value;
205 }
206
195 self.add = function(newID) { 207 self.add = function(newID) {
196 var regexp = new RegExp('(' + baseKey + ')([0-9]+)'), 208 var regexp = new RegExp('(' + baseKey + ')([0-9]+)'),
197 newValue; 209 newValue;
198 newID = newID || baseKey + utils.getNextIDSuffix(self, regexp); 210 newID = newID || baseKey + utils.getNextIDSuffix(self, regexp);
199 if ( _elClass.instanceof(Barricade.ImmutableObject) ) { 211 if (_elClass.instanceof(Barricade.ImmutableObject)) {
200 if ( 'name' in _elClass._schema ) { 212 if ('name' in _elClass._schema) {
201 var nameNum = utils.getNextIDSuffix(self, regexp); 213 var nameNum = utils.getNextIDSuffix(self, regexp);
202 newValue = {name: baseName + nameNum}; 214 newValue = {name: baseName + nameNum};
203 } else { 215 } else {
@@ -207,19 +219,12 @@
207 newValue = ''; 219 newValue = '';
208 } 220 }
209 self.push(newValue, utils.extend(self._parameters, {id: newID})); 221 self.push(newValue, utils.extend(self._parameters, {id: newID}));
210 _items[newID] = self.getByID(newID); 222 _items.push(makeCacheWrapper(self, newID));
211 }; 223 };
212 self.getValues = function() { 224 self.getValues = function() {
213 if ( !Object.keys(_items).length ) { 225 if ( !_items.length ) {
214 self.getIDs().forEach(function(id) { 226 _items = self.toArray().map(function(value) {
215 _items[id] = self.getByID(id); 227 return makeCacheWrapper(self, value.getID());
216 _items[id].keyValue = function() {
217 if ( !arguments.length ) {
218 return this.getID();
219 } else {
220 this.setID(arguments[0]);
221 }
222 };
223 }); 228 });
224 } 229 }
225 return _items; 230 return _items;
@@ -228,8 +233,9 @@
228 return self.toArray(); 233 return self.toArray();
229 }; 234 };
230 self.remove = function(key) { 235 self.remove = function(key) {
231 delete _items[key]; 236 var pos = self.getPosByID(key);
232 Barricade.MutableObject.remove.call(self, self.getPosByID(key)); 237 Barricade.MutableObject.remove.call(self, pos);
238 _items.splice(pos, 1);
233 }; 239 };
234 meldGroup.call(self); 240 meldGroup.call(self);
235 return self; 241 return self;
diff --git a/merlin/static/merlin/templates/fields/dictionary.html b/merlin/static/merlin/templates/fields/dictionary.html
index e915c60..529f5d5 100644
--- a/merlin/static/merlin/templates/fields/dictionary.html
+++ b/merlin/static/merlin/templates/fields/dictionary.html
@@ -1,16 +1,16 @@
1<collapsible-group content="value" on-add="value.add()"> 1<collapsible-group content="value" on-add="value.add()">
2 <div class="three-columns" ng-repeat="(key, subvalue) in value.getValues() track by key"> 2 <div class="three-columns" ng-repeat="subvalue in value.getValues() track by subvalue.keyValue()">
3 <div class="left-column"> 3 <div class="left-column">
4 <div class="form-group"> 4 <div class="form-group">
5 <label for="elem-{$ $id $}.{$ key $}"> 5 <label for="elem-{$ $id $}.{$ subvalue.uid() $}">
6 <editable ng-model="subvalue.keyValue" ng-model-options="{getterSetter: true}"></editable> 6 <editable ng-model="subvalue.keyValue" ng-model-options="{getterSetter: true}"></editable>
7 </label> 7 </label>
8 <div class="input-group"> 8 <div class="input-group">
9 <input id="elem-{$ $id $}.{$ key $}" type="text" class="form-control" ng-model="subvalue.value" 9 <input id="elem-{$ $id $}.{$ subvalue.uid() $}" type="text" class="form-control"
10 ng-model-options="{ getterSetter: true }"> 10 ng-model="subvalue.value" ng-model-options="{ getterSetter: true }">
11 <span class="input-group-btn"> 11 <span class="input-group-btn">
12 <button class="btn btn-default fa fa-minus-circle" type="button" 12 <button class="btn btn-default fa fa-minus-circle" type="button"
13 ng-click="value.remove(key)"></button> 13 ng-click="value.remove(subvalue.keyValue())"></button>
14 </span> 14 </span>
15 </div> 15 </div>
16 </div> 16 </div>