This repository has been archived on 2024-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
yii2-fancytree-widget/assets/src/jquery.fancytree.wide.js
Wanderson Braganca a9dcb2c50a initial commit
2014-06-04 16:56:14 -03:00

114 lines
3.4 KiB
JavaScript

/*!
*
* jquery.fancytree.wide.js
* Support for 100% wide selection bars.
* (Extension module for jquery.fancytree.js: https://github.com/mar10/fancytree/)
*
* Copyright (c) 2014, Martin Wendt (http://wwWendt.de)
*
* Released under the MIT license
* https://github.com/mar10/fancytree/wiki/LicenseInfo
*
* @version 2.1.0
* @date 2014-05-29T16:44
*/
;(function($, window, document, undefined) {
"use strict";
/*******************************************************************************
* Private functions and variables
*/
// function _assert(cond, msg){
// // TODO: see qunit.js extractStacktrace()
// if(!cond){
// msg = msg ? ": " + msg : "";
// $.error("Assertion failed" + msg);
// }
// }
/**
* [ext-wide] Recalculate the width of the selection bar after the tree container
* was resized.<br>
* May be called explicitly on container resize, since there is no resize event
* for DIV tags.
*
* @alias Fancytree#wideUpdate
* @requires jquery.fancytree.wide.js
*/
$.ui.fancytree._FancytreeClass.prototype.wideUpdate = function(){
var inst = this.ext.wide,
prevCw = inst.contWidth,
prevLo = inst.lineOfs;
// http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/
inst.contWidth = parseFloat(this.$container.css("width"), 10);
// Each title is precceeded by 2 or 3 icons (16px + 3 margin)
// + 1px title border and 3px title padding
inst.lineOfs = (this.options.checkbox ? 3 : 2) * 19;
if( prevCw !== inst.contWidth || prevLo !== inst.lineOfs ) {
this.debug("wideUpdate: " + inst.contWidth);
this.visit(function(node){
node.tree._callHook("nodeRenderTitle", node);
});
}
};
/*******************************************************************************
* Extension code
*/
$.ui.fancytree.registerExtension({
name: "wide",
version: "0.0.2",
// Default options for this extension.
options: {
autoResize: true, // call wideUpdate() on window.resize events
cheap: false, // true: use static css only
margin: {left: 3, right: 3} // free margins near the selection bar
},
treeCreate: function(ctx){
this.$container.addClass("fancytree-ext-wide");
this._super(ctx);
// http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/
this._local.contWidth = parseFloat(ctx.tree.$container.css("width"), 10);
// Every nested UL is indented by 16px
// Each title is precceeded by 2 or 3 icons (16px + 3 margin)
// + 1px title border and 3px title padding
this._local.lineOfs = (ctx.options.checkbox ? 3 : 2) * 19;
this._local.levelOfs = 16;
this._local.titlePadding = 3;
$(window).on("resize" + ctx.widget.eventNamespace, $.ui.fancytree.debounce(200, function(event){
if( ctx.options.wide.autoResize && !ctx.options.wide.cheap ) {
ctx.tree.wideUpdate();
}
}));
},
treeDestroy: function(ctx){
$(window).off("resize" + ctx.widget.eventNamespace);
this._super(ctx);
},
nodeRenderTitle: function(ctx) {
var ofs, res, margin,
instOpts = ctx.options.wide,
inst = this._local,
cw = inst.contWidth,
node = ctx.node;
res = this._super(ctx);
if( !instOpts.cheap ) {
margin = instOpts.margin;
ofs = (node.getLevel() - 1) * inst.levelOfs + inst.lineOfs;
$(node.span).find(".fancytree-title").css({
width: cw - margin.left - margin.right - ofs,
marginLeft: -ofs + margin.left,
paddingLeft: +ofs - margin.left + inst.titlePadding,
paddingRight: inst.titlePadding
});
}
return res;
}
});
}(jQuery, window, document));