This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
227 lines (191 loc) · 8.03 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Generated by https://github.com/alexander-schranz/sulu-backend-bundle.
*/
define(function () {
'use strict';
var defaults = {
activeTab: 'general',
data: {},
instanceName: 'form',
newTitle: 'l91_sulu_form.forms.new_form'
};
var tabs = {
'general': {
'name': 'general',
'url': '/admin/api/l91/forms/template',
'formSelector': '#form-form'
}
};
var translatePrefix = 'l91_sulu_form.forms.';
var eventPrefix = 'l91.sulu.form.forms.';
return {
view: true,
layout: {
content: {
width: 'fixed',
topSpace: true,
leftSpace: false,
rightSpace: false
}
},
templates: $.map(tabs, function(value) {
return [value['url']];
}),
/**
* Initializes the collections list
*/
initialize: function () {
// extend defaults with options
this.options = this.sandbox.util.extend(true, {}, defaults, this.options);
this.saved = true;
this.bindCustomEvents();
this.render();
},
/**
* Binds custom related events
*/
bindCustomEvents: function () {
this.sandbox.on('sulu.header.back', function () {
this.sandbox.emit(eventPrefix + 'navigate-list');
}.bind(this));
this.sandbox.on('sulu.toolbar.save', this.save.bind(this));
this.sandbox.on('sulu.toolbar.delete', this.delete.bind(this));
},
/**
* Renders the component
*/
render: function () {
this.setHeaderInfos();
this.renderForm(tabs[this.options.activeTab]['url']);
},
/**
* Renders a specific tab
*/
renderForm: function (template) {
var formSelector = this.getActiveFormSelector();
this.sandbox.dom.html(this.$el, this.renderTemplate(template));
this.sandbox.form.create(formSelector);
this.sandbox.form.setData(formSelector, this.options.data).then(function () {
this.sandbox.start(formSelector); // start all components that are used in the form
this.bindFormEvents();
this.sandbox.dom.find('input[autofocus]').first().focus();
}.bind(this));
},
/**
* Binds sandbox and dom events for active tab
*/
bindFormEvents: function () {
var formSelector = this.getActiveFormSelector();
this.sandbox.dom.on(formSelector, 'keyup', this.activateSaveButton.bind(this), 'input, textarea');
this.sandbox.dom.on(formSelector, 'change', this.activateSaveButton.bind(this), 'input[type="checkbox"], select');
this.sandbox.on('husky.select.width.selected.item', this.activateSaveButton.bind(this));
this.sandbox.on('husky.ckeditor.changed', this.activateSaveButton.bind(this));
this.sandbox.on('sulu.content.changed', this.activateSaveButton.bind(this));
this.sandbox.on('husky.overlay.alert.closed', this.activateSaveButton.bind(this));
// FIXME https://github.com/sulu/sulu/issues/2652
this.initSortableBlock();
this.sandbox.dom.on(formSelector, 'form-add', function(e, propertyName, data, index) {
var $elements = this.sandbox.dom.children(this.$find('[data-mapper-property="' + propertyName + '"]')),
$element = (index !== undefined && $elements.length > index) ? $elements[index] : this.sandbox.dom.last($elements);
// start new subcomponents
this.sandbox.start($element);
// reinit sorting
this.initSortableBlock();
}.bind(this));
this.sandbox.dom.on(formSelector, 'init-sortable', function(e) {
// reinit sorting
this.initSortableBlock();
this.sandbox.emit('sulu.content.changed');
}.bind(this));
},
initSortableBlock: function() {
var $sortable = this.sandbox.dom.find('.sortable', this.$el),
sortable;
if (!!$sortable && $sortable.length > 0) {
this.sandbox.dom.sortable($sortable, 'destroy');
sortable = this.sandbox.dom.sortable($sortable, {
handle: '.move',
forcePlaceholderSize: true
});
// (un)bind event listener
this.sandbox.dom.unbind(sortable, 'sortupdate');
sortable.bind('sortupdate', function(event) {
this.sandbox.emit('sulu.content.changed');
}.bind(this));
}
},
getActiveFormSelector: function() {
return tabs[this.options.activeTab]['formSelector'];
},
/**
* Activates the save button in header
*/
activateSaveButton: function () {
if (this.saved === true) {
this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false);
this.saved = false;
}
},
/**
* Sets all the Info contained in the header
* like breadcrumb or title
*/
setHeaderInfos: function () {
if (!this.options.data.id) {
this.sandbox.emit('sulu.header.toolbar.item.disable', 'settings', false);
}
},
/**
* Deletes the current collection
*/
delete: function () {
if (!!this.options.data.id) {
this.sandbox.emit(eventPrefix + 'delete', [this.options.data.id], null, function () {
this.sandbox.sulu.unlockDeleteSuccessLabel();
this.sandbox.emit(eventPrefix + 'navigate-list');
}.bind(this));
}
},
/**
* Saves the current tab
*/
save: function () {
var formSelector = this.getActiveFormSelector();
if (this.sandbox.form.validate(formSelector)) {
var data = this.sandbox.form.getData(formSelector);
data.id = this.options.data.id;
data.locale = this.options.data.locale;
this.options.data = data;
this.sandbox.emit('sulu.header.toolbar.item.loading', 'save');
this.sandbox.emit(eventPrefix + 'save', this.options.data, this.savedCallback.bind(this, !this.options.data.id));
}
},
/**
* Method which gets called after the save-process has finished
* @param {Boolean} toEdit if true the form will be navigated to the edit-modus
* @param {Object} result the saved collection model or the error model
* @param {Boolean} success to trigger success callback, false to trigger error callback
*/
savedCallback: function (toEdit, result, success) {
if (success === true) {
this.setHeaderInfos();
this.sandbox.emit('sulu.header.toolbar.item.disable', 'save', true);
this.saved = true;
if (toEdit === true) {
this.sandbox.emit(eventPrefix + 'navigate-to', result.id);
} else {
// TODO FIXME else the key of the fields are not set
this.sandbox.emit(eventPrefix + 'navigate-to', result.id);
}
this.sandbox.emit('sulu.labels.success.show', translatePrefix + 'save.success', 'labels.success');
} else {
this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false);
if (result.code === 1) {
this.sandbox.emit('sulu.labels.error.show', translatePrefix + 'save.error-unique', 'labels.error');
} else {
this.sandbox.emit('sulu.labels.error.show', translatePrefix + 'save.error', 'labels.error');
}
}
}
};
});