var BlockController = function(options) { this.init(options) }; BlockController.prototype = { block: false, authenticity_token: false, //new_block_type_id: false, block_types: false, modal: false, init: function(options) { for (var thing in options) this[thing] = options[thing]; //this.set_block_type_editable(); this.set_block_value_editable(this.block); this.set_child_blocks_editable(); this.set_clickable(); }, base_url: function(b) { return '/admin/' + (b.page_id ? 'pages/' + b.page_id : 'posts/' + b.post_id) + '/blocks'; }, block_url: function(b) { return this.base_url(b) + '/' + b.id; }, edit_block: function(block_id) { window.location = this.block_url(that.block) + '/edit'; }, /***************************************************************************** Block Rendering *****************************************************************************/ set_clickable: function() { var that = this; $.ajax({ url: that.base_url(that.block) + '/tree', success: function(blocks) { $(blocks).each(function(i,b) { that.set_clickable_helper(b); }); } }); }, set_clickable_helper: function(b) { var that = this; $('#block_' + b.id).attr('onclick','').unbind('click'); $('#block_' + b.id).click(function(e) { e.stopPropagation(); that.edit_block(b.id); }); if (b.allow_child_blocks == true) { $('#new_block_' + b.id).replaceWith($('') .attr('type', 'button') .val('New Block') .click(function(e) { e.stopPropagation(); that.new_block(b.id); }) ); } var show_mouseover = true; if (b.children && b.children.length > 0) { $.each(b.children, function(i, b2) { if (b2.block_type_id = 34) show_mouseover = false; that.set_clickable_helper(b2); }); } if (show_mouseover) { $('#block_' + b.id).mouseover(function(el) { $('#block_' + b.id).addClass( 'block_over'); }); $('#block_' + b.id).mouseout(function(el) { $('#block_' + b.id).removeClass('block_over'); }); } }, /****************************************************************************/ //set_block_type_editable: function() //{ // var that = this; // var b = this.block; // m = new ModelBinder({ // name: 'Block', // id: b.id, // update_url: '/admin/pages/' + b.page_id + '/blocks/' + b.id, // authenticity_token: that.authenticity_token, // attributes: [{ // name: 'block_type_id', // nice_name: 'Block type', // type: 'select', // value: b.block_type_id, // text: b.block_type.name, // width: 400, // fixed_placeholder: true, // options_url: '/admin/block-types/options', // after_update: function() { parent.controller.render_blocks(); window.location.reload(true); }, // after_cancel: function() { parent.controller.render_blocks(); window.location.reload(true); }, // on_load: function() { that.modal.autosize(); } // },{ // name: 'parent_id', // nice_name: 'Parent ID', // type: 'select', // value: b.parent_id, // text: b.parent_title, // width: 400, // fixed_placeholder: true, // options_url: '/admin/pages/' + b.page_id + '/block-options', // after_update: function() { parent.controller.render_blocks(); }, // after_cancel: function() { parent.controller.render_blocks(); }, // on_load: function() { that.modal.autosize(); } // }] // }); // $('#advanced').hide(); //}, set_block_value_editable: function(b) { var that = this; var bt = b.block_type; if (b.block_type.field_type == 'block') return; var h = { name: 'value', type: bt.field_type, nice_name: bt.description ? bt.description : bt.name, width: bt.width ? bt.width : 780, after_update: function() { parent.controller.render_blocks(); }, after_cancel: function() { parent.controller.render_blocks(); } }; h['value'] = b.value if (bt.field_type == 'checkbox') h['value'] = b.value ? 'true' : 'false'; if (bt.field_type == 'image') h['value'] = b.image.tiny_url; if (bt.field_type == 'file') h['value'] = b.image.url; if (bt.field_type == 'select') h['text'] = b.value; if (bt.height) h['height'] = bt.height; if (bt.fixed_placeholder) h['fixed_placeholder'] = bt.fixed_placeholder; if (bt.options || bt.options_function) h['options_url'] = '/admin/block-types/' + bt.id + '/options'; else if (bt.options_url) h['options_url'] = bt.options_url; if (bt.field_type == 'file') h['update_url'] = that.block_url(b) + '/file'; if (bt.field_type == 'image') { h['update_url'] = that.block_url(b) + '/image' h['image_refresh_delay'] = 100; } m = new ModelBinder({ name: 'Block', id: b.id, update_url: that.block_url(b), authenticity_token: that.authenticity_token, attributes: [h] }); }, set_child_blocks_editable: function() { var that = this; $.each(this.block.children, function(i, b) { var bt = b.block_type; if (bt.field_type == 'block' || bt.field_type == 'richtext') { //$('#block_' + b.id).attr('onclick','').unbind('click'); //$('#block_' + b.id).click(function(e) { // window.location = '/admin/pages/' + b.page_id + '/blocks/' + b.id + '/edit'; //}); } else that.set_block_value_editable(b); }); }, add_child_block: function(block_type_id) { var that = this; if (!this.block_types) { modal.autosize("
Getting block types...
"); $.ajax({ url: '/admin/block-types/options', type: 'get', success: function(resp) { that.block_types = resp; that.add_child_block(); } }); return; } if (!block_type_id) { var select = $('').attr('id', 'new_block_type_id'); $.each(this.block_types, function(i, bt) { var opt = $('').val(bt.value).html(bt.text); select.append(opt); }); var p = $('').addClass('note warning') .append("Select a type of page block: ") .append(select) .append("Adding block...
"); $.ajax({ url: that.block_url(block), type: 'post', data: { block_type_id: block_type_id }, success: function(resp) { if (resp.error) modal.autosize("" + resp.error + "
"); if (resp.block) window.location.reload(true); } }); }, delete_block: function(confirm) { var that = this; if (!confirm) { var p = $('').addClass('note warning') .append("Are you sure you want to delete the block? This can't be undone.Deleting block...
"); $.ajax({ url: that.block_url(that.block), type: 'delete', success: function(resp) { if (resp.error) modal.autosize("" + resp.error + "
"); if (resp.redirect) { parent.controller.render_blocks(); modal.close(); } } }); }, move_up: function() { var that = this; modal.autosize("Moving up...
"); $.ajax({ url: that.block_url(that.block) + '/move-up', type: 'put', success: function(resp) { if (resp.error) modal.autosize("" + resp.error + "
"); if (resp.success) { modal.autosize("" + resp.success + "
"); parent.controller.render_blocks(); } } }); }, move_down: function() { var that = this; modal.autosize("Moving down...
"); $.ajax({ url: that.block_url(that.block) + '/move-down', type: 'put', success: function(resp) { if (resp.error) modal.autosize("" + resp.error + "
"); if (resp.success) { modal.autosize("" + resp.success + "
"); parent.controller.render_blocks(); } } }); } };