Completed
Push — master ( 65994c...1e163f )
by Koen
8s
created

ManageLanguagesDialog.js ➔ ... ➔ lang.hitch   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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);
227
        this.languageController.updateLanguageStore();
228
        this._langGrid.refresh();
229
        this._reset();
230
      }));
231
232
      confirmationDialog.show();
233
    },
234
235
    _showEditLanguageDialog: function(language) {
236
      if (this._editLanguageDialog) {
237
        this._editLanguageDialog.show(language);
238
      }
239
    },
240
241
    _editLanguage: function(language) {
242
      when(this._langStore.add(language)).then(
243
        lang.hitch(this, function (lang) {
244
          var message = 'Language edited: ' + lang.name;
245
          topic.publish('dGrowl', message, {
246
            'title': 'Languages',
247
            'sticky': false,
248
            'channel': 'info'
249
          });
250
          this.languageController.updateLanguageStore();
251
          this._langGrid.refresh();
252
          this._reset();
253
        }),
254
        function (error) {
255
          var errorJson = JSON.parse(error.response.data);
256
          var message = "",
257
            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...
258
          array.forEach(errorJson.errors, function (errorObj) {
259
            for (prop in errorObj) {
260
              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 264. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
261
              message += prop;
0 ignored issues
show
introduced by
The variable prop is changed by the for-each loop on line 259. Only the value of the last iteration will be visible in this function if it is called outside of the loop.
Loading history...
262
              message += "</em>: ";
263
              message += errorObj[prop];
264
              message += "<br>";
265
            }
266
          });
267
          topic.publish('dGrowl', message, {
268
            'title': errorJson.message,
269
            'sticky': true,
270
            'channel': 'error'
271
          });
272
        }
273
      );
274
    }
275
276
  });
277
});
278