-
Notifications
You must be signed in to change notification settings - Fork 397
/
error-handler.js
73 lines (55 loc) · 1.56 KB
/
error-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
var _ = require('./lodash');
var Dom = require('./packages/dom');
var ErrorHandler = function(wrapper, mediator, container) {
this.wrapper = wrapper;
this.mediator = mediator;
this.el = container;
if (_.isUndefined(this.el)) {
this._ensureElement();
this.wrapper.insertBefore(this.el, this.wrapper.firstChild);
}
Dom.hide(this.el);
this._bindFunctions();
this._bindMediatedEvents();
this.initialize();
};
Object.assign(ErrorHandler.prototype, require('./function-bind'), require('./mediated-events'), require('./renderable'), {
errors: [],
className: "st-errors",
eventNamespace: 'errors',
mediatedEvents: {
'reset': 'reset',
'add': 'addMessage',
'render': 'render'
},
initialize: function() {
var list = document.createElement("ul");
var p = document.createElement("p");
p.innerHTML = i18n.t("errors:title");
this.el.appendChild(p)
.appendChild(list);
this.list = list;
},
render: function() {
if (this.errors.length === 0) { return false; }
this.errors.forEach(this.createErrorItem, this);
Dom.show(this.el);
},
createErrorItem: function(errorObj) {
var error = document.createElement("li");
error.classList.add("st-errors__msg");
error.innerHTML = errorObj.text;
this.list.appendChild(error);
},
addMessage: function(error) {
this.errors.push(error);
},
reset: function() {
if (this.errors.length === 0) { return false; }
this.errors = [];
this.list.innerHTML = '';
Dom.hide(this.el);
}
});
module.exports = ErrorHandler;