/** * EGroupware eTemplate2 - JS Tree object * * @link http://community.egroupware.org/egroupware/phpgwapi/js/dhtmlxtree/docsExplorer/dhtmlxtree/ * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @package etemplate * @subpackage api * @link http://www.egroupware.org * @author Nathan Gray * @author Ralf Becker * @copyright Nathan Gray 2011 * @version $Id$ */ /*egw:uses et2_core_inputWidget; /phpgwapi/js/egw_action/egw_dragdrop_dhtmlx_tree.js; /phpgwapi/js/dhtmlxtree/codebase/dhtmlxcommon.js; // using debugable and fixed source of dhtmltree instead: /phpgwapi/js/dhtmlxtree/js/dhtmlXTree.js; /phpgwapi/js/dhtmlxtree/sources/dhtmlxtree.js; /phpgwapi/js/dhtmlxtree/sources/ext/dhtmlxtree_json.js; // /phpgwapi/js/dhtmlxtree/sources/ext/dhtmlxtree_start.js; */ /** * Tree widget * * For syntax of nodes supplied via sel_optons or autoloading refer to etemplate_widget_tree class. * * @augments et2_inputWidget */ var et2_tree = (function(){ "use strict"; return et2_inputWidget.extend( { attributes: { "multiple": { "name": "multiple", "type": "boolean", "default": false, "description": "Allow selecting multiple options" }, "select_options": { "type": "any", "name": "Select options", "default": {}, "description": "Used to set the tree options." }, "onclick": { "description": "JS code which gets executed when clicks on text of a node" }, "onselect": { "name": "onSelect", "type": "js", "default": et2_no_init, "description": "Javascript executed when user selects a node" }, "oncheck": { "name": "onCheck", "type": "js", "default": et2_no_init, "description": "Javascript executed when user checks a node" }, // onChange event is mapped depending on multiple to onCheck or onSelect onopenstart: { "name": "onOpenStart", "type": "js", "default": et2_no_init, "description": "Javascript function executed when user opens a node: function(_id, _widget, _hasChildren) returning true to allow opening!" }, onopenend: { "name": "onOpenEnd", "type": "js", "default": et2_no_init, "description": "Javascript function executed when opening a node is finished: function(_id, _widget, _hasChildren)" }, "image_path": { "name": "Image directory", "type": "string", "default": this.egw().webserverUrl + "/phpgwapi/templates/default/images/dhtmlxtree/", "description": "Directory for tree structure images, set on server-side to 'dhtmlx' subdir of templates image-directory" }, "value": { "type": "any", "default": {} }, "actions": { "name": "Actions array", "type": "any", "default": et2_no_init, "description": "List of egw actions that can be done on the tree. This includes context menu, drag and drop. TODO: Link to action documentation" }, "autoloading": { "name": "Autoloading", "type": "string", "default": "", "description": "JSON URL or menuaction to be called for nodes marked with child=1, but not having children, GET parameter selected contains node-id" }, "std_images": { "name": "Standard images", "type": "string", "default": "", "description": "comma-separated names of icons for a leaf, closed and opend folder (default: leaf.png,folderClosed.png,folderOpen.png), images with extension get loaded from image_path, just 'image' or 'appname/image' are allowed too" }, "multimarking": { "name": "multimarking", "type": "any", "default": false, "description": "Allow marking multiple nodes, default is false which means disabled multiselection, true or 'strict' activates it and 'strict' makes it strick to only same level marking" }, highlighting:{ "name": "highlighting", "type": "boolean", "default": false, "description": "Add highlighting class on hovered over item, highlighting is disabled by default" } }, /** * Constructor * * @memberOf et2_tree */ init: function() { this._super.apply(this, arguments); this.input = null; this.div = $j(document.createElement("div")).addClass("dhtmlxTree"); this.setDOMNode(this.div[0]); }, destroy: function() { if(this.input) { this.input.destructor(); } this.input = null; this._super.apply(this, arguments); }, /** * Get tree items from the sel_options data array * * @param {object} _attrs */ transformAttributes: function(_attrs) { this._super.apply(this, arguments); // If select_options are already known, skip the rest if(this.options && this.options.select_options && !jQuery.isEmptyObject(this.options.select_options)) { return; } var name_parts = this.id.replace(/]/g,'').split('['); // Try to find the options inside the "sel-options" array if(this.getArrayMgr("sel_options")) { // Select options tend to be defined once, at the top level, so try that first var content_options = this.getArrayMgr("sel_options").getRoot().getEntry(name_parts[name_parts.length-1]); // Try again according to ID if(!content_options) content_options = this.getArrayMgr("sel_options").getEntry(this.id); if(_attrs["select_options"] && !jQuery.isEmptyObject(_attrs["select_options"]) && content_options) { _attrs["select_options"] = jQuery.extend({},_attrs["select_options"],content_options); } else if (content_options) { _attrs["select_options"] = content_options; } } // Check whether the options entry was found, if not read it from the // content array. if (_attrs["select_options"] == null) { // Again, try last name part at top level var content_options = this.getArrayMgr('content').getRoot().getEntry(name_parts[name_parts.length-1]); // If that didn't work, check according to ID _attrs["select_options"] = content_options ? content_options : this.getArrayMgr('content') .getEntry("options-" + this.id); } // Default to an empty object if (_attrs["select_options"] == null) { _attrs["select_options"] = {}; } }, // overwrite default onclick to do nothing, as we install onclick via dhtmlxtree click: function(_node) {}, createTree: function(widget) { widget.input = new dhtmlXTreeObject({ parent: widget.div[0], width: '100%', height: '100%', image_path: widget.options.image_path, checkbox: widget.options.multiple }); // to allow "," in value, eg. folder-names, IF value is specified as array widget.input.dlmtr = ':}-*('; if (widget.options.std_images) { widget.setImages.apply(widget, widget.options.std_images.split(',')); } else { // calling setImages to get our png or svg default images widget.setImages(); } // Add in the callback so we can keep the two in sync widget.input.AJAX_callback = function(dxmlObject) { widget._dhtmlxtree_json_callback(JSON.parse(dxmlObject.xmlDoc.responseText), widget.input.lastLoadedXMLId); // Call this in case we added some options that were already selected, but missing if(widget.options.multiple) { widget.set_value(widget.value); } }; if (widget.options.autoloading) { var url = widget.options.autoloading; //Set escaping mode to utf8, as url in //autoloading needs to be utf8 encoded. //For instance item id with umlaut. widget.input.setEscapingMode('utf8'); if (url.charAt(0) != '/' && url.substr(0,4) != 'http') { url = '/json.php?menuaction='+url; } this.autoloading_url = url; widget.input.setXMLAutoLoading(egw.link(url)); widget.input.setDataMode('JSON'); } if (widget.options.multimarking) { widget.input.enableMultiselection(!!widget.options.multimarking, widget.options.multimarking === 'strict'); } // Enable/Disable highlighting widget.input.enableHighlighting(widget.options.highlighting?true:false); // if templates supplies open/close right/down arrows, show no more lines and use them instead of plus/minus var open = egw.image('dhtmlxtree/open'); var close = egw.image('dhtmlxtree/close'); if (open && close) { widget.input.enableTreeLines(false); open = this._rel_url(open); widget.input.setImageArrays('plus', open, open, open, open, open); close = this._rel_url(close); widget.input.setImageArrays('minus', close, close, close, close, close); } }, /** * Install event handlers on tree * * @param _name * @param _handler */ _install_handler: function(_name, _handler) { if (typeof _handler == 'function') { if(this.input == null) this.createTree(this); // automatic convert onChange event to oncheck or onSelect depending on multiple is used or not if (_name == 'onchange') _name = this.options.multiple ? 'oncheck' : 'onselect'; var handler = _handler; var widget = this; this.input.attachEvent(_name, function(_id){ var args = jQuery.makeArray(arguments); // splice in widget as 2. parameter, 1. is new node-id, now 3. is old node id args.splice(1, 0, widget); return handler.apply(this, args); }); } }, set_onchange: function(_handler) { this._install_handler('onchange', _handler); }, set_onclick: function(_handler) { this._install_handler('onclick', _handler); }, set_onselect: function(_handler) { this._install_handler('onselect', _handler); }, set_onopenstart: function(_handler) { this._install_handler('onOpenStart', _handler); }, set_onopenend: function(_handler) { this._install_handler('onOpenEnd', _handler); }, set_select_options: function(options) { var custom_images = false; this.options.select_options = options; if(this.input == null) { this.createTree(this); } // Structure data for category tree if(this._type == 'tree-cat') { var data = {id:0,item:[]}; var stack = {}; for(var key=0; key < options.length; key++) { // See if item has an icon if(options[key].data && typeof options[key].data.icon !== 'undefined' && options[key].data.icon) { var img = this.egw().image(options[key].data.icon, options[key].appname); if(img) { custom_images = true; options[key].im0 = options[key].im1 = options[key].im2 = img; } } // Item color - not working if(options[key].data && typeof options[key].data.color !== 'undefined' && options[key].data.color) { options[key].style = options[key].style || "" + "background-color:'"+options[key].data.color+"';"; } // Tooltip if(options[key].description && !options[key].tooltip) { options[key].tooltip = options[key].description; } var parent_id = parseInt(options[key]['parent']); if(isNaN(parent_id)) parent_id = 0; if(!stack[parent_id]) stack[parent_id] = []; stack[parent_id].push(options[key]); } if(custom_images) { var path = this.input.iconURL; this.input.setIconPath(""); for(var k = 0; k < this.input.imageArray.length; k++) this.input.imageArray[k] = path + this.input.imageArray[k]; } var f=function(data,f) { if (stack[data.id]) { data.item=stack[data.id]; for (var j=0; j= 0) { _text = _text.replace(this._lt_regexp, '<'); } return _text; }, /** * html encoding of text of node incl. all children * * @param {object} _item with required attributes text, id and optional tooltip and item * @return {object} encoded node */ _htmlencode_node: function(_item) { _item.text = this._htmlencode(_item.text); if (_item.item && jQuery.isArray(_item.item)) { for(var i=0; i < _item.item.length; ++i) { this._htmlencode_node(_item.item[i]); } } return _item; }, set_value: function(new_value) { this.value = this._oldValue = (typeof new_value === 'string' && this.options.multiple ? new_value.split(',') : new_value); if(this.input == null) return; if (this.options.multiple) { // Clear all checked var checked = this.input.getAllChecked().split(this.input.dlmtr); for(var i = 0; i < checked.length; i++) { this.input.setCheck(checked[i], false); } // Check selected for(var i = 0; i < this.value.length; i++) { this.input.setCheck(this.value[i], true); // autoloading openning needs to be absolutely based on user interaction // or open flag in folder structure, therefore, We should // not force it to open the node if (!this.options.autoloading) this.input.openItem(this.value[i]); } } else { this.input.selectItem(this.value, false); // false = do not trigger onSelect this.input.focusItem(this.value); this.input.openItem(this.value); } }, /** * Links actions to tree nodes * * @param {object} actions [ {ID: attributes..}+] as for set_actions */ _link_actions: function(actions) { // Get the top level element for the tree // Only look 1 level deep for application object manager var objectManager = egw_getObjectManager(this.egw().appName,true,1); var treeObj = objectManager.getObjectById(this.id); if (treeObj == null) { // Add a new container to the object manager which will hold the tree // objects treeObj = objectManager.addObject( new egwActionObject(this.id, objectManager, null, this._actionManager, EGW_AO_FLAG_IS_CONTAINER), null, EGW_AO_FLAG_IS_CONTAINER ); } // Delete all old objects treeObj.clear(); // Go over the tree parts & add links var action_links = this._get_action_links(actions); if (typeof this.options.select_options != 'undefined') { // Iterate over the options (leaves) and add action to each one var apply_actions = function(treeObj, option) { // Add a new action object to the object manager var obj = treeObj.addObject((typeof option.id == 'number'?String(option.id):option.id), new dhtmlxtreeItemAOI(this.input, option.id)); obj.updateActionLinks(action_links); if(option.item && option.item.length > 0) { for(var i = 0; i < option.item.length; i++) { apply_actions.call(this, treeObj, option.item[i]); } } }; apply_actions.call(this, treeObj, this.options.select_options); } }, /** * getValue, retrieves the Id of the selected Item * @return string or object or null */ getValue: function() { if(this.input == null) return null; if (this.options.multiple) { var allChecked = this.input.getAllChecked().split(this.input.dlmtr); var allUnchecked = this.input.getAllUnchecked().split(this.input.dlmtr); if (this.options.autoloading) { var res = {}; for (var i=0;i