-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (120 loc) · 2.77 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* require dependencies
*/
var yesNoDialog = require('yes-no-dialog'),
domify = require('domify'),
Emitter = require('emitter'),
attr = require('attr'),
classes = require('classes'),
query = require('query'),
each = require('each');
module.exports = ModelEditDialog;
/*
* create new instance
*/
function ModelEditDialog (model, template, lang, opt) {
if (!(this instanceof ModelEditDialog))
return new ModelEditDialog(model, template, lang, opt);
var title,
tmp,
self = this;
/*
* set default lang if not definied
*/
if (lang === undefined)
lang = {};
if (lang.save === undefined)
lang.save = 'save';
if (lang.cancel === undefined)
lang.cancel = 'cancel';
title = lang.title;
if (title === undefined)
title = 'Edit model';
else
delete lang.title;
this.model = model;
this.opt = opt;
/*
* create new form
*/
this.form = domify(template);
/*
* make new bootstrap-dialog
*/
var ynd = this.ynd = yesNoDialog(
title,
this.form,
{
fn: this.save,
scope: this
},
{
fn: this.cancel,
scope: this
},
lang
);
['close','hide'].forEach(function(event){
ynd.on(event, function () {
self.emit(event);
});
});
};
Emitter(ModelEditDialog.prototype);
ModelEditDialog.prototype.save = function () {
var errors,
self = this;
this.model.syncWith(this.form);
/*
* save model
*/
this.model.save(function (err) {
clearErrors.call(self);
if (err && err.message === 'validation failed') {
errors = self.model.errors;
self.emit('error', errors);
displayErrors.call(self, errors);
self.ynd.show();
} else if (err) {
self.emit('save', err);
self.ynd.show();
displayError.call(self);
} else {
self.emit('save');
}
});
};
ModelEditDialog.prototype.cancel = function () {
};
function displayError () {
var errorBox = query('.alert', this.form);
classes(errorBox).remove('hidden');
errorBox.innerHTML = this.opt.error;
}
function displayErrors (errors) {
clearErrors.call(this);
var errorBox,
errorEl,
self = this;
errorBox = query('.alert', this.form);
classes(errorBox).remove('hidden');
errorBox.innerHTML = this.opt.validationError;
errors.forEach(function (err) {
errorEl = query('[name="' + err.attr + '"]', self.form);
if (!errorEl)
return;
attr(errorEl).set('title', err.message);
classes(errorEl).add('error');
});
}
function clearErrors () {
var errorBox = query('.alert', this.form);
if (errorBox) {
classes(errorBox).add('hidden');
errorBox.innerHTML = '';
}
each(query.all('.error', this.form), function (errorEl) {
attr(errorEl).set('title', '');
classes(errorEl).remove('error');
});
}