// setup namespace Zymonic.FilterAction = {}; // base filter action // don't do anything here, makes it easy to establish inheritance Zymonic.FilterAction.FilterAction = function() {}; // init function Zymonic.FilterAction.FilterAction.prototype.init = function(args) { for (var key in args) { this[key] = args[key]; } // lookup target location if not set if (!this.target_location || this.target_location == "") { var parent_location = this.getLink().parents('ul.block'); if (parent_location.length) { var ident = parent_location.first().attr('id'); if (ident) { this.target_location = ident.replace('SLocation_', ''); } } } if (!this.target_location || this.target_location == "") { Zymonic.log("Cannot find target location"); } this.block_added_callbacks = {}; if (this.lookup_ident != "") { Zymonic.add_to_lookup('FilterAction', this.lookup_ident, this); } if (this.result_ident != "") { Zymonic.add_to_lookup('FilterActionByResult', this.result_ident+this.zname, this); } this.setupHandlers(); }; // function to set a handler for when a block is added by this filter action Zymonic.FilterAction.FilterAction.prototype.addBlockAddedCallback = function(key, fn) { // overwrite any existing handler with that key this.block_added_callbacks[key] = fn; }; // function to set an extra param to be sent on running filter action Zymonic.FilterAction.FilterAction.prototype.addExtraParam = function(key, value) { this.params[key] = value; }; // get the parent filter Zymonic.FilterAction.FilterAction.prototype.getParentFilter = function() { return getZymonicFilter(this.filter_ident); }; // get the filter action link Zymonic.FilterAction.FilterAction.prototype.getLink = function() { return $("#"+this.parent_ident+"_"+this.ident+"_link"); }; // get the url to run the filter action Zymonic.FilterAction.FilterAction.prototype.getURL = function() { // Split base URL var url_split = split_baseurl(base_url); // get url params and add the ones we need // Split base URL var url_split = split_baseurl(base_url); var url_params = get_url_params([ "ZZchangedblocks_" + this.target_location + "=new", "ZZblockchange_" + this.target_location + "_new_new=Y", "ZZblockchange_" + this.target_location + "_new_position=" + Zymonic.getNextBlockOrder(this.target_location, this.target_location_position), "ZZblockchange_" + this.target_location + "_new_block_type=" + this.action_type, "ZZwebservicemode=block", "ZZBlocation=" + this.target_location, "ZZno_display_attributes=false", ]); // add params for (var key in this.params) { var value = this.params[key]; // if value is marker, lookup field value if (value.substr(0,1) == '[' && value.substr(value.length-1,1) == ']') { var ident = value.substr(1, value.length-2); var field = getZymonicField(ident); if (field) { value = field.getValue(); } else { value = ''; } } // if value is empty then don't send it if (value && value != '') { url_params.push("ZZblockchange_" + this.target_location + "_new_" + key + "=" + value); } } return url_split[0] + "?" + url_split[1] + url_params.join("&"); }; // function to run filter action Zymonic.FilterAction.FilterAction.prototype.run = function(cb) { // trigger add block action with callback var filter_action = this; var extras = {}; // add params for (var key in this.params) { var value = this.params[key]; // if value is marker, lookup field value if (value.substr(0,1) == '[' && value.substr(value.length-1,1) == ']') { var ident = value.substr(1, value.length-2); var field = getZymonicField(ident); if (field) { value = field.getValue(); } else { value = ''; } } else { // value is sent from server encoded, so decode it for use here value = decodeURIComponent(value); } // if value is empty then don't send it if (value && value != '') { Zymonic.addOpenBlockParam(extras, this.target_location, key, value); } } Zymonic.openBlock( this.params['zname'], this.params['process_id'], this.action_type, this.target_location, extras, function(xml, block_id) { if (cb) { cb(xml, block_id); } for (var key in filter_action.block_added_callbacks) { filter_action.block_added_callbacks[key](xml, block_id); } } ); }; // function to setup handlers on filter action links Zymonic.FilterAction.FilterAction.prototype.setupHandlers = function() { // add click handler to link once the document is ready var filter_action = this; $(document).ready(function() { Zymonic.Utils.attach_event(filter_action.getLink(), "click", filter_action.ident, function(event) { filter_action.run.call(filter_action); // prevent the normal click action event.preventDefault(); }); }); };