automizy-js
Version:
Automizy widget collection
233 lines (226 loc) • 7.56 kB
JavaScript
define([
'automizy/core',
'automizy/button',
'automizy/input',
'automizy/addons/jqueryAddOns',
'automizy/functions/getUniqueString'
], function () {
var Form = function (obj) {
var t = this;
t.d = {
$widget: $('<form class="automizy-form"></form>'),
buttons: [],
inputs: [],
subtitles: [],
htmls: [],
hasObject: false,
id: 'automizy-form-' + $A.getUniqueString(),
create: function () {
},
method: 'POST',
enctype: false,
url: document.location.href
};
t.d.$widget.attr('id', t.id());
if (typeof obj !== 'undefined') {
if (typeof obj.buttons !== 'undefined')
t.buttons(obj.buttons);
if (typeof obj.inputs !== 'undefined')
t.inputs(obj.inputs);
if (typeof obj.htmls !== 'undefined')
t.htmls(obj.htmls);
if (typeof obj.target !== 'undefined')
t.drawTo(obj.target);
if (typeof obj.create === 'function')
t.create(obj.create);
if (typeof obj.method !== 'undefined')
t.method(obj.method);
if (typeof obj.enctype !== 'undefined')
t.enctype(obj.enctype);
if (typeof obj.url !== 'undefined' || typeof obj.action !== 'undefined')
t.url(obj.url || obj.action);
}
};
var p = Form.prototype;
p.subTitle = p.addSubTitle = function (text) {
var t = this;
var id = "automizy-form-subtitle-" + $A.getUniqueString();
if (typeof text === 'string') {
t.d.subtitles.push({id: id, text: text});
$('<div id="' + id + '" class="automizy-form-subtitle"></div>').html(text).appendTo(t.d.$widget);
} else {
console.warn('Bad parameter type.', text);
}
return t;
};
p.removeSubTitle = function (subTitle) {
var t = this;
if (typeof subTitle === 'string') {
for (var i = 0; i < t.d.subtitles.length; i++) {
if (t.d.subtitles[i].id === subTitle)
t.d.subtitles[i].remove();
}
} else if (typeof subTitle === 'object') {
subTitle.remove();
}
};
p.button = p.addButton = p.addButtonTo = function (obj) {
var t = this;
if (typeof obj !== 'undefined') {
obj.target = obj.target || t.d.$widget;
var button = $A.newButton(obj);
t.d.buttons.push(button);
return t;
}
var button = $A.newButton();
t.d.buttons.push(button);
button.drawTo(t.d.$widget);
return button;
};
p.removeButton = function (button) {
var t = this;
if (typeof button === 'string') {
for (var i = 0; i < t.d.buttons.length; i++) {
if (t.d.buttons[i].id === button)
t.d.buttons[i].remove();
}
} else if (typeof button === 'object') {
button.remove();
}
};
p.buttons = p.addButtons = function (buttons) {
var t = this;
if (typeof buttons !== 'undefined') {
for (var i in buttons) {
t.addButton(buttons[i]);
}
return t;
}
return t.d.buttons;
};
p.input = p.addInput = p.addInputTo = function (obj) {
var t = this;
if (typeof obj !== 'undefined') {
if (obj instanceof $A.m.Input) {
if (t.d.hasObject === false)
obj.drawTo(t.d.$widget);
} else {
obj.target = obj.target || t.d.$widget;
var input = $A.newInput(obj);
t.d.inputs.push(input);
}
return t;
}
var input = $A.newInput();
t.d.inputs.push(input);
input.drawTo(t.d.$widget);
return input;
};
p.removeInput = function (input) {
var t = this;
if (typeof input === 'string') {
for (var i = 0; i < t.d.inputs.length; i++) {
if (t.d.inputs[i].id === input)
t.d.inputs[i].remove();
}
} else if (typeof input === 'object') {
input.remove();
}
};
p.inputs = p.addInputs = function (inputs) {
var t = this;
if (typeof inputs !== 'undefined') {
for (var i in inputs) {
t.addInput(inputs[i]);
}
return t;
}
return t.d.inputs;
};
p.method = function (method) {
var t = this;
if (typeof method !== 'undefined') {
t.d.method = method;
t.widget().attr('method', method);
return t;
}
return t.d.method;
};
p.enctype = function (enctype) {
var t = this;
if (typeof enctype !== 'undefined') {
t.d.enctype = enctype;
t.widget().attr('enctype', enctype);
return t;
}
return t.d.enctype;
};
p.url = p.action = function (url) {
var t = this;
if (typeof url !== 'undefined') {
t.d.url = url;
t.widget().attr('action', url);
return t;
}
return t.d.url;
}
p.submit = function () {
var t = this;
t.widget().submit();
}
p.htmls = p.addHtmls = function (htmls) {
var t = this;
if (typeof htmls !== 'undefined') {
for (var i in htmls) {
t.addHtml(htmls[i]);
}
return t;
}
return t.d.htmls;
};
p.html = p.addHtml = p.addHtmlTo = function (html) {
var t = this;
if (typeof html !== 'undefined') {
var id = "automizy-form-html-" + $A.getUniqueString();
var $htmlBox = $('<span id="' + id + '" class="automizy-form-html"></span>');
if (html instanceof jQuery) {
if (t.d.hasObject === false)
html.appendTo($htmlBox);
} else {
$htmlBox.html(html);
}
$htmlBox.appendTo(t.d.$widget);
t.d.htmls.push($htmlBox);
return t;
}
return t.d.htmls;
};
p.removeHtml = function (html) {
var t = this;
if (typeof html === 'string') {
for (var i = 0; i < t.d.htmls.length; i++) {
if (t.d.htmls[i].attr('id') === html)
t.d.htmls[i].remove();
}
} else if (html instanceof jQuery) {
html.remove();
}
};
p.json = function () {
var t = this;
var result = {};
$.each(t.widget().serializeArray(), function () {
var name = this.name;
if (name.slice(-2) === '[]') {
name = name.slice(0, -2);
if (typeof result[name] === 'undefined')
result[name] = [];
result[name].push(this.value);
} else {
result[name] = this.value;
}
});
return JSON.stringify(result);
};
$A.initBasicFunctions(Form, "Form");
});