forked from Hube2/acf-input-counter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacf-input-counter.js
executable file
·339 lines (306 loc) · 15 KB
/
acf-input-counter.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
(function ($) {
$(document).ready(function () {
/**
* Sync conters with qtranslatex language switchers.
*/
$('body').on('click', '.qtranxs-lang-switch', function () {
var parent = $('.multi-language-field'), language = $(this).attr('lang');
// this is for qtranslate_text and qtranslate_textarea fields
parent.find('input[data-language="' + language + '"], textarea[data-language="' + language + '"]').trigger('input');
// this is for qtranslate_wysiwyg field
parent.find('textarea[name$="[' + language + ']"]').trigger('change');
});
});
/**
* default acf.fields.wysiwyg initialization replace the php generated html whit a new one with nue uniq id
* the above mentioned replacement prevent qtranslate content hook removal fron related textarea
* (because the new id didn't match the id stored in qtranslate initial configuration)
*
* solution: redefine acf.fields.wysiwyg.initialize function where replacement lines are simply commented out to prevent replacement
* all works fine with wysiwyg inside a repeater field
* (more investigations will be needed to check how it works with clone field or flexible content field)
*
*/
acf.fields.wysiwyg.initialize = function () {
// bail early if delay
if (this.$el.hasClass('delay'))
return;
// bail early if no tinyMCEPreInit (needed by both tinymce and quicktags)
if (typeof tinyMCEPreInit === 'undefined')
return;
// generate new id
var old_id = this.o.id,
new_id = acf.get_uniqid('acf-editor-'),
html = this.$el.outerHTML();
// replace
html = acf.str_replace(old_id, new_id, html);
/**
* swapping of this.$el and change of this.o.id needs to be prevented to remove qTranslate contentHook for default acf wysiwyg field textarea
* so we need to comment it out
*/
// swap
//this.$el.replaceWith(html);
// update id
//this.o.id = new_id
// initialize
this.initialize_tinymce();
this.initialize_quicktags();
}
// Remove content hooks from standard wysiwyg field
acf.add_filter('wysiwyg_tinymce_settings', function (mceInit, id) {
if (typeof qTranslateConfig == 'object') {
qTranslateConfig.qtx.removeContentHook(mceInit);
$('#' + id).removeClass('qtranxs-translatable');
}
// do something to mceInit
mceInit.setup = function (ed) {
/**
* check counter status before insert new char
* on maxlength reached allows backspace, delete and copy/cut keybord shortcuts
*/
ed.on('keyDown', function (e) {
// console.log(e);
var $max = $(ed.getElement()).attr("maxlength");
if (typeof ($max) == 'undefined') {
return;
}
var $value = ed.getContent();
/**
* remove html tags with regexpr to be sure the length is calculated only by visible characters
*/
var $length = acf.decode($value.replace(/<\/?[^>]+(>|$)/g, "")).length;
if ($length >= $max
&& e.keyCode != 8 // backspace
&& e.keyCode != 46 // delete
&& (e.ctrlKey && e.keyCode != 88) // ctrl+x
&& (e.ctrlKey && e.keyCode != 67) // ctrl+c
) {
tinymce.dom.Event.cancel(e);
}
});
/**
* update counter
*/
ed.on('keyUp', function (e) {
var $max = $(ed.getElement()).attr("maxlength");
if (typeof ($max) == 'undefined') {
return;
}
var $value = ed.getContent();
/**
* remove html tags with regexpr to be sure the length is calculated only by visible characters
*/
var $length = acf.decode($value.replace(/<\/?[^>]+(>|$)/g, "")).length;
$(ed.getElement()).closest('.acf-input').find('.count').text($length);
});
};
// return
return mceInit;
});
acf.change_count_text = function (e) {
var $max = e.$el.attr('maxlength');
if (typeof ($max) == 'undefined' || e.$el.closest('.acf-input').find('.count').length == 0) {
return;
}
var $value = e.$el.val();
var $length = $value.length;
e.$el.closest('.acf-input').find('.count').text($length);
}
acf.change_count_textarea = function (e) {
var $max = e.$el.attr('maxlength');
if (typeof ($max) == 'undefined') {
return;
}
var $value = e.$el.val();
var $length = $value.length;
e.$el.closest('.acf-input').find('.count').text($length);
}
acf.refresh_hidden = function (e) {
var self = $(e.$field);
self.$el = self.find('.acf-input');
/**
* if qTranslete is enabled get the active language
*/
var active_lang = typeof qTranslateConfig == 'object' ? qTranslateConfig.activeLanguage : false;
switch (e.type) {
case 'text':
case 'qtranslate_text':
/**
* this is needed if the field is hidden by tab layout
*/
active_lang ? self.$el.find('input[data-language="' + active_lang + '"]').trigger('input') : self.$el.find('input').trigger('input');
break;
case 'textarea':
case 'qtranslate_textarea':
/**
* this is needed if the field is hidden by tab layout
*/
active_lang ? self.$el.find('textarea[data-language="' + active_lang + '"]').trigger('input') : self.$el.find('textarea').trigger('input');
break;
}
}
acf.check_maxlength = function (e) {
var self = $(e.$field);
// get element
self.$el = self.find('.acf-input');
/**
* timeout is needed to wait for tinymce/quicktags activation
*/
setTimeout(function () {
self.$textarea = self.$el.find('textarea');
if (!self.$el.attr('maxlength')) {
var ajax_data = acf.prepare_for_ajax({
action: 'get_maxlength',
field_key: acf.get_data(self.$el.parent(), 'key')
});
// get maxlength
$.ajax({
url: acf.get('ajaxurl'),
dataType: 'json',
type: 'post',
data: ajax_data,
success: function (json) {
if (json.success) {
//console.info(json, self.$el);
self.$textarea.attr('maxlength', json.data.maxlength);
} else {
console.warn(json, self.$el);
}
},
complete: function () {
/**
* if qTranslete is enabled get the active language
*/
var active_lang = typeof qTranslateConfig == 'object' ? qTranslateConfig.activeLanguage : false;
/**
* this is needed if the field is hidden by tab layout
*/
active_lang ? self.$el.find('textarea[name$="[' + active_lang + ']"]').trigger('change') : self.$textarea.trigger('change');
}
});
}
}, 1);
}
acf.fields.text_counter = acf.field.extend({
type: 'text',
actions: {
'load': 'refresh_hidden',
},
events: {
'input input': 'change_count',
'focus input': 'change_count',
},
change_count: function (e) {
acf.change_count_text(e)
},
refresh_hidden: function () {
acf.refresh_hidden(this)
}
});
acf.fields.qtranslate_text_counter = acf.field.extend({
type: 'qtranslate_text',
actions: {
'load': 'refresh_hidden',
},
events: {
'input input': 'change_count',
'focus input': 'change_count',
},
change_count: function (e) {
acf.change_count_text(e)
},
refresh_hidden: function () {
acf.refresh_hidden(this)
}
});
acf.fields.textarea_counter = acf.field.extend({
type: 'textarea',
actions: {
'load': 'refresh_hidden',
},
events: {
'input textarea': 'change_count',
'focus textarea': 'change_count',
},
change_count: function (e) {
acf.change_count_textarea(e)
},
refresh_hidden: function () {
acf.refresh_hidden(this)
}
});
acf.fields.qtranslate_textarea_counter = acf.field.extend({
type: 'qtranslate_textarea',
actions: {
'load': 'refresh_hidden',
},
events: {
'input textarea': 'change_count',
'focus textarea': 'change_count',
},
change_count: function (e) {
acf.change_count_textarea(e)
},
refresh_hidden: function () {
acf.refresh_hidden(this)
}
});
acf.fields.wysiwyg_counter = acf.field.extend({
type: 'wysiwyg',
actions: {
'load': 'check_maxlength',
},
events: {
'input textarea': 'change_count',
'focus textarea': 'change_count',
'change .wp-editor-area': 'change_count',
'click .wp-media-buttons button': 'check_editor',
},
check_maxlength: function () {
acf.check_maxlength(this)
},
change_count: function (e) {
acf.change_count_textarea(e)
},
check_editor: function (e) {
/**
* force triggering "change" event on tinymce/quicktags switching
*/
$(tinyMCE.activeEditor.targetElm).trigger('change').focus();
}
});
acf.fields.qtranslate_wysiwyg_counter = acf.field.extend({
type: 'qtranslate_wysiwyg',
actions: {
'load': 'check_maxlength',
},
events: {
'input textarea': 'change_count',
'focus textarea': 'change_count',
'change .qtx-wp-editor-area': 'change_count',
'click .wp-editor-tabs button, a.wp-switch-editor': 'check_editor',
},
check_maxlength: function () {
acf.check_maxlength(this)
},
change_count: function (e) {
acf.change_count_textarea(e)
},
check_editor: function (e) {
/**
* timeout is needed to wait for tinymce/quicktags switching
*/
setTimeout(function () {
var tinymceActive = (typeof tinyMCE !== 'undefined') && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
/* Check if TinyMCE is active */
if (tinymceActive) {
var id = $(tinyMCE.activeEditor.container.closest('.multi-language-field')).find('.acf-editor-wrap.current-language textarea').attr('id');
if (tinymce.get(id))
tinymce.get(id).fire('keyUp');
} else {
$('.qtx-wp-editor-area').trigger('change').focus();
}
}, 100);
}
});
})(jQuery);