-
Notifications
You must be signed in to change notification settings - Fork 21
/
inlines.js
145 lines (130 loc) · 5.24 KB
/
inlines.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
function startDynamicInlines() {
let inlineFormSelector = '[data-inline-form]';
hideFields(inlineFormSelector);
deleteButton(inlineFormSelector);
checkMaxNum(inlineFormSelector);
$('[js-data-inline-button]').off().on('click', function(e){
e.preventDefault();
let max_num = this.getAttribute('data-inline-max-num');
let $parent = $(this).parent();
let $inlineForm = $parent.find(inlineFormSelector);
inlineForm = $inlineForm[$inlineForm.length -1];
selectizes = checkSelectizes();
let $clonedInline = $(inlineForm).clone().removeClass('hide');
// Change all the atributes in the hidden for for the next inline
let $nameSet = $(inlineForm).find('input[name*="_set-"]');
let $idSet = $(inlineForm).find('input[id*="_set-"]');
let $forSet = $(inlineForm).find('[for*="_set-"]');
let setNameArray = $nameSet[0].getAttribute('name').split('_');
let setName = setNameArray[0];
// find total number and increment
let $totalForm = $('input[name=' + setName + '_set-TOTAL_FORMS]');
let number = $totalForm.val();
// Find all id, names and for fields and replace the content with a +1 increment
changeFields($nameSet, 'name', number);
changeFields($idSet, 'id', number);
changeFields($forSet, 'for', number);
$totalForm.val(parseInt(number) + 1);
$(inlineForm).prev().append($clonedInline);
if (selectizes) {
startAllSelectizes();
}
deleteButton(inlineFormSelector);
startAllPickers();
startSortInlines();
floatLabels();
betterFile();
// check max number
if (max_num == $inlineForm.length) {
$(this).hide();
}
});
function deleteButton(inlineFormSelector) {
//Hide all delete checkboxes and bind the button
let deleteButtonSelector = '[js-inline-delete]';
let deleteInput = 'input[name*="DELETE"]';
$(deleteButtonSelector).off().on('click', function() {
$parent = $(this).closest(inlineFormSelector);
$deleteCheckbox = $parent.find(deleteInput);
$deleteCheckbox[0].checked = true;
$parent.addClass('hide');
});
}
function changeFields($set, type, number) {
let currentNumberAttr = '-' + (number - 1) +'-';
let newNumberAttr = '-' + number +'-';
for(let i=0, len = $set.length; i < len; i++) {
item = $set[i];
attribute = item.getAttribute(type);
attribute = attribute.replace(currentNumberAttr, newNumberAttr);
item.setAttribute(type, attribute);
}
}
function checkSelectizes() {
$selectize = $(inlineForm).find('[js-selectize-multiple], [js-selectize-autocomplete], [js-selectize]');
if ($selectize.length) {
for(let i=0, len = $selectize.length; i < len; i++) {
let selectize = $selectize[i];
if (selectize.selectize) {
let value = $(selectize).val();
selectize.selectize.destroy();
$(selectize).val(value);
}
}
return true
}
return false
}
function hideFields(inlineFormSelector) {
// hides delete
let deleteInput = 'input[name*="DELETE"]';
let $delete$Fields = $(inlineFormSelector).find(deleteInput);
$delete$Fields.closest('.row').addClass('hide');
}
function checkMaxNum(inlineFormSelector) {
// Check if the form already reached the max number of fields
let $addButton = $('[js-data-inline-button]');
for(let i = 0, len = $addButton.length; i < len; i++) {
let button = $addButton[i];
let $parent = $(button).parent();
let $inlineForm = $parent.find(inlineFormSelector);
let max_num = button.getAttribute('data-inline-max-num');
if (max_num == $inlineForm.length) {
$(button).hide();
}
}
}
}
function checkOrder(fields) {
if (fields.length) {
for(let i=0, len = fields.length; i < len; i++) {
field = fields[i];
field.value = i;
}
}
}
function startSortInlines() {
// check if sort is enebale if so set the order on init
$('[data-sorting-field]').each(function(index) {
// Hide order field
let orderFieldName = this.getAttribute('data-sorting-field');
let orderInput = 'input[name*="-' + orderFieldName + '"]';
let orderFields = this.querySelectorAll(orderInput);
$(orderFields).closest('.row').addClass('hide');
checkOrder(orderFields);
Sortable.create(this, {
handle: '.inline-form-grab',
onUpdate: function (e) {
let el = e.item.parentElement;
let orderFieldName = el.getAttribute('data-sort-field');
let orderInput = 'input[name*="-' + orderFieldName + '"]';
let orderFields = el.querySelectorAll('input[name*="-order"]');
checkOrder(orderFields);
}
});
});
}
function startAllInlines() {
startDynamicInlines();
startSortInlines();
}