Completed
Pull Request — master (#306)
by Koen
01:51 queued 19s
created

chemeController.then   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
define([
2
  'dojo/_base/declare',
3
  'dojo/_base/array',
4
  'dojo/_base/lang',
5
  'dojo/promise/all',
6
  'dojo/dom-construct',
7
  'dojo/dom-class',
8
  'dojo/dom-style',
9
  'dojo/dom-attr',
10
  'dojo/json',
11
  'dojo/query',
12
  'dojo/on',
13
  'dojo/dom',
14
  'dijit/_WidgetBase',
15
  'dijit/_TemplatedMixin',
16
  'dojo/text!./templates/ConceptDetail.html',
17
  '../dialogs/ConceptEditDialog',
18
  'dojo/NodeList-traverse'
19
], function (
20
  declare,
21
  array,
22
  lang,
23
  all,
24
  domConstruct,
25
  domClass,
26
  domStyle,
27
  domAttr,
28
  JSON,
29
  query,
30
  on,
31
  dom,
32
  _WidgetBase,
33
  _TemplatedMixin,
34
  template,
35
  ConceptEditDialog
0 ignored issues
show
Unused Code introduced by
The parameter ConceptEditDialog is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
36
) {
37
  return declare([_WidgetBase, _TemplatedMixin], {
38
39
    templateString: template,
40
    baseClass: 'concept-detail',
41
    concept: null,
42
    conceptId: null,
43
    conceptLabel: null,
44
    scheme: null,
45
    languageController: null,
46
    listController: null,
47
    conceptSchemeController: null,
48
    _editDialog: null,
49
50
    postCreate: function () {
51
      this.inherited(arguments);
52
      console.debug('ConceptDetail::postCreate');
53
      domAttr.set(this.mergeButton, 'disabled', true);
54
    },
55
56
    startup: function () {
57
      this.inherited(arguments);
58
      console.debug('ConceptDetail::startup');
59
60
      this._setData(this.concept);
61
    },
62
63
    _openEditDialog: function (evt) {
64
      console.debug('ConceptDetail::_openEditDialog');
65
      evt ? evt.preventDefault() : null;
66
67
      this.emit('concept.edit', {
68
        concept: this.concept,
69
        schemeId: this.scheme
70
      });
71
    },
72
73
    _openMergeDialog: function(evt) {
74
      console.debug('ConceptDetail::_openMergeDialog');
75
      evt ? evt.preventDefault() : null;
76
77
      this.emit('concept.merge', {
78
        concept: this.concept,
79
        schemeId: this.scheme
80
      });
81
    },
82
83
    _deleteConcept: function(evt) {
84
      console.debug('ConceptDetail::_deleteConcept');
85
      evt ? evt.preventDefault() : null;
86
87
      this.emit('concept.delete', {
88
        concept: this.concept,
89
        schemeId: this.scheme
90
      });
91
    },
92
93
    _setData: function(concept) {
94
      console.log(concept);
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...
95
      // set view data
96
      this.conceptTitleViewNode.innerHTML = '<strong>' + this.scheme + ' : ' + concept.label + '</strong>';
97
      this.idViewNode.innerHTML = 'ID: ' + concept.id;
98
      this.typeViewNode.innerHTML = 'TYPE: ' + concept.type;
99
      this.uriViewNode.innerHTML = 'URI: ';
100
      domConstruct.create('a', { href: this.concept.uri, innerHTML: this.concept.uri, target: '_blank' },
101
        this.uriViewNode);
102
103
      // LABELS
104
      if (concept.labels && concept.label.length > 0) {
105
        var pref = '';
106
        var alt = '';
107
        array.forEach(concept.labels, lang.hitch(this, function(label) {
108
          if (label.type === 'prefLabel') {
109
            pref += label.label + ' (' + label.language + '), ';
110
          }
111
          if (label.type === 'altLabel') {
112
            alt += label.label + ' (' + label.language + '), ';
113
          }
114
        }));
115
        if (alt.length > 2) {
116
          alt = alt.substring(0, alt.length - 2);
117
        }
118
        if (pref.length > 2) {
119
          pref = pref.substring(0, pref.length - 2);
120
        }
121
        this.preferredLabelsNode.innerHTML = pref;
122
        this.alternateLabelsNode.innerHTML = alt;
123
      }
124
125
      // NARROWER
126
      if (concept.narrower && concept.narrower.length > 0) {
127
        var dt = domConstruct.create('dt', { innerHTML: 'Narrower' }, this.relationsListNode, 'first');
128
        var narrowString = '';
129
        array.forEach(concept.narrower, lang.hitch(this, function(narrow) {
130
          narrowString += '<a href="' + narrow.uri + '" target="_blank" >' + narrow.label + '</a> (' + narrow.id + '), '
131
        }));
132
        if (narrowString.length > 2) {
133
          narrowString = narrowString.substring(0, narrowString.length - 2);
134
        }
135
        domConstruct.create('dd', {innerHTML: narrowString}, dt);
136
      }
137
138
      // BROADER
139
      if (concept.broader && concept.broader.length > 0) {
140
        var dt = domConstruct.create('dt', { innerHTML: 'Broader' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
141
        var broadString = '';
142
        array.forEach(concept.broader, lang.hitch(this, function(broader) {
143
          broadString += '<a href="' + broader.uri + '" target="_blank" >' + broader.label + '</a> (' +
144
            broader.id + '), '
145
        }));
146
        if (broadString.length > 2) {
147
          broadString = broadString.substring(0, broadString.length - 2);
148
        }
149
        domConstruct.create('dd', {innerHTML: broadString}, dt);
150
      }
151
152
      // RELATED
153
      if (concept.related && concept.related.length > 0) {
154
        var dt = domConstruct.create('dt', { innerHTML: 'Related' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
155
        var relatedString = '';
156
        array.forEach(concept.related, lang.hitch(this, function(related) {
157
          relatedString += '<a href="' + related.uri + '" target="_blank" >' + related.label + '</a> (' +
158
            related.id + '), '
159
        }));
160
        if (relatedString.length > 2) {
161
          relatedString = relatedString.substring(0, relatedString.length - 2);
162
        }
163
        domConstruct.create('dd', {innerHTML: relatedString}, dt);
164
      }
165
166
      // MEMBER OF
167
      if (concept.member_of && concept.member_of.length > 0) {
168
        var dt = domConstruct.create('dt', { innerHTML: 'Member of' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
169
        var memberOfString = '';
170
        array.forEach(concept.member_of, lang.hitch(this, function(member) {
171
          memberOfString += '<a href="' + member.uri + '" target="_blank" >' + member.label + '</a> (' +
172
            member.id + '), '
173
        }));
174
        if (memberOfString.length > 2) {
175
          memberOfString = memberOfString.substring(0, memberOfString.length - 2);
176
        }
177
        domConstruct.create('dd', {innerHTML: memberOfString}, dt);
178
      }
179
180
      // MEMBERS
181
      if (concept.members && concept.members.length > 0) {
182
        var dt = domConstruct.create('dt', { innerHTML: 'Members' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
183
        var memberString = '';
184
        array.forEach(concept.members, lang.hitch(this, function(member) {
185
          memberString += '<a href="' + member.uri + '" target="_blank" >' + member.label + '</a> (' +
186
            member.id + '), '
187
        }));
188
        if (memberString.length > 2) {
189
          memberString = memberString.substring(0, memberString.length - 2);
190
        }
191
        domConstruct.create('dd', {innerHTML: memberString}, dt);
192
      }
193
194
      // SUBORDINATE ARRAYS
195
      if (concept.subordinate_arrays && concept.subordinate_arrays.length > 0) {
196
        var dt = domConstruct.create('dt', { innerHTML: 'Subordinate <br>arrays' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
197
        var subString = '';
198
        array.forEach(concept.subordinate_arrays, lang.hitch(this, function(subordinate) {
199
          subString += '<a href="' + subordinate.uri + '" target="_blank" >' + subordinate.label + '</a> (' +
200
            subordinate.id + '), '
201
        }));
202
        if (subString.length > 2) {
203
          subString = subString.substring(0, subString.length - 2);
204
        }
205
        domConstruct.create('dd', {innerHTML: subString}, dt);
206
      }
207
208
      // SUPERORDINATES
209
      if (concept.superordinates && concept.superordinates.length > 0) {
210
        var dt = domConstruct.create('dt', { innerHTML: 'Superordinates' }, this.relationsListNode, 'last');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable dt already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
211
        var superString = '';
212
        array.forEach(concept.superordinates, lang.hitch(this, function(superordinate) {
213
          superString += '<a href="' + superordinate.uri + '" target="_blank" >' + superordinate.label + '</a> (' +
214
            superordinate.id + '), '
215
        }));
216
        if (superString.length > 2) {
217
          superString = superString.substring(0, superString.length - 2);
218
        }
219
        domConstruct.create('dd', {innerHTML: superString}, dt);
220
      }
221
222
      // MATCHES
223
      if (concept.matches) {
224
        var matches = concept.matches;
225
        var matchesFound = false;
226
        if (matches.broad && matches.broad.length > 0) {
227
          this._loadMatches(matches.broad, 'broad');
228
          matchesFound = true;
229
        }
230
        if (matches.narrow && matches.narrow.length > 0) {
231
          this._loadMatches(matches.narrow, 'narrow');
232
          matchesFound = true;
233
        }
234
        if (matches.exact && matches.exact.length > 0) {
235
          this._loadMatches(matches.exact, 'exact');
236
          matchesFound = true;
237
        }
238
        if (matches.close && matches.close.length > 0) {
239
          this._loadMatches(matches.close, 'close');
240
          matchesFound = true;
241
        }
242
        if (matches.related && matches.related.length > 0) {
243
          this._loadMatches(matches.related, 'related');
244
          matchesFound = true;
245
        }
246
        if (matchesFound) {
247
          // activate merge button
248
          domAttr.set(this.mergeButton, 'disabled', false);
249
        }
250
      }
251
252
      // NOTES
253
      if (concept.notes && concept.notes.length > 0) {
254
        array.forEach(concept.notes, lang.hitch(this, function(note) {
255
          domConstruct.create('li', {
256
            lang: note.language,
257
            innerHTML: '<strong>' + this._capitalize(note.type) + '</strong> <em>(' + note.language +
258
            ')</em>: ' + note.note
259
          }, this.scopeNoteNode, 'last');
260
        }));
261
      } else {
262
        domConstruct.destroy(this.scopeNoteNode);
263
      }
264
265
      // SOURCES
266
      if (concept.sources && concept.sources.length > 0) {
267
        array.forEach(concept.sources, lang.hitch(this, function(source) {
268
          domConstruct.create('li', {
269
            innerHTML: source.citation
270
          }, this.sourcesNode, 'last');
271
        }));
272
      }else {
273
        domConstruct.destroy(this.sourcesNode);
274
      }
275
    },
276
277
    _capitalize: function (s) {
278
      // returns the first letter capitalized + the string from index 1 and out aka. the rest of the string
279
      return s[0].toUpperCase() + s.substr(1);
280
    },
281
282
    _loadMatches: function(matches, matchType) {
283
      var dt = domConstruct.create('dt', { innerHTML: this.capitalize(matchType), id: matchType }, this.matchesListNode, 'last');
284
      var matchString = '';
0 ignored issues
show
Unused Code introduced by
The variable matchString seems to be never used. Consider removing it.
Loading history...
285
      var promises = [];
286
      var dd = domConstruct.create('dd', {innerHTML: ''}, dt);
287
      array.forEach(matches, function (match, index) {
288
        domConstruct.create('span', {id: match,
289
          innerHTML: match + '&nbsp;<i class="fa fa-spinner fa-pulse"></i>&nbsp;'}, dd);
290
        promises.push(this.conceptSchemeController.getMatch(match, matchType).then(lang.hitch(this, function (matched) {
291
          var matchSpan = dom.byId(matched.data.uri);
292
          matchSpan.innerHTML = '<a href="' + matched.data.uri + '" target="_blank" >' +
293
            matched.data.label + '</a>' + (index === matches.length - 1 ? '' : ', ');
294
          domClass.set(matchSpan, 'matched');
295
        }), function(err) {
296
          console.debug(err);
297
        }));
298
      }, this);
299
300
      all(promises).then(function(res) {
0 ignored issues
show
Unused Code introduced by
The parameter res is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
301
        query('span:not(.matched)', dom.byId(matchType)).forEach(function(item) {
302
          domConstruct.destroy(item);
303
        });
304
      });
305
    },
306
307
    capitalize: function(string) {
308
      return string.charAt(0).toUpperCase() + string.slice(1);
309
    }
310
  });
311
});