is already returned as object (http://api.jquery.com/data/#data-html5)
+ d = $.extend(d, json);
+ }
+ return d;
+}
+
+
+// TODO: use currying
+function _makeNodeTitleMatcher(s){
+ s = s.toLowerCase();
+ return function(node){
+ return node.title.toLowerCase().indexOf(s) >= 0;
+ };
+}
+
+var i,
+ FT = null, // initialized below
+ ENTITY_MAP = {"&": "&", "<": "<", ">": ">", "\"": """, "'": "'", "/": "/"},
+ //boolean attributes that can be set with equivalent class names in the LI tags
+ CLASS_ATTRS = "active expanded focus folder hideCheckbox lazy selected unselectable".split(" "),
+ CLASS_ATTR_MAP = {},
+ // Top-level Fancytree node attributes, that can be set by dict
+ NODE_ATTRS = "expanded extraClasses folder hideCheckbox key lazy refKey selected title tooltip unselectable".split(" "),
+ NODE_ATTR_MAP = {},
+ // Attribute names that should NOT be added to node.data
+ NONE_NODE_DATA_MAP = {"active": true, "children": true, "data": true, "focus": true};
+
+for(i=0; i
+ * For lazy nodes, null or undefined means 'not yet loaded'. Use an empty array
+ * to define a node that has no children.
+ * @property {boolean} expanded Use isExpanded(), setExpanded() to access this property.
+ * @property {string} extraClasses Addtional CSS classes, added to the node's `<span>`
+ * @property {boolean} folder Folder nodes have different default icons and click behavior.
+ * Note: Also non-folders may have children.
+ * @property {string} statusNodeType null or type of temporarily generated system node like 'loading', or 'error'.
+ * @property {boolean} lazy True if this node is loaded on demand, i.e. on first expansion.
+ * @property {boolean} selected Use isSelected(), setSelected() to access this property.
+ * @property {string} tooltip Alternative description used as hover banner
+ */
+function FancytreeNode(parent, obj){
+ var i, l, name, cl;
+
+ this.parent = parent;
+ this.tree = parent.tree;
+ this.ul = null;
+ this.li = null; //
tag
+ this.statusNodeType = null; // if this is a temp. node to display the status of its parent
+ this._isLoading = false; // if this node itself is loading
+ this._error = null; // {message: '...'} if a load error occured
+ this.data = {};
+
+ // TODO: merge this code with node.toDict()
+ // copy attributes from obj object
+ for(i=0, l=NODE_ATTRS.length; i= 0, "insertBefore must be an existing child");
+ // insert nodeList after children[pos]
+ this.children.splice.apply(this.children, [pos, 0].concat(nodeList));
+ }
+ if( !this.parent || this.parent.ul || this.tr ){
+ // render if the parent was rendered (or this is a root node)
+ this.render();
+ }
+ if( this.tree.options.selectMode === 3 ){
+ this.fixSelection3FromEndNodes();
+ }
+ return firstNode;
+ },
+ /**
+ * Append or prepend a node, or append a child node.
+ *
+ * This a convenience function that calls addChildren()
+ *
+ * @param {NodeData} node node definition
+ * @param {string} [mode=child] 'before', 'after', or 'child' ('over' is a synonym for 'child')
+ * @returns {FancytreeNode} new node
+ */
+ addNode: function(node, mode){
+ if(mode === undefined || mode === "over"){
+ mode = "child";
+ }
+ switch(mode){
+ case "after":
+ return this.getParent().addChildren(node, this.getNextSibling());
+ case "before":
+ return this.getParent().addChildren(node, this);
+ case "child":
+ case "over":
+ return this.addChildren(node);
+ }
+ _assert(false, "Invalid mode: " + mode);
+ },
+ /**
+ * Append new node after this.
+ *
+ * This a convenience function that calls addNode(node, 'after')
+ *
+ * @param {NodeData} node node definition
+ * @returns {FancytreeNode} new node
+ */
+ appendSibling: function(node){
+ return this.addNode(node, "after");
+ },
+ /**
+ * Modify existing child nodes.
+ *
+ * @param {NodePatch} patch
+ * @returns {$.Promise}
+ * @see FancytreeNode#addChildren
+ */
+ applyPatch: function(patch) {
+ // patch [key, null] means 'remove'
+ if(patch === null){
+ this.remove();
+ return _getResolvedPromise(this);
+ }
+ // TODO: make sure that root node is not collapsed or modified
+ // copy (most) attributes to node.ATTR or node.data.ATTR
+ var name, promise, v,
+ IGNORE_MAP = { children: true, expanded: true, parent: true }; // TODO: should be global
+
+ for(name in patch){
+ v = patch[name];
+ if( !IGNORE_MAP[name] && !$.isFunction(v)){
+ if(NODE_ATTR_MAP[name]){
+ this[name] = v;
+ }else{
+ this.data[name] = v;
+ }
+ }
+ }
+ // Remove and/or create children
+ if(patch.hasOwnProperty("children")){
+ this.removeChildren();
+ if(patch.children){ // only if not null and not empty list
+ // TODO: addChildren instead?
+ this._setChildren(patch.children);
+ }
+ // TODO: how can we APPEND or INSERT child nodes?
+ }
+ if(this.isVisible()){
+ this.renderTitle();
+ this.renderStatus();
+ }
+ // Expand collapse (final step, since this may be async)
+ if(patch.hasOwnProperty("expanded")){
+ promise = this.setExpanded(patch.expanded);
+ }else{
+ promise = _getResolvedPromise(this);
+ }
+ return promise;
+ },
+ /** Collapse all sibling nodes.
+ * @returns {$.Promise}
+ */
+ collapseSiblings: function() {
+ return this.tree._callHook("nodeCollapseSiblings", this);
+ },
+ /** Copy this node as sibling or child of `node`.
+ *
+ * @param {FancytreeNode} node source node
+ * @param {string} mode 'before' | 'after' | 'child'
+ * @param {Function} [map] callback function(NodeData) that could modify the new node
+ * @returns {FancytreeNode} new
+ */
+ copyTo: function(node, mode, map) {
+ return node.addNode(this.toDict(true, map), mode);
+ },
+ /** Count direct and indirect children.
+ *
+ * @param {boolean} [deep=true] pass 'false' to only count direct children
+ * @returns {int} number of child nodes
+ */
+ countChildren: function(deep) {
+ var cl = this.children, i, l, n;
+ if( !cl ){
+ return 0;
+ }
+ n = cl.length;
+ if(deep !== false){
+ for(i=0, l=n; i= 2 (prepending node info)
+ *
+ * @param {*} msg string or object or array of such
+ */
+ debug: function(msg){
+ if( this.tree.options.debugLevel >= 2 ) {
+ Array.prototype.unshift.call(arguments, this.toString());
+ consoleApply("debug", arguments);
+ }
+ },
+ /** Deprecated.
+ * @deprecated since 2014-02-16. Use resetLazy() instead.
+ */
+ discard: function(){
+ this.warn("FancytreeNode.discard() is deprecated since 2014-02-16. Use .resetLazy() instead.");
+ return this.resetLazy();
+ },
+ // TODO: expand(flag)
+ /**Find all nodes that contain `match` in the title.
+ *
+ * @param {string | function(node)} match string to search for, of a function that
+ * returns `true` if a node is matched.
+ * @returns {FancytreeNode[]} array of nodes (may be empty)
+ * @see FancytreeNode#findAll
+ */
+ findAll: function(match) {
+ match = $.isFunction(match) ? match : _makeNodeTitleMatcher(match);
+ var res = [];
+ this.visit(function(n){
+ if(match(n)){
+ res.push(n);
+ }
+ });
+ return res;
+ },
+ /**Find first node that contains `match` in the title (not including self).
+ *
+ * @param {string | function(node)} match string to search for, of a function that
+ * returns `true` if a node is matched.
+ * @returns {FancytreeNode} matching node or null
+ * @example
+ * fat text
+ */
+ findFirst: function(match) {
+ match = $.isFunction(match) ? match : _makeNodeTitleMatcher(match);
+ var res = null;
+ this.visit(function(n){
+ if(match(n)){
+ res = n;
+ return false;
+ }
+ });
+ return res;
+ },
+ /* Apply selection state (internal use only) */
+ _changeSelectStatusAttrs: function (state) {
+ var changed = false;
+
+ switch(state){
+ case false:
+ changed = ( this.selected || this.partsel );
+ this.selected = false;
+ this.partsel = false;
+ break;
+ case true:
+ changed = ( !this.selected || !this.partsel );
+ this.selected = true;
+ this.partsel = true;
+ break;
+ case undefined:
+ changed = ( this.selected || !this.partsel );
+ this.selected = false;
+ this.partsel = true;
+ break;
+ default:
+ _assert(false, "invalid state: " + state);
+ }
+ // this.debug("fixSelection3AfterLoad() _changeSelectStatusAttrs()", state, changed);
+ if( changed ){
+ this.renderStatus();
+ }
+ return changed;
+ },
+ /**
+ * Fix selection status, after this node was (de)selected in multi-hier mode.
+ * This includes (de)selecting all children.
+ */
+ fixSelection3AfterClick: function() {
+ var flag = this.isSelected();
+
+// this.debug("fixSelection3AfterClick()");
+
+ this.visit(function(node){
+ node._changeSelectStatusAttrs(flag);
+ });
+ this.fixSelection3FromEndNodes();
+ },
+ /**
+ * Fix selection status for multi-hier mode.
+ * Only end-nodes are considered to update the descendants branch and parents.
+ * Should be called after this node has loaded new children or after
+ * children have been modified using the API.
+ */
+ fixSelection3FromEndNodes: function() {
+// this.debug("fixSelection3FromEndNodes()");
+ _assert(this.tree.options.selectMode === 3, "expected selectMode 3");
+
+ // Visit all end nodes and adjust their parent's `selected` and `partsel`
+ // attributes. Return selection state true, false, or undefined.
+ function _walk(node){
+ var i, l, child, s, state, allSelected,someSelected,
+ children = node.children;
+
+ if( children && children.length ){
+ // check all children recursively
+ allSelected = true;
+ someSelected = false;
+
+ for( i=0, l=children.length; i= 1 (prepending node info)
+ *
+ * @param {*} msg string or object or array of such
+ */
+ info: function(msg){
+ if( this.tree.options.debugLevel >= 1 ) {
+ Array.prototype.unshift.call(arguments, this.toString());
+ consoleApply("info", arguments);
+ }
+ },
+ /** Return true if node is active (see also FancytreeNode#isSelected).
+ * @returns {boolean}
+ */
+ isActive: function() {
+ return (this.tree.activeNode === this);
+ },
+ /** Return true if node is a direct child of otherNode.
+ * @param {FancytreeNode} otherNode
+ * @returns {boolean}
+ */
+ isChildOf: function(otherNode) {
+ return (this.parent && this.parent === otherNode);
+ },
+ /** Return true, if node is a direct or indirect sub node of otherNode.
+ * @param {FancytreeNode} otherNode
+ * @returns {boolean}
+ */
+ isDescendantOf: function(otherNode) {
+ if(!otherNode || otherNode.tree !== this.tree){
+ return false;
+ }
+ var p = this.parent;
+ while( p ) {
+ if( p === otherNode ){
+ return true;
+ }
+ p = p.parent;
+ }
+ return false;
+ },
+ /** Return true if node is expanded.
+ * @returns {boolean}
+ */
+ isExpanded: function() {
+ return !!this.expanded;
+ },
+ /** Return true if node is the first node of its parent's children.
+ * @returns {boolean}
+ */
+ isFirstSibling: function() {
+ var p = this.parent;
+ return !p || p.children[0] === this;
+ },
+ /** Return true if node is a folder, i.e. has the node.folder attribute set.
+ * @returns {boolean}
+ */
+ isFolder: function() {
+ return !!this.folder;
+ },
+ /** Return true if node is the last node of its parent's children.
+ * @returns {boolean}
+ */
+ isLastSibling: function() {
+ var p = this.parent;
+ return !p || p.children[p.children.length-1] === this;
+ },
+ /** Return true if node is lazy (even if data was already loaded)
+ * @returns {boolean}
+ */
+ isLazy: function() {
+ return !!this.lazy;
+ },
+ /** Return true if node is lazy and loaded. For non-lazy nodes always return true.
+ * @returns {boolean}
+ */
+ isLoaded: function() {
+ return !this.lazy || this.hasChildren() !== undefined; // Also checks if the only child is a status node
+ },
+ /** Return true if children are currently beeing loaded, i.e. a Ajax request is pending.
+ * @returns {boolean}
+ */
+ isLoading: function() {
+ return !!this._isLoading;
+ },
+ /** Return true if this is the (invisible) system root node.
+ * @returns {boolean}
+ */
+ isRoot: function() {
+ return (this.tree.rootNode === this);
+ },
+ /** Return true if node is selected, i.e. has a checkmark set (see also FancytreeNode#isActive).
+ * @returns {boolean}
+ */
+ isSelected: function() {
+ return !!this.selected;
+ },
+ /** Return true if this node is a temporarily generated system node like
+ * 'loading', or 'error' (node.statusNodeType contains the type).
+ * @returns {boolean}
+ */
+ isStatusNode: function() {
+ return !!this.statusNodeType;
+ },
+ /** Return true if node is lazy and not yet loaded. For non-lazy nodes always return false.
+ * @returns {boolean}
+ */
+ isUndefined: function() {
+ return this.hasChildren() === undefined; // also checks if the only child is a status node
+ },
+ /** Return true if all parent nodes are expanded. Note: this does not check
+ * whether the node is scrolled into the visible part of the screen.
+ * @returns {boolean}
+ */
+ isVisible: function() {
+ var i, l,
+ parents = this.getParentList(false, false);
+
+ for(i=0, l=parents.length; i= 0; i--){
+ // that.debug("pushexpand" + parents[i]);
+ deferreds.push(parents[i].setExpanded(true, opts));
+ }
+ $.when.apply($, deferreds).done(function(){
+ // All expands have finished
+ // that.debug("expand DONE", scroll);
+ if( scroll ){
+ that.scrollIntoView(effects).done(function(){
+ // that.debug("scroll DONE");
+ dfd.resolve();
+ });
+ } else {
+ dfd.resolve();
+ }
+ });
+ return dfd.promise();
+ },
+ /** Move this node to targetNode.
+ * @param {FancytreeNode} targetNode
+ * @param {string} mode
+ * 'child': append this node as last child of targetNode.
+ * This is the default. To be compatble with the D'n'd
+ * hitMode, we also accept 'over'.
+ * 'before': add this node as sibling before targetNode.
+ * 'after': add this node as sibling after targetNode.
tag yet:
+// if( !targetParent.ul ) {
+// // This is the parent's first child: create UL tag
+// // (Hidden, because it will be
+// targetParent.ul = document.createElement("ul");
+// targetParent.ul.style.display = "none";
+// targetParent.li.appendChild(targetParent.ul);
+// }
+// // Issue 319: Add to target DOM parent (only if node was already rendered(expanded))
+// if(this.li){
+// targetParent.ul.appendChild(this.li);
+// }^
+
+ // Let caller modify the nodes
+ if( map ){
+ targetNode.visit(map, true);
+ }
+ // Handle cross-tree moves
+ if( this.tree !== targetNode.tree ) {
+ // Fix node.tree for all source nodes
+// _assert(false, "Cross-tree move is not yet implemented.");
+ this.warn("Cross-tree moveTo is experimantal!");
+ this.visit(function(n){
+ // TODO: fix selection state and activation, ...
+ n.tree = targetNode.tree;
+ }, true);
+ }
+
+ // A collaposed node won't re-render children, so we have to remove it manually
+ // if( !targetParent.expanded ){
+ // prevParent.ul.removeChild(this.li);
+ // }
+
+ // Update HTML markup
+ if( !prevParent.isDescendantOf(targetParent)) {
+ prevParent.render();
+ }
+ if( !targetParent.isDescendantOf(prevParent) && targetParent !== prevParent) {
+ targetParent.render();
+ }
+ // TODO: fix selection state
+ // TODO: fix active state
+
+/*
+ var tree = this.tree;
+ var opts = tree.options;
+ var pers = tree.persistence;
+
+
+ // Always expand, if it's below minExpandLevel
+// tree.logDebug ("%s._addChildNode(%o), l=%o", this, ftnode, ftnode.getLevel());
+ if ( opts.minExpandLevel >= ftnode.getLevel() ) {
+// tree.logDebug ("Force expand for %o", ftnode);
+ this.bExpanded = true;
+ }
+
+ // In multi-hier mode, update the parents selection state
+ // DT issue #82: only if not initializing, because the children may not exist yet
+// if( !ftnode.data.isStatusNode() && opts.selectMode==3 && !isInitializing )
+// ftnode._fixSelectionState();
+
+ // In multi-hier mode, update the parents selection state
+ if( ftnode.bSelected && opts.selectMode==3 ) {
+ var p = this;
+ while( p ) {
+ if( !p.hasSubSel )
+ p._setSubSel(true);
+ p = p.parent;
+ }
+ }
+ // render this node and the new child
+ if ( tree.bEnableUpdate )
+ this.render();
+
+ return ftnode;
+
+*/
+ },
+ /** Set focus relative to this node and optionally activate.
+ *
+ * @param {number} where The keyCode that would normally trigger this move,
+ * e.g. `$.ui.keyCode.LEFT` would collapse the node if it
+ * is expanded or move to the parent oterwise.
+ * @param {boolean} [activate=true]
+ * @returns {$.Promise}
+ */
+ navigate: function(where, activate) {
+ var i, parents,
+ handled = true,
+ KC = $.ui.keyCode,
+ sib = null;
+
+ // Navigate to node
+ function _goto(n){
+ if( n ){
+ try { n.makeVisible(); } catch(e) {} // #272
+ // Node may still be hidden by a filter
+ if( ! $(n.span).is(":visible") ) {
+ n.debug("Navigate: skipping hidden node");
+ n.navigate(where, activate);
+ return;
+ }
+ return activate === false ? n.setFocus() : n.setActive();
+ }
+ }
+
+ switch( where ) {
+ case KC.BACKSPACE:
+ if( this.parent && this.parent.parent ) {
+ _goto(this.parent);
+ }
+ break;
+ case KC.LEFT:
+ if( this.expanded ) {
+ this.setExpanded(false);
+ _goto(this);
+ } else if( this.parent && this.parent.parent ) {
+ _goto(this.parent);
+ }
+ break;
+ case KC.RIGHT:
+ if( !this.expanded && (this.children || this.lazy) ) {
+ this.setExpanded();
+ _goto(this);
+ } else if( this.children && this.children.length ) {
+ _goto(this.children[0]);
+ }
+ break;
+ case KC.UP:
+ sib = this.getPrevSibling();
+ while( sib && sib.expanded && sib.children && sib.children.length ){
+ sib = sib.children[sib.children.length - 1];
+ }
+ if( !sib && this.parent && this.parent.parent ){
+ sib = this.parent;
+ }
+ _goto(sib);
+ break;
+ case KC.DOWN:
+ if( this.expanded && this.children && this.children.length ) {
+ sib = this.children[0];
+ } else {
+ parents = this.getParentList(false, true);
+ for(i=parents.length-1; i>=0; i--) {
+ sib = parents[i].getNextSibling();
+ if( sib ){ break; }
+ }
+ }
+ _goto(sib);
+ break;
+ default:
+ handled = false;
+ }
+ },
+ /**
+ * Remove this node (not allowed for system root).
+ */
+ remove: function() {
+ return this.parent.removeChild(this);
+ },
+ /**
+ * Remove childNode from list of direct children.
+ * @param {FancytreeNode} childNode
+ */
+ removeChild: function(childNode) {
+ return this.tree._callHook("nodeRemoveChild", this, childNode);
+ },
+ /**
+ * Remove all child nodes and descendents. This converts the node into a leaf.
+ * If this was a lazy node, it is still considered 'loaded'; call node.resetLazy()
+ * in order to trigger lazyLoad on next expand.
+ */
+ removeChildren: function() {
+ return this.tree._callHook("nodeRemoveChildren", this);
+ },
+ /**
+ * This method renders and updates all HTML markup that is required
+ * to display this node in its current state.
+ * Note:
+ *
+ *
It should only be neccessary to call this method after the node object
+ * was modified by direct access to its properties, because the common
+ * API methods (node.setTitle(), moveTo(), addChildren(), remove(), ...)
+ * already handle this.
+ *
{@link FancytreeNode#renderTitle} and {@link FancytreeNode#renderStatus}
+ * are implied. If changes are more local, calling only renderTitle() or
+ * renderStatus() may be sufficient and faster.
+ *
If a node was created/removed, node.render() must be called on the parent.
+ *
+ *
+ * @param {boolean} [force=false] re-render, even if html markup was already created
+ * @param {boolean} [deep=false] also render all descendants, even if parent is collapsed
+ */
+ render: function(force, deep) {
+ return this.tree._callHook("nodeRender", this, force, deep);
+ },
+ /** Create HTML markup for the node's outer (expander, checkbox, icon, and title).
+ * @see Fancytree_Hooks#nodeRenderTitle
+ */
+ renderTitle: function() {
+ return this.tree._callHook("nodeRenderTitle", this);
+ },
+ /** Update element's CSS classes according to node state.
+ * @see Fancytree_Hooks#nodeRenderStatus
+ */
+ renderStatus: function() {
+ return this.tree._callHook("nodeRenderStatus", this);
+ },
+ /**
+ * Remove all children, collapse, and set the lazy-flag, so that the lazyLoad
+ * event is triggered on next expand.
+ */
+ resetLazy: function() {
+ this.removeChildren();
+ this.expanded = false;
+ this.lazy = true;
+ this.children = undefined;
+ this.renderStatus();
+ },
+ /** Schedule activity for delayed execution (cancel any pending request).
+ * scheduleAction('cancel') will only cancel a pending request (if any).
+ * @param {string} mode
+ * @param {number} ms
+ */
+ scheduleAction: function(mode, ms) {
+ if( this.tree.timer ) {
+ clearTimeout(this.tree.timer);
+// this.tree.debug("clearTimeout(%o)", this.tree.timer);
+ }
+ this.tree.timer = null;
+ var self = this; // required for closures
+ switch (mode) {
+ case "cancel":
+ // Simply made sure that timer was cleared
+ break;
+ case "expand":
+ this.tree.timer = setTimeout(function(){
+ self.tree.debug("setTimeout: trigger expand");
+ self.setExpanded(true);
+ }, ms);
+ break;
+ case "activate":
+ this.tree.timer = setTimeout(function(){
+ self.tree.debug("setTimeout: trigger activate");
+ self.setActive(true);
+ }, ms);
+ break;
+ default:
+ throw "Invalid mode " + mode;
+ }
+// this.tree.debug("setTimeout(%s, %s): %s", mode, ms, this.tree.timer);
+ },
+ /**
+ *
+ * @param {boolean | PlainObject} [effects=false] animation options.
+ * @param {object} [options=null] {topNode: null, effects: ..., parent: ...} this node will remain visible in
+ * any case, even if `this` is outside the scroll pane.
+ * @returns {$.Promise}
+ */
+ scrollIntoView: function(effects, options) {
+ if( options !== undefined && _isNode(options) ) {
+ this.warn("scrollIntoView() with 'topNode' option is deprecated since 2014-05-08. Use 'options.topNode' instead.");
+ options = {topNode: options};
+ }
+ // this.$scrollParent = (this.options.scrollParent === "auto") ? $ul.scrollParent() : $(this.options.scrollParent);
+ // this.$scrollParent = this.$scrollParent.length ? this.$scrollParent || this.$container;
+
+ var topNodeY, nodeY, horzScrollbarHeight, containerOffsetTop,
+ opts = $.extend({
+ effects: (effects === true) ? {duration: 200, queue: false} : effects,
+ scrollOfs: this.tree.options.scrollOfs,
+ scrollParent: this.tree.options.scrollParent || this.tree.$container,
+ topNode: null
+ }, options),
+ dfd = new $.Deferred(),
+ that = this,
+ nodeHeight = $(this.span).height(),
+ $container = $(opts.scrollParent),
+ topOfs = opts.scrollOfs.top || 0,
+ bottomOfs = opts.scrollOfs.bottom || 0,
+ containerHeight = $container.height(),// - topOfs - bottomOfs,
+ scrollTop = $container.scrollTop(),
+ $animateTarget = $container,
+ isParentWindow = $container[0] === window,
+ topNode = opts.topNode || null,
+ newScrollTop = null;
+
+ // this.debug("scrollIntoView(), scrollTop=", scrollTop, opts.scrollOfs);
+ _assert($(this.span).is(":visible"), "scrollIntoView node is invisible"); // otherwise we cannot calc offsets
+
+ if( isParentWindow ) {
+ nodeY = $(this.span).offset().top;
+ topNodeY = topNode ? $(topNode.span).offset().top : 0;
+ $animateTarget = $("html,body");
+
+ } else {
+ _assert($container[0] !== document && $container[0] !== document.body, "scrollParent should be an simple element or `window`, not document or body.");
+
+ containerOffsetTop = $container.offset().top,
+ nodeY = $(this.span).offset().top - containerOffsetTop + scrollTop; // relative to scroll parent
+ topNodeY = topNode ? $(topNode.span).offset().top - containerOffsetTop + scrollTop : 0;
+ horzScrollbarHeight = Math.max(0, ($container.innerHeight() - $container[0].clientHeight));
+ containerHeight -= horzScrollbarHeight;
+ }
+
+ // this.debug(" scrollIntoView(), nodeY=", nodeY, "containerHeight=", containerHeight);
+ if( nodeY < (scrollTop + topOfs) ){
+ // Node is above visible container area
+ newScrollTop = nodeY - topOfs;
+ // this.debug(" scrollIntoView(), UPPER newScrollTop=", newScrollTop);
+
+ }else if((nodeY + nodeHeight) > (scrollTop + containerHeight - bottomOfs)){
+ newScrollTop = nodeY + nodeHeight - containerHeight + bottomOfs;
+ // this.debug(" scrollIntoView(), LOWER newScrollTop=", newScrollTop);
+ // If a topNode was passed, make sure that it is never scrolled
+ // outside the upper border
+ if(topNode){
+ _assert($(topNode.span).is(":visible"));
+ if( topNodeY < newScrollTop ){
+ newScrollTop = topNodeY - topOfs;
+ // this.debug(" scrollIntoView(), TOP newScrollTop=", newScrollTop);
+ }
+ }
+ }
+
+ if(newScrollTop !== null){
+ // this.debug(" scrollIntoView(), SET newScrollTop=", newScrollTop);
+ if(opts.effects){
+ opts.effects.complete = function(){
+ dfd.resolveWith(that);
+ };
+ $animateTarget.stop(true).animate({
+ scrollTop: newScrollTop
+ }, opts.effects);
+ }else{
+ $animateTarget[0].scrollTop = newScrollTop;
+ dfd.resolveWith(this);
+ }
+ }else{
+ dfd.resolveWith(this);
+ }
+ return dfd.promise();
+ },
+
+ /**Activate this node.
+ * @param {boolean} [flag=true] pass false to deactivate
+ * @param {object} [opts] additional options. Defaults to {noEvents: false}
+ */
+ setActive: function(flag, opts){
+ return this.tree._callHook("nodeSetActive", this, flag, opts);
+ },
+ /**Expand or collapse this node. Promise is resolved, when lazy loading and animations are done.
+ * @param {boolean} [flag=true] pass false to collapse
+ * @param {object} [opts] additional options. Defaults to {noAnimation: false, noEvents: false}
+ * @returns {$.Promise}
+ */
+ setExpanded: function(flag, opts){
+ return this.tree._callHook("nodeSetExpanded", this, flag, opts);
+ },
+ /**Set keyboard focus to this node.
+ * @param {boolean} [flag=true] pass false to blur
+ * @see Fancytree#setFocus
+ */
+ setFocus: function(flag){
+ return this.tree._callHook("nodeSetFocus", this, flag);
+ },
+ /**Select this node, i.e. check the checkbox.
+ * @param {boolean} [flag=true] pass false to deselect
+ */
+ setSelected: function(flag){
+ return this.tree._callHook("nodeSetSelected", this, flag);
+ },
+ /**Mark a lazy node as 'error', 'loading', or 'ok'.
+ * @param {string} status 'error', 'ok'
+ * @param {string} [message]
+ * @param {string} [details]
+ */
+ setStatus: function(status, message, details){
+ return this.tree._callHook("nodeSetStatus", this, status, message, details);
+ },
+ /**Rename this node.
+ * @param {string} title
+ */
+ setTitle: function(title){
+ this.title = title;
+ this.renderTitle();
+ },
+ /**Sort child list by title.
+ * @param {function} [cmp] custom compare function(a, b) that returns -1, 0, or 1 (defaults to sort by title).
+ * @param {boolean} [deep=false] pass true to sort all descendant nodes
+ */
+ sortChildren: function(cmp, deep) {
+ var i,l,
+ cl = this.children;
+
+ if( !cl ){
+ return;
+ }
+ cmp = cmp || function(a, b) {
+ var x = a.title.toLowerCase(),
+ y = b.title.toLowerCase();
+ return x === y ? 0 : x > y ? 1 : -1;
+ };
+ cl.sort(cmp);
+ if( deep ){
+ for(i=0, l=cl.length; i";
+ },
+ /** Call fn(node) for all child nodes.
+ * Stop iteration, if fn() returns false. Skip current branch, if fn() returns "skip".
+ * Return false if iteration was stopped.
+ *
+ * @param {function} fn the callback function.
+ * Return false to stop iteration, return "skip" to skip this node and children only.
+ * @param {boolean} [includeSelf=false]
+ * @returns {boolean}
+ */
+ visit: function(fn, includeSelf) {
+ var i, l,
+ res = true,
+ children = this.children;
+
+ if( includeSelf === true ) {
+ res = fn(this);
+ if( res === false || res === "skip" ){
+ return res;
+ }
+ }
+ if(children){
+ for(i=0, l=children.length; i
+ * Stop iteration, if fn() returns false.
+ * Return false if iteration was stopped.
+ *
+ * @param {function} fn the callback function.
+ * Return false to stop iteration, return "skip" to skip this node and children only.
+ * @param {boolean} [includeSelf=false]
+ * @returns {boolean}
+ */
+ visitParents: function(fn, includeSelf) {
+ // Visit parent nodes (bottom up)
+ if(includeSelf && fn(this) === false){
+ return false;
+ }
+ var p = this.parent;
+ while( p ) {
+ if(fn(p) === false){
+ return false;
+ }
+ p = p.parent;
+ }
+ return true;
+ },
+ /** Write warning to browser console (prepending node info)
+ *
+ * @param {*} msg string or object or array of such
+ */
+ warn: function(msg){
+ Array.prototype.unshift.call(arguments, this.toString());
+ consoleApply("warn", arguments);
+ }
+};
+
+
+/* *****************************************************************************
+ * Fancytree
+ */
+/**
+ * Construct a new tree object.
+ *
+ * @class Fancytree
+ * @classdesc The controller behind a fancytree.
+ * This class also contains 'hook methods': see {@link Fancytree_Hooks}.
+ *
+ * @param {Widget} widget
+ *
+ * @property {FancytreeOptions} options
+ * @property {FancytreeNode} rootNode
+ * @property {FancytreeNode} activeNode
+ * @property {FancytreeNode} focusNode
+ * @property {jQueryObject} $div
+ * @property {object} widget
+ * @property {object} ext
+ * @property {object} data
+ * @property {object} options
+ * @property {string} _id
+ * @property {string} statusClassPropName
+ * @property {string} ariaPropName
+ * @property {string} nodeContainerAttrName
+ * @property {string} $container
+ * @property {FancytreeNode} lastSelectedNode
+ */
+function Fancytree(widget) {
+ this.widget = widget;
+ this.$div = widget.element;
+ this.options = widget.options;
+ if( this.options && $.isFunction(this.options.lazyload) ) {
+ if( ! $.isFunction(this.options.lazyLoad ) ) {
+ this.options.lazyLoad = function() {
+ FT.warn("The 'lazyload' event is deprecated since 2014-02-25. Use 'lazyLoad' (with uppercase L) instead.");
+ widget.options.lazyload.apply(this, arguments);
+ };
+ }
+ }
+ if( this.options && $.isFunction(this.options.loaderror) ) {
+ $.error("The 'loaderror' event was renamed since 2014-07-03. Use 'loadError' (with uppercase E) instead.");
+ }
+ this.ext = {}; // Active extension instances
+ // allow to init tree.data.foo from
+ *
+ *
+ * @param {EventData} ctx
+ * @param {boolean} [force=false] re-render, even if html markup was already created
+ * @param {boolean} [deep=false] also render all descendants, even if parent is collapsed
+ * @param {boolean} [collapsed=false] force root node to be collapsed, so we can apply animated expand later
+ */
+ nodeRender: function(ctx, force, deep, collapsed, _recursive) {
+ /* This method must take care of all cases where the current data mode
+ * (i.e. node hierarchy) does not match the current markup.
+ *
+ * - node was not yet rendered:
+ * create markup
+ * - node was rendered: exit fast
+ * - children have been added
+ * - childern have been removed
+ */
+ var childLI, childNode1, childNode2, i, l, next, subCtx,
+ node = ctx.node,
+ tree = ctx.tree,
+ opts = ctx.options,
+ aria = opts.aria,
+ firstTime = false,
+ parent = node.parent,
+ isRootNode = !parent,
+ children = node.children;
+ // FT.debug("nodeRender(" + !!force + ", " + !!deep + ")", node.toString());
+
+ if( ! isRootNode && ! parent.ul ) {
+ // Calling node.collapse on a deep, unrendered node
+ return;
+ }
+ _assert(isRootNode || parent.ul, "parent UL must exist");
+
+// if(node.li && (force || (node.li.parentNode !== node.parent.ul) ) ){
+// if(node.li.parentNode !== node.parent.ul){
+// // alert("unlink " + node + " (must be child of " + node.parent + ")");
+// this.warn("unlink " + node + " (must be child of " + node.parent + ")");
+// }
+// // this.debug("nodeRemoveMarkup...");
+// this.nodeRemoveMarkup(ctx);
+// }
+ // Render the node
+ if( !isRootNode ){
+ // Discard markup on force-mode, or if it is not linked to parent