/** * EGroupware eTemplate2 - JS Tree object * * @link http://community.egroupware.org/egroupware/api/js/dhtmlxtree/docsExplorer/dhtmlxtree/ * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @package etemplate * @subpackage api * @link https://www.egroupware.org * @author Nathan Gray * @author Ralf Becker * @copyright Nathan Gray 2011 */ /*egw:uses et2_core_inputWidget; /api/js/egw_action/egw_dragdrop_dhtmlx_tree.js; /api/js/dhtmlxtree/codebase/dhtmlxcommon.js; // using debugable and fixed source of dhtmltree instead: /api/js/dhtmlxtree/js/dhtmlXTree.js; /api/js/dhtmlxtree/sources/dhtmlxtree.js; /api/js/dhtmlxtree/sources/ext/dhtmlxtree_json.js; // /api/js/dhtmlxtree/sources/ext/dhtmlxtree_start.js; */ import {et2_register_widget, WidgetConfig} from "./et2_core_widget"; import {et2_inputWidget} from "./et2_core_inputWidget"; import {ClassWithAttributes} from "./et2_core_inheritance"; import {et2_no_init} from "./et2_core_common"; import {egw, framework} from "../jsapi/egw_global"; import {EGW_AO_FLAG_IS_CONTAINER, egw_getAppObjectManager, egw_getObjectManager, egwActionObject} from "../egw_action/egw_action.js"; import {dhtmlxtreeItemAOI} from "../egw_action/egw_dragdrop_dhtmlx_tree.js"; import {egwIsMobile} from "../egw_action/egw_action_common.js"; /* no module, but egw:uses is ignored, so adding it here commented out import '../../../api/js/dhtmlxtree/sources/dhtmlxtree.js'; import '../../../api/js/dhtmlxtree/sources/ext/dhtmlxtree_json.js'; import '../../../api/js/dhtmlxtree/sources/ext/dhtmlxtree_start.js'; */ /** * Tree widget * * For syntax of nodes supplied via sel_options or autoloading refer to Etemplate\Widget\Tree class. * * @augments et2_inputWidget */ export class et2_tree extends et2_inputWidget { static readonly _attributes : any = { "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": egw().webserverUrl + "/api/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" } }; private input : any = null; private div : JQuery; private autoloading_url: any; /** * Regexp used by _htmlencode */ _lt_regexp : RegExp = /= 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 */ private _htmlencode_node(_item : {text : string, item : any}) : object { _item.text = this._htmlencode(_item.text); if (_item.item && jQuery.isArray(_item.item)) { for(let i=0; i < _item.item.length; ++i) { this._htmlencode_node(_item.item[i]); } } return _item; } set_value(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 let checked = this.input.getAllChecked().split(this.input.dlmtr); for(let i = 0; i < checked.length; i++) { this.input.setCheck(checked[i], false); } // Check selected for(let 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(actions) { // Get the top level element for the tree // Only look 1 level deep for application object manager let objectManager = egw_getObjectManager(this.egw().app_name(),true,1); let 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 let 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 let apply_actions = function(treeObj, option) { // Add a new action object to the object manager // @ts-ignore let 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(let 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() { if(this.input == null) return null; if (this.options.multiple) { let allChecked = this.input.getAllChecked().split(this.input.dlmtr); let allUnchecked = this.input.getAllUnchecked().split(this.input.dlmtr); if (this.options.autoloading) { let res = {}; for (let i=0;iegw_getAppObjectManager()).getObjectById(this.id); for(var i=0; i < treeObj.children.length; i++) { if(treeObj.children[i].id == _id) { treeObj.children[i].id = _newItemId; if (treeObj.children[i].iface) treeObj.children[i].iface.id = _newItemId; break; } } if (typeof _label != 'undefined') this.setLabel(_newItemId, _label); } /** * deleteItem, deletes an item by id * @param _id ID of the node * @param _selectParent select the parent node true/false * @return void */ deleteItem(_id, _selectParent) { if(this.input == null) return null; this.input.deleteItem(_id, _selectParent); // Update action // since the action ID has to = this.id, getObjectById() won't work let treeObj = (egw_getAppObjectManager()).getObjectById(this.id); for(let i=0; i < treeObj.children.length; i++) { if(treeObj.children[i].id == _id) { treeObj.children.splice(i,1); } } } /** * Updates a leaf of the tree by requesting new information from the server using the * autoloading attribute. * * @param {string} _id ID of the node * @param {Object} [data] If provided, the item is refreshed directly with * the provided data instead of asking the server * @return void */ refreshItem(_id,data) { if(this.input == null) return null; this.input.deleteChildItems(_id); this.input.setDataMode('JSON'); /* Can't use this, it doesn't allow a callback this.input.refreshItem(_id); */ let self = this; if(typeof data != 'undefined' && data != null) { this.input.loadJSONObject(data, function() { self._dhtmlxtree_json_callback(data, _id);} ); } else { this.input.loadJSON(this.egw().link(this.autoloading_url, {id: _id}), function(dxmlObject) {self._dhtmlxtree_json_callback(JSON.parse(dxmlObject.xmlDoc.responseText), _id);} ); } } /** * focus the item, and scrolls it into view * * @param _id ID of the node * @return void */ focusItem(_id) { if(this.input == null) return null; this.input.focusItem(_id); } /** * hasChildren * * @param _id ID of the node * @return the number of childelements */ hasChildren(_id) { if(this.input == null) return null; return this.input.hasChildren(_id); } /** * Callback for after using dhtmlxtree's AJAX loading * The tree has visually already been updated at this point, we just need * to update the internal data. * * @param {object} new_data Fresh data for the tree * @param {string} update_option_id optional If provided, only update that node (and children) with the * provided data instead of the whole thing. Allows for partial updates. * @return void */ private _dhtmlxtree_json_callback(new_data, update_option_id) { // not sure if it makes sense to try update_option_id, so far I only seen it to be -1 let parent_id = typeof update_option_id != 'undefined' && update_option_id != -1 ? update_option_id : new_data.id; // find root of loaded data to merge it there let option = this._find_in_item(parent_id, this.options.select_options); // if we found it, merge it if (option) { jQuery.extend(option,new_data || {}); } else // else store it in root { this.options.select_options = new_data; } // Update actions by just re-setting them this.set_actions(this.options.actions || {}); } /** * Recursive search item object for given id * * @param {string} _id * @param {object} _item * @returns */ private _find_in_item(_id, _item) { if (_item && _item.id == _id) { return _item; } if (_item && typeof _item.item != 'undefined') { for(let i=0; i < _item.item.length; ++i) { let found = this._find_in_item(_id, _item.item[i]); if (found) return found; } } return null; } /** * Get node data by id * * @param {string} _id id of node * @return {object} object with attributes id, im0-2, text, tooltip, ... as set via select_options or autoload url */ getNode(_id) { return this._find_in_item(_id, this.options.select_options); } /** * Sets label of an item by id * * @param _id ID of the node * @param _label label to set * @param _tooltip new tooltip, default is previous set tooltip * @return void */ setLabel(_id, _label, _tooltip?) { if(this.input == null) return null; let tooltip = _tooltip || (this.getNode(_id) && this.getNode(_id).tooltip ? this.getNode(_id).tooltip : ""); this.input.setItemText(_id, this._htmlencode(_label), tooltip); } /** * Sets a style for an item by id * * @param {string} _id ID of node * @param {string} _style style to set * @return void */ setStyle(_id, _style) { if(this.input == null) return null; this.input.setItemStyle(_id, _style); } /** * getLabel, gets the Label of of an item by id * @param _id ID of the node * @return _label */ getLabel(_id) { if(this.input == null) return null; return this.input.getItemText(_id); } /** * getSelectedNode, retrieves the full node of the selected Item * @return string or null */ getSelectedNode() { if(this.input == null) return null; // no support for multiple selections // as there is no get Method to return the full selected node, we use this return this.options.multiple ? null : this.input._selected[0]; } /** * getTreeNodeOpenItems * * @param {string} _nodeID the nodeID where to start from (initial node) * @param {string} mode the mode to run in: "forced" fakes the initial node openState to be open * @return {object} structured array of node ids: array(message-ids) */ getTreeNodeOpenItems(_nodeID : string, mode? : string) { if(this.input == null) return null; let z = this.input.getSubItems(_nodeID).split(this.input.dlmtr); let oS; let PoS; let rv; let returnValue = [_nodeID]; let modetorun = "none"; if (mode) { modetorun = mode; } PoS = this.input.getOpenState(_nodeID); if (modetorun == "forced") PoS = 1; if (PoS == 1) { for(let i=0;i