-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectizeImageTags.js
66 lines (50 loc) · 1.92 KB
/
SelectizeImageTags.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
/**
* Selectize
* ============================================
*/
function initSelectizeImageTags() {
for(var name in config.SelectizeImageTags) {
// get inputfields
var thisTagsFields = $("." + name);
// get inputfields inside repeaters
var thisRepeaterTagsFields = $("li[class*='" + name + "_repeater']").filter(function() {
var re = new RegExp("\\b" + name + "_repeater");
return this.className.match(re);
});
// get the inputs inside those inputfields
var thisTagsInputs = thisTagsFields.add(thisRepeaterTagsFields).find("input[name^='tags_']");
// at this point, name is our fieldName, and field holds the selectize init object
var field = config.SelectizeImageTags[name];
if(field.create == true) {
// each image has it's own tags field...
thisTagsInputs.each(function() {
var thisTagsField = $(this);
var thisTags = thisTagsField.val();
if(thisTags) {
var thisTagsArr = thisTags.split(" ");
$(thisTagsArr).each(function(i, ttag) {
if(lookup(field.options, "value", ttag)) return;
field.options.push ({ "value": ttag, "text": ttag});
});
}
});
} // end if value.create
thisTagsInputs.selectize(field);
}
}
function lookup(array, prop, value) {
for (var i = 0, len = array.length; i < len; i++)
if (array[i] && array[i][prop] === value) return array[i];
}
// Init on DOM ready
$(document).ready(function () {
initSelectizeImageTags();
});
// Init on AJAX-loading of image field
$(document).on('reloaded', '.InputfieldImage', function() {
initSelectizeImageTags();
});
// Init on image uploaded
$(document).on('AjaxUploadDone', '.gridImages', function() {
initSelectizeImageTags();
});