Browse Source

Fix changing the key in dictionary widget

Co-Authored-By: Vlad Okhrimenko <vokhrimenko@mirantis.com>
Closes-Bug: #1449450
Change-Id: I74db5ec953ba5d1b657c90ce9f7a4d8fc286b63f
Timur Sufiev 3 years ago
parent
commit
df533b33c1

+ 22
- 16
merlin/static/merlin/js/merlin.field.models.js View File

@@ -185,19 +185,31 @@
185 185
       var dictionaryModel = Barricade.MutableObject.extend({
186 186
         create: function(json, parameters) {
187 187
           var self = Barricade.MutableObject.create.call(this, json, parameters),
188
-            _items = {},
188
+            _items = [],
189 189
             _elClass = self._elementClass,
190 190
             baseKey = utils.getMeta(_elClass, 'baseKey') || 'key',
191 191
             baseName = utils.getMeta(_elClass, 'baseName') || utils.makeTitle(baseKey);
192 192
 
193 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 207
           self.add = function(newID) {
196 208
             var regexp = new RegExp('(' + baseKey + ')([0-9]+)'),
197 209
               newValue;
198 210
             newID = newID || baseKey + utils.getNextIDSuffix(self, regexp);
199
-            if ( _elClass.instanceof(Barricade.ImmutableObject) ) {
200
-              if ( 'name' in _elClass._schema ) {
211
+            if (_elClass.instanceof(Barricade.ImmutableObject)) {
212
+              if ('name' in _elClass._schema) {
201 213
                 var nameNum = utils.getNextIDSuffix(self, regexp);
202 214
                 newValue = {name: baseName + nameNum};
203 215
               } else {
@@ -207,19 +219,12 @@
207 219
               newValue = '';
208 220
             }
209 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 224
           self.getValues = function() {
213
-            if ( !Object.keys(_items).length ) {
214
-              self.getIDs().forEach(function(id) {
215
-                _items[id] = self.getByID(id);
216
-                _items[id].keyValue = function() {
217
-                  if ( !arguments.length ) {
218
-                    return this.getID();
219
-                  } else {
220
-                    this.setID(arguments[0]);
221
-                  }
222
-                };
225
+            if ( !_items.length ) {
226
+              _items = self.toArray().map(function(value) {
227
+                return makeCacheWrapper(self, value.getID());
223 228
               });
224 229
             }
225 230
             return _items;
@@ -228,8 +233,9 @@
228 233
             return self.toArray();
229 234
           };
230 235
           self.remove = function(key) {
231
-            delete _items[key];
232
-            Barricade.MutableObject.remove.call(self, self.getPosByID(key));
236
+            var pos = self.getPosByID(key);
237
+            Barricade.MutableObject.remove.call(self, pos);
238
+            _items.splice(pos, 1);
233 239
           };
234 240
           meldGroup.call(self);
235 241
           return self;

+ 5
- 5
merlin/static/merlin/templates/fields/dictionary.html View File

@@ -1,16 +1,16 @@
1 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 3
     <div class="left-column">
4 4
       <div class="form-group">
5
-        <label for="elem-{$ $id $}.{$ key $}">
5
+        <label for="elem-{$ $id $}.{$ subvalue.uid() $}">
6 6
           <editable ng-model="subvalue.keyValue" ng-model-options="{getterSetter: true}"></editable>
7 7
         </label>
8 8
         <div class="input-group">
9
-          <input id="elem-{$ $id $}.{$ key $}" type="text" class="form-control" ng-model="subvalue.value"
10
-              ng-model-options="{ getterSetter: true }">
9
+          <input id="elem-{$ $id $}.{$ subvalue.uid() $}" type="text" class="form-control"
10
+                 ng-model="subvalue.value" ng-model-options="{ getterSetter: true }">
11 11
           <span class="input-group-btn">
12 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 14
           </span>
15 15
         </div>
16 16
       </div>

Loading…
Cancel
Save