Completed
Push — master ( 076b54...819ed0 )
by Bart
9s
created

RelationManager.js ➔ ... ➔ declare._removeRow   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 23
rs 5.8541
1
define([
2
  'dojo/_base/declare',
3
  'dojo/_base/array',
4
  'dojo/_base/lang',
5
  'dojo/dom-construct',
6
  'dojo/dom-class',
7
  'dojo/dom-style',
8
  'dojo/json',
9
  'dojo/topic',
10
  'dojo/on',
11
  'dijit/_WidgetBase',
12
  'dijit/_TemplatedMixin',
13
  'dojo/text!./templates/RelationManager.html',
14
  'dstore/Memory',
15
  'dstore/Trackable',
16
  'dgrid/OnDemandGrid',
17
  'dgrid/extensions/DijitRegistry',
18
  'dgrid/extensions/ColumnResizer',
19
  '../../utils/DomUtils',
20
  '../dialogs/AddRelationDialog'
21
], function (
22
  declare,
23
  array,
24
  lang,
25
  domConstruct,
26
  domClass,
27
  domStyle,
28
  JSON,
29
  topic,
30
  on,
31
  _WidgetBase,
32
  _TemplatedMixin,
33
  template,
34
  Memory,
35
  Trackable,
36
  OnDemandGrid,
37
  DijitRegistry,
38
  ColumnResizer,
39
  DomUtils,
40
  AddRelationDialog
41
) {
42
  return declare([_WidgetBase, _TemplatedMixin], {
43
44
    templateString: template,
45
    baseClass: 'relation-manager',
46
    languageController: null,
47
    listController: null,
48
    conceptSchemeController: null,
49
    concept: null,
50
    scheme: null,
51
    _broaderStore: null,
52
    _broaderGrid: null,
53
    _narrowerStore: null,
54
    _narrowerGrid: null,
55
    _relatedStore: null,
56
    _relatedGrid: null,
57
    _memberOfStore: null,
58
    _memberOfGrid: null,
59
    _membersStore: null,
60
    _membersGrid: null,
61
    _subordinateStore: null,
62
    _subordinateGrid: null,
63
    _superordinatesCollStore: null,
64
    _superordinatesCollGrid: null,
65
    _index: 0,
66
    _isCollection: null,
67
    _relationStore: null,
68
    _addRelationDialog: null,
69
70
    postCreate: function () {
71
      this.inherited(arguments);
72
      console.debug('RelationManager::postCreate');
73
      this.trackableMemory = declare([Memory, Trackable]);
74
75
      this._broaderStore = new this.trackableMemory({ data: this.concept ? this.concept.broader : [] });
76
      this._broaderGrid = this._createGrid({
77
        collection: this._broaderStore,
78
        type: 'broader'
79
      }, this.broaderGridNode);
80
81
      this._narrowerStore = new this.trackableMemory({ data: this.concept ? this.concept.narrower : [] });
82
      this._narrowerGrid = this._createGrid({
83
        collection: this._narrowerStore,
84
        type: 'narrower'
85
      }, this.narrowerGridNode);
86
87
      this._relatedStore = new this.trackableMemory({ data: this.concept ? this.concept.related : [] });
88
      this._relatedGrid = this._createGrid({
89
        collection: this._relatedStore,
90
        type: 'related'
91
      }, this.relatedGridNode);
92
93
      this._memberOfStore = new this.trackableMemory({ data: this.concept ? this.concept.member_of : [] });
94
      this._memberOfGrid = this._createGrid({
95
        collection: this._memberOfStore,
96
        type: 'memberOf'
97
      }, this.memberOfGridNode);
98
99
      this._membersStore = new this.trackableMemory({ data: this.concept ? this.concept.members : [] });
100
      this._membersGrid = this._createGrid({
101
        collection: this._membersStore,
102
        type: 'members'
103
      }, this.membersGridNode);
104
105
      this._subordinateStore = new this.trackableMemory({ data: this.concept ? this.concept.subordinate_arrays : [] });
106
      this._subordinateGrid = this._createGrid({
107
        collection: this._subordinateStore,
108
        type: 'subordinate'
109
      }, this.subordinateGridNode);
110
111
      this._superordinatesCollStore = new this.trackableMemory({ data: this.concept ? this.concept.superordinates : [] });
112
      this._superordinatesCollGrid = this._createGrid({
113
        collection: this._superordinatesCollStore,
114
        type: 'superordinate'
115
      }, this.superordinatesCollGridNode);
116
117
      this._relationStore = this.conceptSchemeController.getConceptSchemeTree(this.scheme);
118
      this._addRelationDialog = new AddRelationDialog({
119
        parentNode: this,
120
        relationStore: this._relationStore,
121
        concept: this.concept,
122
        scheme: this.scheme
123
      });
124
      this._addRelationDialog.startup();
125
      this.own(
126
        on(this._addRelationDialog, 'ok', lang.hitch(this, function (evt) {
127
          this._addRelation(evt.conceptId, evt.conceptLabel, evt.conceptPath, evt.relation);
128
        }))
129
      );
130
131
      if (this.concept && this.concept.type === 'collection') {
132
        this.setCollectionTypes();
133
      } else {
134
        this.setConceptTypes();
135
      }
136
    },
137
138
    startup: function () {
139
      this.inherited(arguments);
140
      console.debug('RelationManager::startup');
141
      this._broaderGrid.startup();
142
      this._narrowerGrid.startup();
143
      this._relatedGrid.startup();
144
      this._memberOfGrid.startup();
145
      this._membersGrid.startup();
146
      this._subordinateGrid.startup();
147
      this._superordinatesCollGrid.startup();
148
    },
149
150
    setScheme: function (scheme) {
151
      this.scheme = scheme;
152
      this._relationStore = this.conceptSchemeController.getConceptSchemeTree(this.scheme);
153
      this._addRelationDialog.setScheme(scheme);
154
    },
155
156
    reset: function() {
157
      var TrackableMemory = declare([Memory, Trackable]);
158
      this._broaderStore = new TrackableMemory({ data: [] });
159
      this._broaderGrid.set('collection', this._broaderStore);
160
      this._narrowerStore = new TrackableMemory({ data: [] });
161
      this._narrowerGrid.set('collection', this._narrowerStore);
162
      this._relatedStore = new TrackableMemory({ data: [] });
163
      this._relatedGrid.set('colleciton', this._relatedStore);
164
      this._memberOfStore = new TrackableMemory({ data: [] });
165
      this._memberOfGrid.set('collection', this._memberOfStore);
166
      this._subordinateStore = new TrackableMemory({ data: [] });
167
      this._subordinateGrid.set('collection', this._subordinateStore);
168
      this._superordinatesCollStore = new TrackableMemory({ data: [] });
169
      this._superordinatesCollGrid.set('collection', this._superordinatesCollStore);
170
    },
171
172
    setCollectionTypes: function() {
173
      this._isCollection = true;
174
      this.broaderContainerNode.style.display = 'none';
175
      this.broaderGridNode.style.display = 'none';
176
      this.narrowerContainerNode.style.display = 'none';
177
      this.narrowerGridNode.style.display = 'none';
178
      this.relatedContainerNode.style.display = 'none';
179
      this.relatedGridNode.style.display = 'none';
180
      this.subordinateArraysContainerNode.style.display = 'none';
181
      this.subordinateGridNode.style.display = 'none';
182
183
      this.superordinatesCollContainerNode.style.display = 'block';
184
      this.superordinatesCollGridNode.style.display = 'block';
185
      this.membersContainerNode.style.display = 'block';
186
      this.membersGridNode.style.display = 'block';
187
    },
188
189
    setConceptTypes: function() {
190
      this._isCollection = false;
191
      this.broaderContainerNode.style.display = 'block';
192
      this.broaderGridNode.style.display = 'block';
193
      this.narrowerContainerNode.style.display = 'block';
194
      this.narrowerGridNode.style.display = 'block';
195
      this.relatedContainerNode.style.display = 'block';
196
      this.relatedGridNode.style.display = 'block';
197
      this.subordinateArraysContainerNode.style.display = 'block';
198
      this.subordinateGridNode.style.display = 'block';
199
200
      this.membersContainerNode.style.display = 'none';
201
      this.membersGridNode.style.display = 'none';
202
      this.superordinatesCollContainerNode.style.display = 'none';
203
      this.superordinatesCollGridNode.style.display = 'none';
204
    },
205
206
    _createGrid: function(options, node) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
207
      var columns = {
208
        label: {
209
          label: ''
210
        },
211
        remove: {
212
          label: '',
213
          renderCell: lang.hitch(this, function (object) {
214
            if (object.id === undefined) {
215
              return null;
216
            }
217
            var div = domConstruct.create('div', {'class': 'dGridHyperlink'});
218
            domConstruct.create('a', {
219
              href: '#',
220
              title: 'Remove relation',
221
              className: 'fa fa-trash',
222
              innerHTML: '',
223
              onclick: lang.hitch(this, function (evt) {
224
                evt.preventDefault();
225
                this._removeRow(object.id, options.type);
226
              })
227
            }, div);
228
            return div;
229
          })
230
        }
231
      };
232
233
      var grid = new (declare([OnDemandGrid, DijitRegistry, ColumnResizer]))({
234
        className: "dgrid-autoheight",
235
        collection: options.collection,
236
        columns: columns,
237
        showHeader: false,
238
        noDataMessage: '',
239
        loadingMessage: 'Fetching data..'
240
      }, node);
241
242
      grid.on('dgrid-error', function(event) {
243
        console.log(event.error.message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
244
      });
245
246
      return grid;
247
    },
248
249
    getData: function() {
250
      var relations = {};
251
      if (!this._isCollection) {
252
        relations.related = array.map(this._relatedStore.data, function (item) {
253
          var con = {};
254
          con.id = item.id;
255
          return con;
256
        }, this);
257
        relations.narrower = array.map(this._narrowerStore.data, function (item) {
258
          var con = {};
259
          con.id = item.id;
260
          return con;
261
        }, this);
262
        relations.broader = array.map(this._broaderStore.data, function (item) {
263
          var con = {};
264
          con.id = item.id;
265
          return con;
266
        }, this);
267
        /* jshint -W106 */
268
        relations.subordinate_arrays = array.map(this._subordinateStore.data, function (item) {
269
          var con = {};
270
          con.id = item.id;
271
          return con;
272
        }, this);
273
        /* jshint +W106 */
274
      } else {
275
        relations.members = array.map(this._membersStore.data, function (item) {
276
          var con = {};
277
          con.id = item.id;
278
          return con;
279
        }, this);
280
        relations.superordinates = array.map(this._superordinatesCollStore.data, function (item) {
281
          var con = {};
282
          con.id = item.id;
283
          return con;
284
        }, this);
285
      }
286
287
      /* jshint -W106 */
288
      relations.member_of = array.map(this._memberOfStore.data, function(item) {
289
        var con = {};
290
        con.id = item.id;
291
        return con;
292
      }, this);
293
      /* jshint +W106 */
294
295
      return relations;
296
    },
297
298
    _addRelation: function(id, label, path, relation) {
299
      var store = relation;
300
301
      var found = array.some(store.data, function (item) {
302
        return item.id == id;
303
      });
304
      if (!found) {
305
        store.add({id: id, label: label, path: path});
306
        return true;
307
      }
308
      return false;
309
    },
310
311
    setConcept: function(concept) {
312
      if (concept) {
313
        this.concept = concept;
314
        this._broaderStore = new this.trackableMemory({data: this.concept ? this.concept.broader : []});
315
        this._broaderGrid.set('collection', this._broaderStore);
316
        this._narrowerStore = new this.trackableMemory({data: this.concept ? this.concept.narrower : []});
317
        this._narrowerGrid.set('collection', this._narrowerStore);
318
        this._relatedStore = new this.trackableMemory({data: this.concept ? this.concept.related : []});
319
        this._relatedGrid.set('collection', this._relatedStore);
320
        this._memberOfStore = new this.trackableMemory({data: this.concept ? this.concept.member_of : []});
321
        this._memberOfGrid.set('collection', this._memberOfStore);
322
        this._membersStore = new this.trackableMemory({data: this.concept ? this.concept.members : []});
323
        this._membersGrid.set('collection', this._membersStore);
324
        this._subordinateStore = new this.trackableMemory({data: this.concept ? this.concept.subordinate_arrays : []});
325
        this._subordinateGrid.set('collection', this._subordinateStore);
326
        this._superordinatesCollStore = new this.trackableMemory({data: this.concept ? this.concept.superordinates : []});
327
        this._superordinatesCollGrid.set('collection', this._superordinatesCollStore);
328
329
        if (this.concept.type === 'collection') {
330
          this.setCollectionTypes();
331
        } else {
332
          this.setConceptTypes();
333
        }
334
      }
335
    },
336
337
    _addBroader: function(evt) {
338
      evt ? evt.preventDefault(): null;
339
      this._addRelationDialog.show(this._broaderStore, this._relationStore);
340
    },
341
342
    _addNarrower: function(evt) {
343
      evt ? evt.preventDefault(): null;
344
      this._addRelationDialog.show(this._narrowerStore, this._relationStore);
345
    },
346
347
    _addRelated: function(evt) {
348
      evt ? evt.preventDefault(): null;
349
      this._addRelationDialog.show(this._relatedStore, this._relationStore);
350
    },
351
352
    _addMemberOf: function(evt) {
353
      evt ? evt.preventDefault(): null;
354
      this._addRelationDialog.show(this._memberOfStore, this._relationStore);
355
    },
356
357
    _addMembers: function(evt) {
358
      evt ? evt.preventDefault(): null;
359
      this._addRelationDialog.show(this._membersStore, this._relationStore);
360
    },
361
362
    _addSubordinateArray: function(evt) {
363
      evt ? evt.preventDefault(): null;
364
      this._addRelationDialog.show(this._subordinateStore, this._relationStore);
365
    },
366
367
    _addSuperordinates: function(evt) {
368
      evt ? evt.preventDefault(): null;
369
      this._addRelationDialog.show(this._superordinatesCollStore, this._relationStore);
370
    },
371
372
    _removeRow: function(rowId, type) {
373
      console.log(rowId, type);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
374
      var store = null;
0 ignored issues
show
Unused Code introduced by
The assignment to store seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
375
      switch(type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
376
        case 'broader': store = this._broaderStore;
377
          break;
378
        case 'narrower': store = this._narrowerStore;
379
          break;
380
        case 'related': store = this._relatedStore;
381
          break;
382
        case 'memberOf': store = this._memberOfStore;
383
          break;
384
        case 'members': store = this._membersStore;
385
          break;
386
        case 'subordinate': store = this._subordinateStore;
387
          break;
388
        case 'superordinate': store = this._superordinatesCollStore;
389
          break;
390
      }
391
      if (store) {
392
        store.remove(rowId);
393
      }
394
    }
395
  });
396
});
397