Completed
Push — master ( 78237d...8836d0 )
by Koen
8s
created

atramhasis/static/admin/src/app/ui/dialogs/ManageLanguagesDialog.js   B

Complexity

Total Complexity 38
Complexity/F 1.36

Size

Lines of Code 293
Function Count 28

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 6
dl 0
loc 293
rs 8.3999
wmc 38
mnd 2
bc 36
fnc 28
bpm 1.2857
cpm 1.3571
noi 8

1 Function

Rating   Name   Duplication   Size   Complexity  
B ManageLanguagesDialog.js ➔ define 0 273 1
1
define([
2
  'dojo/_base/declare',
3
  'dijit/_TemplatedMixin',
4
  'dijit/_WidgetsInTemplateMixin',
5
  'dijit/Dialog',
6
  'dojo/topic',
7
  'dojo/_base/lang',
8
  'dojo/query',
9
  'dojo/on',
10
  'dojo/when',
11
  'dojo/_base/array',
12
  'dojo/dom-construct',
13
  'dgrid/OnDemandGrid',
14
  'dgrid/extensions/DijitRegistry',
15
  'dgrid/extensions/ColumnResizer',
16
  'dojo/text!./templates/ManageLanguagesDialog.html',
17
  'dijit/ConfirmDialog',
18
  '../../utils/DomUtils',
19
  './LanguageDialog',
20
  'dojo/NodeList-manipulate'
21
], function (
22
  declare,
23
  _TemplatedMixin,
24
  _WidgetsInTemplateMixin,
25
  Dialog,
26
  topic,
27
  lang,
28
  query,
29
  on,
30
  when,
31
  array,
32
  domConstruct,
33
  OnDemandGrid,
34
  DijitRegistry,
35
  ColumnResizer,
36
  template,
37
  ConfirmDialog,
38
  DomUtils,
39
  LanguageDialog
40
) {
41
  return declare([Dialog, _TemplatedMixin, _WidgetsInTemplateMixin], {
42
43
    templateString: template,
44
    parentNode: null,
45
    baseClass: 'manage-languages-dialog',
46
    title: 'Manage languages',
47
    languageController: null,
48
    _langStore: null,
49
    _langGrid: null,
50
51
    postCreate: function () {
52
      this.inherited(arguments);
53
54
      this._langStore = this.languageController.getLanguageStore();
55
      this._langGrid = this._createGrid({
56
        collection: this._langStore
57
      }, this.langGridNode);
58
59
      this._editLanguageDialog = new LanguageDialog({
60
        edit: true
61
      });
62
      this._editLanguageDialog.startup();
63
64
      on(this._editLanguageDialog, 'edit.language', lang.hitch(this, function(evt) {
65
        this._editLanguage(evt.language);
66
      }));
67
    },
68
69
    startup: function () {
70
      this.inherited(arguments);
71
      this._langGrid.startup();
72
      this._langGrid.resize();
73
    },
74
75
    _createGrid: function(options, node) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
      var columns = {
77
        id: {
78
          label: "ID",
79
          field: "id"
80
        },
81
        name: {
82
          label: "Name",
83
          field: "name"
84
        },
85
        actions: {
86
          label: '',
87
          renderCell: lang.hitch(this, function (object) {
88
            if (object.id === undefined) {
89
              return null;
90
            }
91
            var div = domConstruct.create('div', {'class': 'dGridHyperlink'});
92
            domConstruct.create('a', {
93
              href: '#',
94
              title: 'Edit language',
95
              className: 'fa fa-pencil',
96
              innerHTML: '',
97
              onclick: lang.hitch(this, function (evt) {
98
                evt.preventDefault();
99
                this._showEditLanguageDialog(object);
100
              })
101
            }, div);
102
103
            domConstruct.create('a', {
104
              href: '#',
105
              title: 'Remove language',
106
              className: 'fa fa-trash',
107
              innerHTML: '',
108
              style: 'margin-left: 10px;',
109
              onclick: lang.hitch(this, function (evt) {
110
                evt.preventDefault();
111
                this._removeRow(object);
112
              })
113
            }, div);
114
            return div;
115
          })
116
        }
117
      };
118
119
      var grid = new (declare([OnDemandGrid, DijitRegistry, ColumnResizer]))({
120
        collection: options.collection,
121
        columns: columns,
122
        noDataMessage: 'No languages found',
123
        loadingMessage: 'Fetching data..'
124
      }, node);
125
126
      grid.on('dgrid-error', function(event) {
127
        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...
128
      });
129
130
      return grid;
131
    },
132
133
    hide: function () {
134
      this.inherited(arguments);
135
    },
136
137
    show: function () {
138
      this.inherited(arguments);
139
      this._reset();
140
    },
141
142
    _addLangClick: function (evt) {
143
      console.debug('LanguagesDialog::_okClick');
144
      evt.preventDefault();
145
      if (this._validate()) {
146
        var obj = {
147
          id: this.codeInputNode.value,
148
          name: this.nameInputNode.value
149
        };
150
        when(this._langStore.add(obj)).then(
151
          lang.hitch(this, function (lang) {
152
            var message = 'New language added: ' + lang.name;
153
            topic.publish('dGrowl', message, {
154
              'title': 'Languages',
155
              'sticky': false,
156
              'channel': 'info'
157
            });
158
            this.languageController.updateLanguageStore();
159
            this._langGrid.refresh();
160
            this._reset();
161
          }),
162
          function (error) {
163
            var errorJson = JSON.parse(error.response.data);
164
            var message = "",
165
              prop = null;
0 ignored issues
show
Unused Code introduced by
The assignment to prop 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...
166
            array.forEach(errorJson.errors, function (errorObj) {
167
              for (prop in errorObj) {
168
                message += "-<em>";
0 ignored issues
show
Bug introduced by
The variable message is changed as part of the for-each loop for example by "<br>" on line 172. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
169
                message += prop;
0 ignored issues
show
introduced by
The variable prop is changed by the for-each loop on line 167. Only the value of the last iteration will be visible in this function if it is called outside of the loop.
Loading history...
170
                message += "</em>: ";
171
                message += errorObj[prop];
172
                message += "<br>";
173
              }
174
            });
175
            topic.publish('dGrowl', message, {
176
              'title': errorJson.message,
177
              'sticky': true,
178
              'channel': 'error'
179
            });
180
          }
181
        );
182
      } else {
183
        topic.publish('dGrowl', '-Please fill in a language code to add a new language.', {
184
          'title': 'Invalid values',
185
          'sticky': true,
186
          'channel': 'error'
187
        });
188
      }
189
    },
190
191
    _cancelClick: function (evt) {
192
      console.debug('LanguagesDialog::_cancelClick');
193
      evt.preventDefault();
194
      this.hide();
195
    },
196
197
    _reset: function () {
198
      this._langGrid.resize();
199
      this.codeInputNode.value = '';
200
      this.nameInputNode.value = '';
201
    },
202
203
    _validate: function () {
204
      if (this.codeInputNode.value.trim() !== '' && this.codeInputNode.value.trim() !== '') {
205
        return true;
206
      }
207
      return false;
208
    },
209
210
    _removeRow: function(language) {
211
      var content = '<p style="font-size: 15px;">Are you sure you want to remove <strong>'+ language.name +
212
        '</strong> (ID: ' + language.id + ')?</p>';
213
214
      var confirmationDialog = new ConfirmDialog({
215
        title: 'Delete language',
216
        content: content,
217
        baseClass: 'confirm-dialog'
218
      });
219
      query('.dijitButton', confirmationDialog.domNode).addClass('button tiny');
220
      confirmationDialog.closeText.innerHTML = '<i class="fa fa-times"></i>';
221
222
      on(confirmationDialog, 'close', function() {
223
        confirmationDialog.destroy();
224
      });
225
      on(confirmationDialog, 'execute', lang.hitch(this, function () {
226
        this._langStore.remove(language.id).then(function(removedLang) {
227
          topic.publish('dGrowl', 'Language removed: ' + removedLang.name + ' (' + removedLang.id + ')', {
228
            'title': 'Languages',
229
            'sticky': false,
230
            'channel': 'info'
231
          });
232
          this.languageController.updateLanguageStore();
233
          this._langGrid.refresh();
234
          this._reset();
235
        }, function(err) {
236
          if (err.response && err.response.status === '409' || err.response.status === 409) {
237
            topic.publish('dGrowl', language.name + ' (' + language.id +
238
              ') is in use and can\'t be removed', {
239
              'title': 'Languages',
240
              'sticky': false,
241
              'channel': 'warn'
242
            });
243
          }
244
        });l
0 ignored issues
show
Bug introduced by
The variable l seems to be never declared. If this is a global, consider adding a /** global: l */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
245
246
      }));
247
248
      confirmationDialog.show();
249
    },
250
251
    _showEditLanguageDialog: function(language) {
252
      if (this._editLanguageDialog) {
253
        this._editLanguageDialog.show(language);
254
      }
255
    },
256
257
    _editLanguage: function(language) {
258
      when(this._langStore.add(language)).then(
259
        lang.hitch(this, function (lang) {
260
          var message = 'Language edited: ' + lang.name;
261
          topic.publish('dGrowl', message, {
262
            'title': 'Languages',
263
            'sticky': false,
264
            'channel': 'info'
265
          });
266
          this.languageController.updateLanguageStore();
267
          this._langGrid.refresh();
268
          this._reset();
269
        }),
270
        function (error) {
271
          var errorJson = JSON.parse(error.response.data);
272
          var message = "",
273
            prop = null;
0 ignored issues
show
Unused Code introduced by
The assignment to prop 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...
274
          array.forEach(errorJson.errors, function (errorObj) {
275
            for (prop in errorObj) {
276
              message += "-<em>";
0 ignored issues
show
Bug introduced by
The variable message is changed as part of the for-each loop for example by "<br>" on line 280. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
277
              message += prop;
0 ignored issues
show
introduced by
The variable prop is changed by the for-each loop on line 275. Only the value of the last iteration will be visible in this function if it is called outside of the loop.
Loading history...
278
              message += "</em>: ";
279
              message += errorObj[prop];
280
              message += "<br>";
281
            }
282
          });
283
          topic.publish('dGrowl', message, {
284
            'title': errorJson.message,
285
            'sticky': true,
286
            'channel': 'error'
287
          });
288
        }
289
      );
290
    }
291
292
  });
293
});
294