Effect.DefaultOptions.duration = 0.5; Ajax.Responders.register({ onCreate: function() { if (Ajax.activeRequestCount) Element.show("ajax_loader"); else Element.hide("ajax_loader"); }, onComplete: function() { if (Ajax.activeRequestCount) Element.show("ajax_loader"); else Element.hide("ajax_loader"); } }); var sort = "categoryname"; function new_category(id) { return admin_category(id, {parent_id:id}); } function edit_category(id) { return admin_category(id, {id:id}); } function edit_category_image(id) { new ImageManager('formholder_'+id, 'category', id, 2); } function edit_product_image(id) { new ImageManager('formholder', 'product', id, 4); } function update_products_table() { var q = $F("q"); new Ajax.Updater('table', document.location, { parameters: { q: q, sort: sort, action: "get_products_table", discount_const_amount: discount_const_amount }, onSuccess:update_mode }); } function update_mode() { if(discount_const_amount) { Element.show($('discount_const_symbol')); Element.hide($('discount_percentage_symbol')); } else { Element.hide($('discount_const_symbol')); Element.show($('discount_percentage_symbol')); } } function save_categories() { var sortform = $("sortform"); categories = $$("ul.categories"); categories.each(function(cat) { if (!cat.id) return; var parent_id = cat.id.split("_")[1]; $A(cat.childNodes).each(function(item, sort) { id = item.id.split("_")[1]; sortform.appendChild(Builder.node("input", {"type":"hidden", "name":"category["+id+"]", "value":parent_id+":"+sort})); }); }); sortform.submit(); sortform.disable(); } function add_category_sortables() { var targets = $$("ul.categories").reverse(); targets.each(function(cat) { if (!cat.id) return; Sortable.create(cat, { window: scroll, handle: "bullet", onUpdate: function() { Element.show("sortform"); } }); }); var targets = $$("ul.categories li div.controlholder span.name"); targets.each(function(item) { Droppables.add(item, { accept: "category", //greedy: true, hoverclass: "accepting", onDrop: function(draggable, droppable) { item = droppable.parentNode.parentNode; if (item == draggable) return; id = item.id.split("_")[1]; subcats = $("subcats_"+id); subcats.appendChild(draggable); Element.show("sortform"); add_category_sortables(); } }); }); } Event.observe(window, "load", add_category_sortables); function delete_category(id, name) { var id = id; if (!confirm("Are you sure you want to delete this category?\n\nCategory: "+name)) return; new Ajax.Request(document.location, { parameters: { action: "delete", id: id }, onSuccess: function(t) { data = read_json(t); if (!data.response) return; Element.remove("category_"+id); } }); } function delete_product(id, name) { var id = id; if (!confirm("Are you sure you want to delete this product?\n\nProduct: "+name)) return; new Ajax.Request(document.location, { parameters: { action: "delete", id: id }, onSuccess: function(t) { data = read_json(t); if (!data.response) return; update_products_table(); } }); } function admin_category(id, parameters) { var id = id; parameters.action = "getform"; return admin_handle_request(id, parameters); } function admin_handle_request(id, parameters, cb) { new Ajax.Request(document.location, { parameters: parameters, onSuccess: function(t) { data = read_json(t); if (!data.response) return; var formholder = $("formholder_"+id); Element.hide(formholder); Element.update(formholder, data.html); if (cb) cb(id); Effect.BlindDown(formholder, { afterFinish: function() { var inputs = formholder.getElementsByTagName("input"); if (inputs.length) inputs[0].focus(); } }); } }); } function edit_products_cb(id) { Sortable.create("products_"+id, { window: scroll, handle: "bullet", onUpdate: function() { if (!Element.visible("sortform_"+id)) Effect.BlindDown("sortform_"+id); } }); } function edit_products(id) { var id = id; parameters = { action: "getproducts", id: id } return admin_handle_request(id, parameters, edit_products_cb); } function save_product_order(id) { postBody = Sortable.serialize("products_"+id, { name: "products" }); new Ajax.Request(document.location, { postBody: postBody+"&action=save_products", onSuccess: function() { document.location = document.location; } }); } function admin_products(id) { if (typeof(tinyMCE) != "undefined") { try { tinyMCE.triggerSave() $A(document.getElementsByTagName("textarea")).each(function(el) { tinyMCE.execCommand('mceRemoveControl',false,el.id); }); } catch(e) {} } parameters = { action: "getform", id: id } new Ajax.Request(document.location, { parameters: parameters, onSuccess: function(t) { data = read_json(t); if (!data.response) return; var formholder = $("formholder"); Element.hide(formholder); Element.update(formholder, data.html); if (data.js) eval(data.js); Effect.BlindDown(formholder); Element.scrollTo(formholder); } }); } function form_submit(form) { if (typeof(tinyMCE) != "undefined") { try { tinyMCE.triggerSave() $A(document.getElementsByTagName("textarea")).each(function(el) { tinyMCE.execCommand('mceRemoveControl',false,el.id); }); } catch(e) {} } info = Form.serialize(form, true); new Ajax.Request(document.location, { parameters: info, onSuccess: function(t) { data = read_json(t); if (data.response) { switch (data.action) { case "exec": eval(data.code); break; } } else { Element.update(data.formholder, data.html); if (data.js) eval(data.js); } } }); } function add_mce_controls() { $A(document.getElementsByTagName("textarea")).each(function(el) { try { if (el.id != "_TinyMCE_clipboardHTML") tinyMCE.execCommand('mceAddControl',false,el.id); } catch (e) {} }); } function form_cancel(form) { Effect.BlindUp(Element.findParentOfType(form, "FORM").parentNode); } function read_json(t) { return eval('('+t.responseText+')'); } function do_mmd(ev) { if (ev.keyCode != Event.KEY_RETURN) return true; el = Event.element(ev); name = el.getAttribute("name"); value = $F(el); if (!value.length) return; new Ajax.Request(document.location, { parameters: {"action":"mmd_"+name+"_add", "value":value}, onSuccess: function(t) { data = read_json(t); cont = $("mmd_"+name); if (data.exists) { inputs = cont.getElementsByTagName("input"); inputsl = inputs.length; for (var i = 0; i < inputsl; i++) { input = inputs[i]; if (input.value == data.id) { input.checked = "checked"; break; } } } else { // Build label the hard way.. grr IE label = Builder.node("label", [ Builder.node("input", { "type":"checkbox", "name":"mmd_"+data.key+"[]", "value":data.id, "checked":"checked" }), data.value ]); cont.appendChild(label); } el.value = ""; } }); Event.stop(ev); } var debug = false; var ImageManager = Class.create({ initialize: function(holder, type, id, num) { this.holder = $(holder); this.type = type; this.id = id; this.num = num; this.buildUI(); }, buildUI: function() { if (Element.visible(this.holder) && this.holder.hasChildNodes()) { Effect.BlindUp(this.holder, { afterFinish: this._buildUI.bind(this) }); } else { this._buildUI(); } }, _buildUI: function() { Element.infanticide(this.holder); Element.hide(this.holder); height = ((this.num+1) * 33) + 20; this.images_div = Builder.node("div", {"class":"im-images"}); this.iframe = Builder.node("iframe", {"height":height,"frameborder":"0","className":"imagemanager", "src":"imagemanager.html?type="+this.type+"&id="+this.id+"&num="+this.num}); Event.observe(this.iframe, "load", this.decorateIframe.bind(this)); this.holder.appendChild(this.images_div); this.holder.appendChild(this.iframe); Effect.BlindDown(this.holder); }, closeUI: function() { Effect.BlindUp(this.holder, { afterFinish: function() { Element.infanticide(this.holder); Element.show(this.holder); }.bind(this) }); }, decorateIframe: function() { this.idoc = this.iframe.contentWindow.document; var form = this.idoc.getElementById("form"); if (!form) return this.closeUI(); new Ajax.Request("imagemanager.html", { parameters: {action:"getimages",type:this.type, id:this.id}, onSuccess: function(t) { data = read_json(t); Element.infanticide(this.images_div); anticache = new Date().getTime(); data.each(function(num) { var div = this.images_div.appendChild(Builder.node("div", [ Builder.node("img", {"src":"../../resize/"+this.type+"_"+this.id+"/"+num+"/75.jpg?"+anticache}), Builder.node("br"), Builder.node("a", {"href":"#", "id":"image_"+this.type+"_"+this.id+"_"+num}, "[ delete ]") ])); Event.observe(div, "click", this.deleteImage); }.bind(this)); }.bind(this) }); }, // Static deleteImage: function(ev) { var el = Event.element(ev); parts = el.id.split("_"); type = parts[1]; id = parts[2]; num = parts[3]; new Ajax.Request("imagemanager.html", { parameters: { action: "deleteimage", type: type, id: id, num: num }, onSuccess: function(t) { var el_id = "image_"+type+"_"+id+"_"+num; Effect.Fade($(el_id).parentNode, { afterFinish: function() { Element.remove($(el_id).parentNode); } }); } }); } }); var MultiLineInput = Class.create({ initialize: function (el) { this.container = $(el); this.template = this.container.getElementsByTagName("p")[0].cloneNode(true); $A(this.container.getElementsByTagName("p")).each(function(line) { this.addHandlers(line) }.bind(this)); this.rescan(); }, rescan: function() { var lines = $A(this.container.getElementsByTagName("p")); var nlines = lines.length; var remove = []; var blank_at_end = 0; lines.each(function(line, n) { var inputs = $A(line.getElementsByTagName("input")); blank = true; inputs.each(function(input) { if ($F(input).strip()) { blank = false; throw $break; } else { return; } }); if (blank) { blank_at_end++; if (n < nlines-1) remove.push(n); } else blank_at_end = 0; }); remove.each(function(n) { Element.remove(lines[n]); }); if (blank_at_end < 1) { var line = this.container.appendChild(this.template.cloneNode(true)); $A(line.getElementsByTagName("input")).each(function(input) { input.value = ""; }); this.addHandlers(line); } }, addHandlers: function(line) { $A(line.getElementsByTagName("input")).each(function(input) { Event.observe(input, "change", this.rescan.bind(this)); }.bind(this)); } }); Element.findParentOfType = function(el, type) { while (el = el.parentNode) { if (el.nodeName == type) return el; } return false; } Element.infanticide = function(node) { node = $(node) if (node) while (node.hasChildNodes()) node.removeChild(node.firstChild) } function setProductLive(el) { new Ajax.Request(document.location, { parameters: { action: "setlive", id: el.value, value: Number(el.checked) } }); } // {{{ Discounts functions discount_const_amount = 0; // use an int because ''+false == 'false' != false function setAllDiscounts() { var discount = $F("discount"); $$("input.discount").each(function(el) { el.value = discount; }); showSave(); } function saveAllDiscounts() { var postBody = "action=set_all_discounts&discount_const_amount="+discount_const_amount; $$("input.discount").each(function(el) { var id = el.id.split("-")[1]; var discount = el.value; postBody += "&discounts["+id+"]="+escape(discount); }); disableSave(); new Ajax.Request(document.location, { postBody: postBody, onComplete: function(ret) {console.log(ret); hideSave();} }); } function cancelDiscounts() { update_products_table(); hideSave(); Element.show("search"); } function showSave(el) { if (Element.visible("save")) return $$(".save-data").each(Element.show); Element.hide("search"); } function disableSave() { $$(".save-data input").each(Form.Element.disable); } function enableSave() { $$(".save-data input").each(Form.Element.enable); } function hideSave() { $$(".save-data").each(Element.hide); enableSave() Element.show("search"); update_products_table(); } // }}}