/* * REditor v0.91 unstable alpha3 rc62 * Probably you are wondering what REditor means :) * And you have problem - you are not the only one who don't know ;P * @author: Reinmar pkoszulinski@gmail.com * @license: what for? noone will wan't to use this piece of shit ;* * */ /* TODO * server side error handling * multilanguage * add triggering callbacks * values as AJAX params - get them from url! */ var REditor = Class.create({ __form_container_el: null, //element where the form will be inserted __response_container_el: null, //element which is replaced by ajax response __url: null, //post __control_el: null, //external control element - click this to edit __options: null, //set of options - __default_options mixed with user def __fields: null, //infos about input, selects, etc __fields_dom: null, //fields - inputs, selects - to get their value __store: null, //store content of container when it is replaced by form initialize: function (form_container, response_container, url, control, args) { this.__form_container_el = form_container; this.__response_container_el = response_container; this.__url = url; this.__control_el = control; //be sure that all tables are initialized here //prevent loading old fields in new submited form this.__fields = new Array(); this.__fields_dom = new Array(); this.__store = new Array(); this.__prepareOptions(args); Event.observe(this.__control_el, 'click', function () { this.startEdit(); }.bind(this)); }, startEdit: function () { this.__hideStartContent(); this.__createForm(); this.__triggerCallback('startEdit', null); }, cancelEdit: function () { this.__removeForm(); this.__restoreStartContent(); this.__triggerCallback('cancelEdit', null); }, addField: function (field) { this.__fields.push(field); }, __prepareOptions: function (args) { this.__options = Object.clone(REditor.__default_options); Object.extend(this.__options, args); }, //remove all nodes from this.__form_container_el and store them __hideStartContent: function () { var s = new Array(); var c = this.__form_container_el; $A(this.__form_container_el.childNodes).each(function (el) { s.push(c.removeChild(el)); }); this.__store = s; }, //restore removed nodes (that is content that was there at page load) to this.__form_container_el __restoreStartContent: function () { var c = this.__form_container_el; this.__store.each(function (el) { c.appendChild(el); }); this.__store = new Array(); }, //remove everything in container (used for removing form after canceling) __removeForm: function () { var c = this.__form_container_el; $A(this.__form_container_el.childNodes).each(function (el) { c.removeChild(el); }); this.__fields_dom = new Array(); //delete all elements //TODO v2.0 - do not delete them, but store here between next "edit -> cancel" clicks //so values of them also will be stored //createForm must check if this array exists }, __createForm: function () { var l = new Array(); this.__fields.each(function (field) { l.push(this.__createLine(field)); }.bind(this)); var c = this.__form_container_el; l.each(function (el) { c.insert(el); }); var p = new Element('p', {class: this.__options.class_name_prefix + 'footer'}); var s = new Element('button', {type: 'submit'}).update('Zapisz'); s.observe('click', function () { this.submit(); }.bind(this)); p.insert(s); var s = new Element('button', {type: 'button'}).update('Anuluj'); s.observe('click', function() { this.cancelEdit(); }.bind(this)); p.insert(s); c.insert(p); }, submit: function () { var url = this.__url + this.__prepareParams() + this.__options.callback(this.__fields_dom); var reditor = this; this.__form_container_el.down('.' + this.__options.class_name_prefix + 'footer').insert({bottom: ' Zapisywanie...'}); new Ajax.Request(url, { onSuccess: function (transport) { //TODO - let user define what to do with this reditor.__response_container_el.replace(transport.responseText); this.__triggerCallback('ajaxSuccess', null); }, onFailure: function () { alert('Błąd podczas zapisywania formularza'); reditor.cancelEdit(); } }); }, __createInputText: function (field) { var f = new Element('input', {name: field.name, value: field.value, type: 'text'}); if (field.id) f.writeAttribute({id: field.id}); this.__fields_dom.push(f); return f; }, __createTextarea: function (field) { var f = new Element('textarea', {name: field.name, cols: field.cols, rows: field.rows}).setValue(field.value); if (field.id) f.writeAttribute({id: field.id}); this.__fields_dom.push(f); return f; }, __createUserElement: function (field) { var f = new Element(field.tagname, field.params); if (field.content) f.update(field.content); return f; }, //create paragraph with proper field and label __createLine: function (field) { switch (field.type) { case 'textarea': var f = this.__createTextarea(field); break; case 'user': //user defined element var f = this.__createUserElement(field); break; default: //case 'inputtext' var f = this.__createInputText(field); }; var p = new Element('p', {class: this.__options.class_name_prefix + 'line'}); if (field.label) { var l = new Element('label', {'for': field.id}).update(field.label); //STUPID OPERA!! (I mean '' for for) p.update(this.__options.label_field_separator); p.insert({top: l}); } p.insert({bottom: f}); return p; }, __triggerCallback: function(param_name, arg) { if ('function' == typeof this.__options[param_name]) { this.__options[param_name](this, arg); } }, //return for all fields name1=value1&name2=value2& __prepareParams: function() { var params = '?'; this.__fields_dom.each(function (el) { params += el.getAttribute('name') + '=' + encodeURIComponent(el.value) + '&' }); return params; } }); REditor.__default_options = { //attached after url+__prepareParams() callback: function (fields) { return '' }, class_name_prefix: 'reditor_', label_field_separator: ': ', startEdit: function (reditor, args) {}, cancelEdit: function (retitor, args) {}, ajaxSuccess: function (retitor, args) {} };