1
|
|
|
/*global document */ |
2
|
|
|
|
3
|
|
|
import EditorUtils from '../modules/EditorUtils' |
4
|
|
|
import Json from '../modules/EditorJson' |
5
|
|
|
import {IframeNode, IframeCommentNode} from '../utils/iframe' |
6
|
|
|
import Handlebars from 'handlebars' |
7
|
|
|
import RichText from '../utils/rich-texarea' |
8
|
|
|
import Color from '../utils/color-picker' |
9
|
|
|
import Link from '../utils/link-picker' |
10
|
|
|
import image from '../utils/img-picker' |
11
|
|
|
import smiley from '../utils/smiley-picker' |
12
|
|
|
import on from 'on' |
13
|
|
|
|
14
|
|
|
export default class EditorInputs { |
15
|
|
|
constructor() { |
16
|
|
|
this._json = Json.instance |
17
|
|
|
var colorWysiwyg = document.querySelector('.wysiwyg-popup.color') |
18
|
|
|
if (colorWysiwyg != null) { |
19
|
|
|
this.color = new Color(colorWysiwyg) |
20
|
|
|
} |
21
|
|
|
var linkWysiwyg = document.querySelector('.wysiwyg-popup.link') |
22
|
|
|
if (linkWysiwyg != null) { |
23
|
|
|
this.link = new Link(linkWysiwyg) |
24
|
|
|
} |
25
|
|
|
var imgWysiwyg = document.querySelector('.wysiwyg-popup.image') |
26
|
|
|
if (imgWysiwyg != null) { |
27
|
|
|
this.image = new image(imgWysiwyg) |
|
|
|
|
28
|
|
|
} |
29
|
|
|
var imgWysiwyg = document.querySelector('.wysiwyg-popup.smiley') |
|
|
|
|
30
|
|
|
if (imgWysiwyg != null) { |
31
|
|
|
this.smiley = new smiley(imgWysiwyg) |
|
|
|
|
32
|
|
|
} |
33
|
|
|
this.onBlur = on(this) |
34
|
|
|
this.onReload = on(this) |
35
|
|
|
this.onDisableInput = on(this) |
36
|
|
|
|
37
|
|
|
this._inputElements() |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
rebind() { |
41
|
|
|
this._reloads = [].slice.call(document.querySelectorAll('[reload=true]')) |
42
|
|
|
this._inputs = [].slice.call(document.querySelectorAll('input.form-abe')) |
43
|
|
|
this._inputs = this._inputs.concat([].slice.call(document.querySelectorAll('textarea.form-abe'))) |
44
|
|
|
|
45
|
|
|
Array.prototype.forEach.call(this._reloads, (reload) => { |
46
|
|
|
reload.removeEventListener('blur', this._handleReloadBlur) |
47
|
|
|
reload.addEventListener('blur', this._handleReloadBlur) |
48
|
|
|
}) |
49
|
|
|
|
50
|
|
|
Array.prototype.forEach.call(this._inputs, (input) => { |
51
|
|
|
input.removeEventListener('focus', this._handleInputFocus) |
52
|
|
|
input.addEventListener('focus', this._handleInputFocus) |
53
|
|
|
}) |
54
|
|
|
|
55
|
|
|
this._selects = [].slice.call(document.querySelectorAll('#abeForm select')) |
56
|
|
|
Array.prototype.forEach.call(this._selects, (select) => { |
57
|
|
|
select.removeEventListener('change', this._handleChangeSelect) |
58
|
|
|
select.addEventListener('change', this._handleChangeSelect) |
59
|
|
|
}) |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Manage input element to update template page |
64
|
|
|
* @return {void} |
65
|
|
|
*/ |
66
|
|
|
_inputElements(){ |
67
|
|
|
this._handleReloadBlur = this._inputReloadBlur.bind(this) |
68
|
|
|
this._handleInputFocus = this._inputFocus.bind(this) |
69
|
|
|
this._handleInputBlur = this._inputBlur.bind(this) |
70
|
|
|
this._handleInputKeyup = this._inputKeyup.bind(this) |
71
|
|
|
this._handleChangeSelect = this._changeSelect.bind(this) |
72
|
|
|
|
73
|
|
|
var richs = document.querySelectorAll('.rich') |
74
|
|
|
if(typeof richs !== 'undefined' && richs !== null){ |
75
|
|
|
Array.prototype.forEach.call(richs, (rich) => { |
76
|
|
|
new RichText(rich, this.color, this.link, this.image, this.smiley) |
|
|
|
|
77
|
|
|
}) |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
this.rebind() |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
_hideIfEmpty(node, value) { |
84
|
|
|
var hide = IframeNode('#page-template', '[data-if-empty-clear="' + node.getAttribute('data-abe-') + '"]')[0] |
85
|
|
|
|
86
|
|
|
if(typeof hide !== 'undefined' && hide !== null) { |
87
|
|
|
if(value === '') { |
88
|
|
|
hide.style.display = 'none' |
89
|
|
|
}else { |
90
|
|
|
hide.style.display = '' |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* [_inputBlur description] |
97
|
|
|
* @param {[type]} e [description] |
98
|
|
|
* @return {[type]} [description] |
99
|
|
|
*/ |
100
|
|
|
_inputBlur(e) { |
101
|
|
|
e.target.removeEventListener('keyup', this._handleInputFocus) |
102
|
|
|
e.target.removeEventListener('blur', this._handleInputFocus) |
103
|
|
|
|
104
|
|
|
var nodes = EditorUtils.getNode(EditorUtils.getAttr(e.target)) |
105
|
|
|
Array.prototype.forEach.call(nodes, (node) => { |
106
|
|
|
this._hideIfEmpty(node, e.target.value) |
107
|
|
|
EditorUtils.formToHtml(node, e.target) |
108
|
|
|
node.classList.remove('select-border') |
109
|
|
|
node.classList.remove('display-attr') |
110
|
|
|
}) |
111
|
|
|
|
112
|
|
|
this.onBlur._fire(nodes, e.target) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* [_inputKeyup description] |
117
|
|
|
* @param {[type]} e [description] |
118
|
|
|
* @return {[type]} [description] |
119
|
|
|
*/ |
120
|
|
|
_inputKeyup(e) { |
121
|
|
|
var nodes = EditorUtils.getNode(EditorUtils.getAttr(e.target)) |
122
|
|
|
Array.prototype.forEach.call(nodes, (node) => { |
123
|
|
|
this._hideIfEmpty(node, e.target.value) |
124
|
|
|
EditorUtils.formToHtml(node, e.target) |
125
|
|
|
}) |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* [_inputFocus description] |
130
|
|
|
* @param {[type]} e [description] |
131
|
|
|
* @return {[type]} [description] |
132
|
|
|
*/ |
133
|
|
|
_inputReloadBlur(e) { |
134
|
|
|
if (!e.target.parentNode.classList.contains('upload-wrapper') && e.currentTarget.getAttribute('data-autocomplete') !== 'true') { |
135
|
|
|
this.onReload._fire() |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* [_inputFocus description] |
141
|
|
|
* @param {[type]} e [description] |
142
|
|
|
* @return {[type]} [description] |
143
|
|
|
*/ |
144
|
|
|
_inputFocus(e) { |
145
|
|
|
EditorUtils.checkAttribute() |
146
|
|
|
EditorUtils.scrollToInputElement(e.target) |
147
|
|
|
|
148
|
|
|
// switch to set appropriate output {text|link|image|...} |
149
|
|
|
// listen to user input on ABE from |
150
|
|
|
e.target.addEventListener('keyup', this._handleInputKeyup) |
151
|
|
|
e.target.addEventListener('blur', this._handleInputBlur) |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* [_changeSelect description] |
156
|
|
|
* @param {[type]} e [description] |
157
|
|
|
* @return {[type]} [description] |
158
|
|
|
*/ |
159
|
|
|
_changeSelect(e) { |
160
|
|
|
var target = e.currentTarget |
161
|
|
|
var maxLength = parseInt(target.getAttribute('data-maxlength')) |
162
|
|
|
var options = [].slice.call(target.querySelectorAll('option')) |
163
|
|
|
var optionsChecked = target.querySelectorAll('option:checked') |
164
|
|
|
var count = optionsChecked.length |
165
|
|
|
var attr = EditorUtils.getAttr(target) |
166
|
|
|
|
167
|
|
|
if(typeof maxLength !== 'undefined' && maxLength !== null && maxLength !== '' && !isNaN(maxLength)) { |
168
|
|
|
var lastValues |
169
|
|
|
if(count > maxLength) { |
170
|
|
|
lastValues = JSON.parse(target.getAttribute('last-values')) |
171
|
|
|
|
172
|
|
|
Array.prototype.forEach.call(optionsChecked, function(optionChecked) { |
173
|
|
|
var unselect = true |
174
|
|
|
Array.prototype.forEach.call(lastValues, function(lastValue) { |
175
|
|
|
if(optionChecked.value.indexOf('{') > -1 || optionChecked.value.indexOf('[') > -1) { |
176
|
|
|
if(JSON.stringify(JSON.parse(optionChecked.value)) == JSON.stringify(lastValue)) { |
177
|
|
|
unselect = false |
178
|
|
|
} |
179
|
|
|
}else { |
180
|
|
|
if(optionChecked.value == lastValue) { |
181
|
|
|
unselect = false |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
}) |
185
|
|
|
|
186
|
|
|
if(unselect) { |
187
|
|
|
optionChecked.removeAttribute('selected') |
188
|
|
|
optionChecked.selected = false |
189
|
|
|
optionChecked.disabled = true |
190
|
|
|
} |
191
|
|
|
}) |
192
|
|
|
}else { |
193
|
|
|
lastValues = '[' |
194
|
|
|
Array.prototype.forEach.call(optionsChecked, function(optionChecked) { |
195
|
|
|
if(optionChecked.value !== '') { |
196
|
|
|
if(optionChecked.value.indexOf('{') > -1 || optionChecked.value.indexOf('[') > -1) { |
197
|
|
|
lastValues += JSON.stringify(JSON.parse(optionChecked.value)) |
198
|
|
|
}else { |
199
|
|
|
lastValues += `"${optionChecked.value}"` |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
lastValues += ',' |
203
|
|
|
}) |
204
|
|
View Code Duplication |
lastValues = lastValues.substring(0, lastValues.length - 1) |
|
|
|
|
205
|
|
|
lastValues += ']' |
206
|
|
|
target.setAttribute('last-values', lastValues) |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// var blockContent = IframeNode('#page-template', '.select-' + attr.id)[0] |
211
|
|
|
|
212
|
|
|
var nodeComments = IframeCommentNode('#page-template', attr.id) |
213
|
|
|
|
214
|
|
|
if(typeof nodeComments !== 'undefined' && nodeComments !== null && nodeComments.length > 0) { |
215
|
|
|
var checked = e.target.querySelectorAll('option:checked') |
216
|
|
|
var json = this._json.data |
217
|
|
|
|
218
|
|
|
json[attr.id] = [] |
219
|
|
|
Array.prototype.forEach.call(checked, (check) => { |
220
|
|
|
if(check.value !== '') { |
221
|
|
|
if(check.value.indexOf('{') > -1 || check.value.indexOf('[') > -1) { |
222
|
|
|
json[attr.id].push(JSON.parse(check.value)) |
223
|
|
|
}else { |
224
|
|
|
json[attr.id].push(check.value) |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
}) |
228
|
|
|
|
229
|
|
|
this._json.data = json |
230
|
|
|
|
231
|
|
|
try { |
232
|
|
|
Array.prototype.forEach.call(nodeComments, (nodeComment) => { |
233
|
|
|
var blockHtml = unescape(nodeComment.textContent.replace(/\[\[([\S\s]*?)\]\]/, '')).replace(/\[0\]-/g, '[0]-') |
234
|
|
|
|
235
|
|
|
// var blockHtml = unescape(blockContent.innerHTML).replace(/\[0\]-/g, '[0]-') |
236
|
|
|
var template = Handlebars.compile(blockHtml, {noEscape: true}) |
237
|
|
|
var compiled = template(this._json.data) |
238
|
|
|
|
239
|
|
|
nodeComment.parentNode.innerHTML = compiled + `<!-- ${nodeComment.textContent} -->` |
240
|
|
|
}) |
241
|
|
|
} catch(e) { |
242
|
|
|
console.log(e) |
|
|
|
|
243
|
|
|
} |
244
|
|
|
}else if(typeof attr.id !== 'undefined' && attr.id !== null) { |
245
|
|
|
var nodes = EditorUtils.getNode(attr) |
246
|
|
|
Array.prototype.forEach.call(nodes, (node) => { |
247
|
|
|
EditorUtils.formToHtml(node, target) |
248
|
|
|
}) |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
Array.prototype.forEach.call(options, function(option) { |
252
|
|
|
option.disabled = false |
253
|
|
|
}) |
254
|
|
|
} |
255
|
|
|
} |