Skip to content

Commit a2d0b82

Browse files
author
Eugene Tupikov
committed
Merge remote-tracking branch 'origin/master'
2 parents 7c02a4e + 85f4588 commit a2d0b82

File tree

4 files changed

+123
-94
lines changed

4 files changed

+123
-94
lines changed

gulpfile.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
var gulp = require('gulp'),
2-
uglify = require('gulp-uglify'),
3-
concat = require('gulp-concat'),
4-
rename = require('gulp-rename');
1+
const {gulp, src, dest} = require('gulp');
2+
const uglify = require('gulp-uglify-es').default;
3+
const concat = require('gulp-concat');
4+
const rename = require('gulp-rename');
55

6-
gulp.task('js', function () {
6+
function build () {
77
var path = './src/assets/src/js/';
8-
9-
return gulp.src([
8+
9+
return src([
1010
path + 'jquery.multipleInput.js'
1111
])
1212
.pipe(concat('jquery.multipleInput.js'))
1313
.pipe(uglify())
1414
.pipe(rename({suffix: '.min'}))
15-
.pipe(gulp.dest(path));
16-
});
17-
18-
gulp.task('default', ['js']);
15+
.pipe(dest(path));
16+
};
17+
18+
exports.default = build;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
},
2323
"homepage": "https://github.com/unclead/yii2-multiple-input",
2424
"devDependencies": {
25-
"gulp": "^3.8.8",
26-
"gulp-uglify": "^1.0.1",
25+
"gulp": "^4.0.2",
2726
"gulp-concat": "^2.4.1",
28-
"gulp-rename": "^1.2.0"
27+
"gulp-rename": "^1.4.0",
28+
"gulp-uglify-es": "3.0.0"
2929
}
3030
}

src/assets/src/js/jquery.multipleInput.js

Lines changed: 108 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
$wrapper.on('click.multipleInput', '.js-input-clone', function (e) {
159159
e.stopPropagation();
160-
addInput($(this), getRowValues($(this)));
160+
cloneInput($(this));
161161
});
162162

163163
var i = 0,
@@ -196,10 +196,10 @@
196196
$wrapper.data('multipleInput').settings = settings;
197197

198198
$wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
199-
addAttribute($(this));
199+
addActiveFormAttribute($(this));
200200
});
201201

202-
$wrapper.data('multipleInput').currentIndex = getCurrentIndex($wrapper);
202+
$wrapper.data('multipleInput').currentIndex = getCurrentRowsCount($wrapper);
203203
isActiveFormEnabled = true;
204204

205205
clearInterval(intervalID);
@@ -212,7 +212,7 @@
212212
// If after a second system could not detect ActiveForm it means
213213
// that widget is used without ActiveForm and we should just complete initialization of the widget
214214
if (form.length === 0 || i > 10) {
215-
$wrapper.data('multipleInput').currentIndex = getCurrentIndex($wrapper);
215+
$wrapper.data('multipleInput').currentIndex = getCurrentRowsCount($wrapper);
216216
isActiveFormEnabled = false;
217217

218218
clearInterval(intervalID);
@@ -260,92 +260,134 @@
260260
}
261261
};
262262

263-
var addInput = function (btn, values) {
264-
var $wrapper = $(btn).closest('.multiple-input').first(),
265-
data = $wrapper.data('multipleInput'),
266-
settings = data.settings,
267-
template = settings.template,
268-
inputList = $wrapper.children('.multiple-input-list').first();
263+
var cloneInput = function (btn) {
264+
let $wrapper = $(btn).closest('.multiple-input').first();
265+
let data = $wrapper.data('multipleInput');
266+
let settings = data.settings;
267+
268+
let values = {};
269+
270+
btn.closest('.multiple-input-list__item').find('input, select, textarea').each(function (k, v) {
271+
let $element = $(v);
272+
273+
let id = getInputId($element);
274+
if (id) {
275+
let columnName = id.replace(settings.inputId, '').replace(/-\d+-/, '');
276+
277+
if ($element.is(':checkbox')) {
278+
if (!values.hasOwnProperty(columnName)) {
279+
values[columnName] = [];
280+
}
281+
282+
if ($element.is(':checked')) {
283+
values[columnName].push($element.val());
284+
}
285+
} else {
286+
values[columnName] = $element.val();
287+
}
288+
}
289+
});
269290

270-
if (settings.max !== null && getCurrentIndex($wrapper) >= settings.max) {
291+
addInput(btn, values);
292+
}
293+
294+
var addInput = function (btn, rowValues) {
295+
rowValues = rowValues || {};
296+
297+
let $wrapper = $(btn).closest('.multiple-input').first();
298+
let data = $wrapper.data('multipleInput');
299+
let settings = data.settings;
300+
let inputList = $wrapper.children('.multiple-input-list').first();
301+
302+
if (settings.max !== null && getCurrentRowsCount($wrapper) >= settings.max) {
271303
return;
272304
}
273305

274-
template = replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex, template);
275-
var $addedInput = $(template);
276-
var currentIndex = data.currentIndex;
306+
let currentIndex = data.currentIndex;
307+
308+
let template = replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex, settings.template);
309+
let $newRow = $(template);
277310

278311
var beforeAddEvent = $.Event(events.beforeAddRow);
279-
$wrapper.trigger(beforeAddEvent, [$addedInput, currentIndex]);
280312

313+
$wrapper.trigger(beforeAddEvent, [$newRow, currentIndex]);
281314
if (beforeAddEvent.result === false) {
282315
return;
283316
}
284317

318+
$newRow.find('input, select, textarea').each(function (index, element) {
319+
let $element = $(element);
285320

286-
if (settings.prepend) {
287-
$addedInput.hide().prependTo(inputList).fadeIn(300);
288-
} else {
289-
$addedInput.hide().appendTo(inputList).fadeIn(300);
290-
}
321+
let id = getInputId($element);
322+
if (id) {
323+
let columnName = id.replace(settings.inputId, '').replace(/-\d+-/, '');
291324

292-
if (values instanceof Object) {
293-
var tmp = [];
294-
for (var key in values) {
295-
if (values.hasOwnProperty(key)) {
296-
tmp.push(values[key]);
297-
}
298-
}
325+
if (rowValues.hasOwnProperty(columnName)) {
326+
let tag = $element.get(0).tagName;
299327

300-
values = tmp;
301-
}
328+
let inputValue = rowValues[columnName];
302329

303-
var jsTemplate;
330+
switch (tag) {
331+
case 'INPUT':
332+
if ($element.is(':checkbox')) {
333+
if (inputValue.indexOf($element.val()) !== -1) {
334+
$element.prop('checked', true);
335+
}
336+
} else {
337+
$element.val(inputValue);
338+
}
304339

305-
for (var i in settings.jsTemplates) {
306-
jsTemplate = settings.jsTemplates[i];
307-
jsTemplate = replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex, jsTemplate);
308-
jsTemplate = replaceAll('%7B' + settings.indexPlaceholder + '%7D', data.currentIndex, jsTemplate);
340+
break;
309341

310-
window.eval(jsTemplate);
311-
}
342+
case 'TEXTAREA':
343+
$element.val(inputValue);
344+
break;
312345

313-
var index = 0;
314-
315-
$(template).find('input, select, textarea').each(function (k, v) {
316-
var ele = $(v),
317-
tag = v.tagName,
318-
id = getInputId(ele),
319-
obj = $('#' + id);
320-
321-
if (values) {
322-
var val = values[index];
323-
324-
if (tag === 'INPUT' || tag === 'TEXTAREA') {
325-
obj.val(val);
326-
} else if (tag === 'SELECT') {
327-
if (val && val.indexOf('option') !== -1) {
328-
obj.append(val);
329-
} else {
330-
var option = obj.find('option[value="' + val + '"]');
331-
if (option.length) {
332-
obj.val(val);
333-
}
346+
case 'SELECT':
347+
if (inputValue && inputValue.indexOf('option') !== -1) {
348+
$element.append(inputValue);
349+
} else {
350+
var option = $element.find('option[value="' + inputValue + '"]');
351+
if (option.length) {
352+
$element.val(inputValue);
353+
}
354+
}
355+
356+
break;
357+
358+
default:
359+
break;
334360
}
335361
}
336362
}
337363

364+
338365
if (isActiveFormEnabled) {
339-
addAttribute(ele);
366+
addActiveFormAttribute($element);
340367
}
341-
342-
index++;
343368
});
344369

370+
// display the new row
371+
if (settings.prepend) {
372+
$newRow.hide().prependTo(inputList).fadeIn(300);
373+
} else {
374+
$newRow.hide().appendTo(inputList).fadeIn(300);
375+
}
376+
377+
// apply js templates
378+
let jsTemplate = null;
379+
for (var i in settings.jsTemplates) {
380+
jsTemplate = settings.jsTemplates[i];
381+
jsTemplate = replaceAll('{' + settings.indexPlaceholder + '}', currentIndex, jsTemplate);
382+
jsTemplate = replaceAll('%7B' + settings.indexPlaceholder + '%7D', currentIndex, jsTemplate);
383+
384+
window.eval(jsTemplate);
385+
}
386+
345387
$wrapper.data('multipleInput').currentIndex++;
346388

347389
var afterAddEvent = $.Event(events.afterAddRow);
348-
$wrapper.trigger(afterAddEvent, [$addedInput, currentIndex]);
390+
$wrapper.trigger(afterAddEvent, [$newRow, currentIndex]);
349391
};
350392

351393
var removeInput = function ($btn) {
@@ -354,7 +396,7 @@
354396
data = $wrapper.data('multipleInput'),
355397
settings = data.settings;
356398

357-
var currentIndex = getCurrentIndex($wrapper);
399+
var currentIndex = getCurrentRowsCount($wrapper);
358400
if (currentIndex > settings.min) {
359401
var event = $.Event(events.beforeDeleteRow);
360402
$wrapper.trigger(event, [$toDelete, currentIndex]);
@@ -365,7 +407,7 @@
365407

366408
if (isActiveFormEnabled) {
367409
$toDelete.find('input, select, textarea').each(function (index, ele) {
368-
removeAttribute($(ele));
410+
removeActiveFormAttribute($(ele));
369411
});
370412
}
371413

@@ -383,7 +425,7 @@
383425
*
384426
* @param input
385427
*/
386-
var addAttribute = function (input) {
428+
var addActiveFormAttribute = function (input) {
387429
var id = getInputId(input);
388430

389431
// skip if we could not get an ID of input
@@ -435,7 +477,7 @@
435477
/**
436478
* Removes an attribute from ActiveForm.
437479
*/
438-
var removeAttribute = function (ele) {
480+
var removeActiveFormAttribute = function (ele) {
439481
var id = getInputId(ele);
440482

441483
if (id === null) {
@@ -463,27 +505,14 @@
463505
return id;
464506
};
465507

466-
var getCurrentIndex = function($wrapper) {
508+
var getCurrentRowsCount = function($wrapper) {
467509
return $wrapper
468510
.find('.multiple-input-list .multiple-input-list__item')
469511
.filter(function(){
470512
return $(this).parents('.multiple-input').first().attr('id') === $wrapper.attr('id');
471513
}).length;
472514
};
473515

474-
var getRowValues = function (element) {
475-
var values = {};
476-
element.closest('tr').find('td').each(function (index, value) {
477-
$(value).find('input, select, textarea').each(function (k, v) {
478-
var ele = $(v),
479-
id = getInputId(ele),
480-
obj = $('#' + id);
481-
values[id] = obj.val();
482-
});
483-
});
484-
return values;
485-
};
486-
487516
var replaceAll = function (search, replace, subject) {
488517
if (!(subject instanceof String) && typeof subject !== 'string') {
489518
console.warn('Call replaceAll for non-string value: ' + subject);

0 commit comments

Comments
 (0)