123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
-
- (function($) {
-
-
-
- var options;
- var defaultPaddingLeft;
- var persistStore;
-
- $.fn.treeTable = function(opts) {
- options = $.extend({}, $.fn.treeTable.defaults, opts);
-
- if(options.persist) {
- persistStore = new Persist.Store(options.persistStoreName);
- }
-
- return this.each(function() {
- $(this).addClass("treeTable").find("tbody tr").each(function() {
-
- if (!$(this).hasClass('initialized')) {
- var isRootNode = ($(this)[0].className.search(options.childPrefix) == -1);
-
-
-
-
- if (isRootNode && isNaN(defaultPaddingLeft)) {
- defaultPaddingLeft = options.initialIndent + parseInt($($(this).children("td")[options.treeColumn]).css('padding-left'), 10);
- }
-
-
- if(!isRootNode && options.expandable && options.initialState == "collapsed") {
- $(this).addClass('ui-helper-hidden');
- }
-
-
-
- if(!options.expandable || isRootNode) {
- initialize($(this));
- }
- }
- });
- });
- };
-
- $.fn.treeTable.defaults = {
- childPrefix: "child-of-",
- clickableElement: false,
- doubleclickMode: false,
- expandable: true,
- indent: 19,
- initialIndent: 0,
- initialState: "collapsed",
- initialRootState: "collapsed",
- onNodeShow: null,
- onNodeHide: null,
- onExpandableInit: null,
- onNonExpandableInit: null,
- treeColumn: 0,
- persist: false,
- persistStoreName: 'treeTable',
- stringExpand: "Expand",
- stringCollapse: "Collapse"
- };
-
-
- $.fn.expandAll = function() {
- $(this).find("tr").each(function() {
- $(this).expand();
- });
- };
-
-
- $.fn.collapseAll = function() {
- $(this).find("tr").each(function() {
- $(this).collapse();
- });
- };
-
-
- $.fn.collapse = function() {
- return this.each(function() {
- $(this).removeClass("expanded").addClass("collapsed");
-
- if (options.persist) {
- persistNodeState($(this));
- }
-
- childrenOf($(this)).each(function() {
- if(!$(this).hasClass("collapsed")) {
- $(this).collapse();
- }
-
- $(this).addClass('ui-helper-hidden');
-
- if($.isFunction(options.onNodeHide)) {
- options.onNodeHide.call(this);
- }
-
- });
- });
- };
-
-
- $.fn.expand = function() {
- return this.each(function() {
- $(this).removeClass("collapsed").addClass("expanded");
-
- if (options.persist) {
- persistNodeState($(this));
- }
-
- childrenOf($(this)).each(function() {
- initialize($(this));
-
- if($(this).is(".expanded.parent")) {
- $(this).expand();
- }
-
- $(this).removeClass('ui-helper-hidden');
-
- if($.isFunction(options.onNodeShow)) {
- options.onNodeShow.call(this);
- }
- });
- });
- };
-
-
- $.fn.reveal = function() {
- $(ancestorsOf($(this)).reverse()).each(function() {
- initialize($(this));
- $(this).expand().show();
- });
-
- return this;
- };
-
-
- $.fn.appendBranchTo = function(destination) {
- var node = $(this);
- var parent = parentOf(node);
- var target = $(destination);
-
- var ancestorNames = $.map(ancestorsOf(target), function(a) { return a.id; });
-
-
-
-
-
-
-
-
- if($.inArray(node[0].id, ancestorNames) == -1 && (!parent || (target.id != parent[0].id)) && target.id != node[0].id) {
- insert(node, 'after', target);
- if(parent) { node.removeClass(options.childPrefix + parent[0].id); }
- node.addClass(options.childPrefix + target[0].id);
- indent(node, ancestorsOf(node).length * options.indent);
- }
-
- return this;
- };
-
-
- $.fn.moveBranch = function(where, element) {
-
- if(where == 'in') { $(this).appendBranchTo(element); return; }
-
- if($.inArray(where, ['before','after']) == -1) { return; }
-
- var node = $(this);
- var parent = parentOf(node);
-
- var target = $(element);
- var targetParent = parentOf(target);
-
- var ancestorNames = $.map(ancestorsOf(target), function(a) { return a.id; });
-
-
-
-
-
- if($.inArray(node[0].id, ancestorNames) == -1 && target[0].id != node[0].id) {
- insert(node, where, target);
- if(parent) { node.removeClass(options.childPrefix + parent[0].id); }
- if(targetParent) { node.addClass(options.childPrefix + targetParent[0].id); }
- indent(node, ancestorsOf(node).length * options.indent);
- }
-
- return this;
- };
-
-
- $.fn.reverse = function() {
- return this.pushStack(this.get().reverse(), arguments);
- };
-
-
- $.fn.toggleBranch = function() {
- if($(this).hasClass("collapsed")) {
- $(this).expand();
- } else {
- $(this).collapse();
- }
-
- return this;
- };
-
-
- $.fn.nodeParent = function () {
- var $node = $(this);
- var match = $node[0].className.match(new RegExp(options.childPrefix+'[^\\s]+', 'i'));
- return (match) ? $('#node-'+match[0].substring(14)) : null;
- }
-
-
- $.fn.nodeReinitialize = function() {
- reinitialize($(this));
-
- return this;
- };
-
-
- $.fn.selectBranch = function() {
- return selectBranch(this);
- };
-
-
- $.fn.branchLast = function() {
- return branchLast(this);
- };
-
-
-
- function ancestorsOf(node) {
- var ancestors = [];
- while(node = parentOf(node)) {
- ancestors[ancestors.length] = node[0];
- }
- return ancestors;
- };
-
- function childrenOf(node) {
- return $(node).siblings("tr." + options.childPrefix + node[0].id);
- };
-
-
-
- function branchLast(node) {
- return (childrenOf(node).length) ? branchLast(childrenOf(node).last()) : $(node);
- };
-
-
-
-
-
-
-
- function selectBranch(node) {
- var nodelast = branchLast(node);
- return (node[0].id === nodelast[0].id) ? node : node.nextUntil(nodelast).addBack().add(nodelast);
- };
-
- function getPaddingLeft(node) {
- return ancestorsOf(node).length * options.indent;
- }
-
- function indent(node, value) {
- var cell = $(node.children("td")[options.treeColumn]);
- cell[0].style.paddingLeft = options.initialIndent + value + 'px';
-
- childrenOf(node).each(function() {
- indent($(this), value + options.indent);
- });
- };
-
- function initialize(node) {
- if(!node.hasClass("initialized")) {
- node.addClass("initialized");
-
- var isRootNode = (node[0].className.search(options.childPrefix) == -1);
- var childNodes = childrenOf(node);
- var expandable = childNodes.length > 0;
-
- if(!node.hasClass("parent") && expandable) {
- node.addClass("parent");
- }
-
- if($.isFunction(options.onNodeInit)) {
- options.onNodeInit.call(this, node, expandable, isRootNode);
- }
-
- if(expandable) {
- indent(node, getPaddingLeft(node));
-
- if(options.expandable) {
- var handle = (options.clickableElement) ? node.find(options.clickableElement) : node;
- handle.attr('title', options.stringExpand).addClass('expander');
-
- handle.on((options.doubleclickMode) ? 'dblclick' : 'click', function(e){
- e.preventDefault;
- node.toggleBranch();
- });
-
- if (options.persist && getPersistedNodeState(node)) {
- node.addClass('expanded');
- }
-
-
- if(!(node.hasClass("expanded") || node.hasClass("collapsed"))) {
- (isRootNode) ? node.addClass(options.initialRootState) : node.addClass(options.initialState);
- }
-
- if(node.hasClass("expanded")) {
- node.expand();
- }
- }
- }
- }
- };
-
- function reinitialize(node) {
- if(node.hasClass("initialized")) {
- node.removeClass('initialized').removeClass('parent')
- .removeClass('expanded').removeClass('collapsed');
-
- var isRootNode = (node[0].className.search(options.childPrefix) == -1);
- var childNodes = childrenOf(node);
- var expandable = childNodes.length > 0;
-
- if(options.expandable) {
- var handle = (options.clickableElement) ? node.find(options.clickableElement) : node;
- handle.removeAttr('title').removeClass('expander');
-
- handle.off((options.doubleclickMode) ? 'dblclick' : 'click');
- }
-
- if($.isFunction(options.onNodeReinit)) {
- options.onNodeReinit.call(this, node, expandable, isRootNode);
- }
-
- if(expandable) { node.addClass('expanded'); }
- initialize(node);
- }
- };
-
-
-
-
-
- function insert(node, where, target) {
- if(where == 'before') {
-
-
- selectBranch(node).insertBefore(target);
- }
- if(where == 'after') {
-
-
- var targetlast = branchLast(target);
- selectBranch(node).insertAfter(targetlast);
- }
- };
-
- function parentOf(node) {
- var classNames = node[0].className.split(' ');
-
- for(var key=0; key<classNames.length; key++) {
- if(classNames[key].match(options.childPrefix)) {
- return $(node).siblings("#" + classNames[key].substring(options.childPrefix.length));
- }
- }
-
- return null;
- };
-
-
- function persistNodeState(node) {
- if(node.hasClass('expanded')) {
- try {
- persistStore.set(node.attr('id'), '1');
- } catch (err) {
-
- }
- } else {
- try {
- persistStore.remove(node.attr('id'));
- } catch (err) {
-
- }
- }
- }
-
- function getPersistedNodeState(node) {
- try {
- return persistStore.get(node.attr('id')) == '1';
- } catch (err) {
- return false;
- }
- }
- })(jQuery);
-
|