Files
Grav-Admin-Plugin/themes/grav/js/forms/fields/array.js

151 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-06-01 21:22:56 +02:00
(function () {
var root = window || {};
root = root.GravJS = root.GravJS || {};
root = root.FormFields = root.FormFields || {};
var ArrayField = function (el, form) {
el = $(el);
this.el = el.is('[' + form.fieldIndicator + ']') ? el : el.closest('[' + form.fieldIndicator + ']');
this.el.on('click', '[data-grav-array-action="add"]', this.add.bind(this));
this.el.on('click', '[data-grav-array-action="rem"]', this.remove.bind(this));
this.el.on('keyup', '[data-grav-array-type="key"]', this.update.bind(this));
};
ArrayField.getName = function () {
return 'array';
};
ArrayField.getTypes = function () {
return [ 'array' ];
};
ArrayField.prototype.valid = function() {
return true;
};
ArrayField.prototype.disabled = function() {
return false;
};
ArrayField.prototype.name = function(name) {
2015-08-03 12:33:03 -07:00
if (name && !this.isValueOnly()) {
2015-06-01 21:22:56 +02:00
this.el.data('grav-array-name', name);
return name;
2015-08-03 12:33:03 -07:00
} else {
return '';
2015-06-01 21:22:56 +02:00
}
return this.el.data('grav-array-name')
};
ArrayField.prototype.isValueOnly = function() {
return this.el.find('[data-grav-array-mode="value_only"]').length;
};
2015-06-01 21:22:56 +02:00
ArrayField.prototype.value = function(val) {
if (typeof val === 'object') {
// Remove old
this.el.find('[data-grav-array-type="row"]').remove();
var container = this.el.find('[data-grav-array-type="container"]');
for (var key in val) { if (val.hasOwnProperty(key)) {
container.append(this._getNewField(key, val[key]));
}
}
return val;
}
var values = {};
this.el.find('[data-grav-array-type="value"]').each(function () {
var key = $(this).attr('name'),
value = $(this).val();
values[key] = value;
});
return values;
};
ArrayField.prototype.reset = function() {
this.value('');
};
ArrayField.prototype.formValues = function() {
var values = this.value(),
name = this.name(),
formValues = {};
for (var key in values) { if (values.hasOwnProperty(key)) {
2015-08-03 12:33:03 -07:00
formValues[this.isValueOnly() ? key : name + '[' + key + ']'] = values[key];
2015-06-01 21:22:56 +02:00
}
}
return formValues;
};
ArrayField.prototype.add = function() {
$(this._getNewField()).insertAfter($(event.target).closest('[data-grav-array-type="row"]'));
if (this.isValueOnly()) {
this.refreshAll();
}
2015-06-01 21:22:56 +02:00
};
ArrayField.prototype.remove = function() {
$(event.target).closest('[data-grav-array-type="row"]').remove();
if (this.isValueOnly()) {
this.refreshAll();
}
2015-06-01 21:22:56 +02:00
};
ArrayField.prototype.update = function() {
var keyField = $(event.target),
valueField = keyField.closest('[data-grav-array-type="row"]').find('[data-grav-array-type="value"]');
2015-08-03 12:33:03 -07:00
valueField.attr('name', this.getFieldName() + '[' + keyField.val() + ']');
2015-06-01 21:22:56 +02:00
};
ArrayField.prototype.refreshAll = function() {
2015-08-03 12:33:03 -07:00
var that = this;
this.el.find('[data-grav-array-type="value"]').each(function(index, element){
2015-08-03 12:33:03 -07:00
$(element).attr('name', that.getFieldName() + '[' + index + ']');
});
};
2015-08-03 12:33:03 -07:00
ArrayField.prototype.getFieldName = function(element) {
return this.el.data('grav-array-name');
};
2015-06-01 21:22:56 +02:00
ArrayField.prototype._getNewField = function(key, value) {
var name = this.name(),
value_only = this.isValueOnly(),
2015-06-01 21:22:56 +02:00
placeholder = {
key: this.el.data('grav-array-keyname') || 'Key',
val: this.el.data('grav-array-valuename') || 'Value'
};
key = key || '';
value = value || '';
var output;
if (value_only) {
output = '<div class="form-row array-field-value_only" data-grav-array-type="row">' + "\n" +
'<input data-grav-array-type="value" type="text" value="' + value + '" placeholder="' + placeholder.val + '" />' + "\n";
} else {
output = '<div class="form-row" data-grav-array-type="row">' + "\n" +
'<input data-grav-array-type="key" type="text" value="' + key + '" placeholder="' + placeholder.key + '" />' + "\n" +
'<input data-grav-array-type="value" type="text" name="' + key + '" value="' + value + '" placeholder="' + "\n" + placeholder.val + '" />';
}
output += '<span data-grav-array-action="rem" class="fa fa-minus"></span>' + "\n" +
'<span data-grav-array-action="add" class="fa fa-plus"></span>' + "\n" +
'</div>';
return output;
2015-06-01 21:22:56 +02:00
};
root.Array = ArrayField;
})();