Completed
Push — master ( 96c8f8...621469 )
by Koen
10s
created

nderCell   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
nc 2
nop 1
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/on',
10
  'dojo/topic',
11
  'dijit/_WidgetBase',
12
  'dijit/_TemplatedMixin',
13
  'dojo/text!./templates/SourcesManager.html',
14
  'dstore/Memory',
15
  'dstore/Trackable',
16
  'dgrid/OnDemandGrid',
17
  'dgrid/extensions/DijitRegistry',
18
  'dgrid/extensions/ColumnResizer',
19
  '../../utils/DomUtils',
20
  '../dialogs/SourcesDialog'
21
], function (
22
  declare,
23
  array,
24
  lang,
25
  domConstruct,
26
  domClass,
27
  domStyle,
28
  JSON,
29
  on,
30
  topic,
31
  _WidgetBase,
32
  _TemplatedMixin,
33
  template,
34
  Memory,
35
  Trackable,
36
  OnDemandGrid,
37
  DijitRegistry,
38
  ColumnResizer,
39
  DomUtils,
40
  SourcesDialog
41
) {
42
  return declare([_WidgetBase, _TemplatedMixin], {
43
44
    templateString: template,
45
    baseClass: 'sources-manager',
46
    languageController: null,
47
    listController: null,
48
    concept: null,
49
    languageList: null,
50
    _sourcesStore: null,
51
    _sourcesGrid: null,
52
    _sourcesDialog: null,
53
    _index: 0,
54
55
    postCreate: function () {
56
      this.inherited(arguments);
57
      console.debug('SourcesManager::postCreate');
58
59
      this.trackableMemory = declare([Memory, Trackable]);
60
      this._sourcesStore = new this.trackableMemory({ data: [] });
61
      if (this.concept) {
62
        array.forEach(this.concept.sources, lang.hitch(this, function (item) {
63
          item.id = this._index++;
64
          this._sourcesStore.put(item);
65
        }));
66
      }
67
      this._createGrid({
68
        collection: this._sourcesStore
69
      }, this.sourcesGridNode);
70
71
      this._sourcesDialog = new SourcesDialog({
72
        parentNode: this
73
      });
74
      on(this._sourcesDialog, 'add.source', lang.hitch(this, function(evt) {
75
        this._doAddSource(evt);
76
      }));
77
      on(this._sourcesDialog, 'edit.source', lang.hitch(this, function(evt) {
78
        this._doEditSource(evt);
79
      }));
80
    },
81
82
    startup: function () {
83
      this.inherited(arguments);
84
      console.debug('SourcesManager::startup');
85
      this._sourcesDialog.startup();
86
      this._sourcesGrid.startup();
87
      this._sourcesGrid.resize();
88
    },
89
90
    reset: function() {
91
      if (this._sourcesDialog) { this._sourcesDialog.reset(); }
92
      var TrackableMemory = declare([Memory, Trackable]);
93
      this._sourcesStore = new TrackableMemory({ data: [] });
94
      this._sourcesGrid.set('collection', this._sourcesStore);
95
    },
96
97
    _createGrid: function(options, node) {
98
      var columns = {
99
        citation: {
100
          label: "Citation",
101
          field: "citation",
102
          renderCell: function(object) {
103
            if (object) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if object is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
104
              var div = domConstruct.create('div', {innerHTML: object.citation});
105
              return div;
106
            }
107
          }
108
        },
109
        actions: {
110
          label: '',
111
          renderCell: lang.hitch(this, function (object) {
112
            if (object.id === undefined) {
113
              return null;
114
            }
115
            var div = domConstruct.create('div', {'class': 'dGridHyperlink'});
116
            domConstruct.create('a', {
117
              href: '#',
118
              title: 'Edit source',
119
              className: 'fa fa-pencil',
120
              innerHTML: '',
121
              style: 'margin-right: 12px;',
122
              onclick: lang.hitch(this, function (evt) {
123
                evt.preventDefault();
124
                this._editSource(object);
125
              })
126
            }, div);
127
            domConstruct.create('a', {
128
              href: '#',
129
              title: 'Remove source',
130
              className: 'fa fa-trash',
131
              innerHTML: '',
132
              onclick: lang.hitch(this, function (evt) {
133
                evt.preventDefault();
134
                this._removeRow(object.id);
135
              })
136
            }, div);
137
            return div;
138
          })
139
        }
140
      };
141
142
      var grid = new (declare([OnDemandGrid, DijitRegistry, ColumnResizer]))({
143
        collection: options.collection,
144
        columns: columns,
145
        showHeader: false,
146
        noDataMessage: 'No sources found',
147
        loadingMessage: 'Fetching data..'
148
      }, node);
149
150
      this._sourcesGrid = grid;
151
152
      grid.on('dgrid-error', function(event) {
153
        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...
154
      });
155
    },
156
157
    getData: function() {
158
      var sources = {
159
        sources: this._sourcesStore.data
160
      };
161
      return sources;
162
    },
163
164
    setConcept: function(concept) {
165
      if (concept) {
166
        this.concept = concept;
167
        this._sourcesStore = new this.trackableMemory({ data: [] });
168
        array.forEach(this.concept.sources, lang.hitch(this, function (item) {
169
          item.id = this._index++;
170
          this._sourcesStore.put(item);
171
        }));
172
        this._sourcesGrid.set('collection', this._sourcesStore);
173
      }
174
    },
175
176
    _addSource: function(evt) {
177
      evt ? evt.preventDefault() : null;
178
      this._sourcesDialog.show();
179
    },
180
181
    _editSource: function(source) {
182
      this._sourcesDialog.show(source);
183
    },
184
185
    _doAddSource: function(source) {
186
      var newSource = {
187
        citation: source.citation
188
      };
189
      console.log(newSource);
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...
190
      this._sourcesStore.add(newSource);
191
    },
192
193
    _doEditSource: function(source) {
194
      var editSource = {
195
        citation: source.citation,
196
        id: source.id
197
      };
198
      console.log(editSource);
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...
199
      this._sourcesStore.put(editSource);
200
    },
201
202
    _removeRow: function(rowId) {
203
      this._sourcesStore.remove(rowId);
204
    }
205
  });
206
});