forked from mattrand/EasyTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.easytree.min.js
10 lines (9 loc) · 16.9 KB
/
jquery.easytree.min.js
1
2
3
4
5
6
7
8
9
10
/*!
* jQuery EasyTree Plugin
* http://www.EasyJsTree.com
*
* Copyright 2014 Matthew Rand
* Released under the MIT license
* V1.0.1
*/
(function ($) { $.fn.easytree = function (a) { var b = this; var c = new F(b, a); return c }; var F = function (z, A) { var B = { allowActivate: true, data: null, dataUrl: null, dataUrlJson: null, disableIcons: false, enableDnd: false, ordering: null, slidingTime: 100, minOpenLevels: 0, building: null, built: null, toggling: null, toggled: null, opening: null, opened: null, openLazyNode: null, closing: null, closed: null, canDrop: null, dropping: null, dropped: null, stateChanged: null }; var C; var D = null; var E = new Object(); this.init = function (b, c) { B = $.extend(B, c); init(); C = b; var d = ''; if (B.dataUrl) { ajaxService(B.dataUrl, B.dataUrlJson, function (a) { d = convertInputDataToJson(a); if (!d) { alert("EasyTree: Invalid data!"); return this } buildTree(d); return this }) } else if (B.data) { d = convertInputDataToJson(B.data); if (!d) { alert("EasyTree: Invalid data!"); return this } buildTree(d) } else { d = convertInputDataToJson(C.html()); if (!d) { alert("EasyTree: Invalid data!"); return this } buildTree(d) } return this }; this.options = B; this.rebuildTree = function (a) { var b = a ? convertInputDataToJson(a) : D; if (!b) { alert("EasyTree: Invalid data!") } buildTree(b) }; this.getAllNodes = function () { return D }; this.getNode = function (a) { return getNode(D, a) }; this.addNode = function (a, b) { if (!b) { D.push(a); return } var c = getNode(D, b); if (!a) { return } if (!c.children) { c.children = [] } c.children.push(a) }; this.removeNode = function (a) { removeNode(D, a) }; this.activateNode = function (a) { unactivateAll(D); if (!B.allowActivate) { return } var b = getNode(D, a); if (!b) { return } b.isActive = true; $('#' + b.id).addClass('easytree-active') }; this.toggleNode = function (a) { var b = getNode(D, a); if (!b) { return } toggleNodeBegin(event, D, b) }; function nodeClick(a) { var b = getElementId(this); var c = a.data; var d = getNode(c, b); if (!d) { return } unactivateAll(c); if (!B.allowActivate) { return } d.isActive = true; $('#' + d.id).addClass('easytree-active'); if (B.stateChanged) { var j = getMinifiedJson(c); B.stateChanged(c, j) } } function toggleNodeEvt(a) { var b = getElementId(this); var c = a.data; var d = getNode(c, b); if (!d) { return } toggleNodeBegin(a, c, d) } function toggleNodeBegin(c, d, e) { var f = ''; if (B.toggling) { f = B.toggling(c, d, e); if (f === false) { return false } } if (e.isExpanded) { if (B.closing) { f = B.closing(c, d, e); if (f === false) { return false } } } else { if (B.opening) { f = B.opening(c, d, e); if (f === false) { return false } } } if (e.isLazy && !e.isExpanded) { var g = e.children && e.children.length > 0; f = true; if (B.openLazyNode) { f = B.openLazyNode(c, d, e, g) } if (e.lazyUrl && f !== false) { ajaxService(e.lazyUrl, e.lazyUrlJson, function (a) { if (a.d) { a = a.d } var b = convertInputDataToJson(a); if ($.isArray(b)) { e.children = b } else { e.children = []; e.children.push(b) } buildTree(d); toggleNodeEnd(c, d, e) }); return false } } toggleNodeEnd(c, d, e) } function toggleNodeEnd(a, b, c) { if (c.isExpanded) { openCloseNode(b, c.id, "close"); renderNode(c, "close"); if (B.closed) { B.closed(a, b, c) } } else { openCloseNode(b, c.id, "open"); renderNode(c, "open"); if (B.opened) { B.opened(a, b, c) } } if (B.toggled) { var d = B.toggled(a, b, c) } } function dragStart(a) { if (!B.enableDnd) { return } var b = a.target; while (b) { if (b.className.indexOf("easytree-draggable") > -1) { break } b = b.parentElement } if (!b) { return } unsourceAll(D); unactivateAll(D); $('#' + b.id).addClass('easytree-drag-source'); resetDnd(E); E.createClone = !(b.className.indexOf("easytree-no-clone") > -1); E.dragok = true; E.sourceEl = b; E.sourceId = b.id; E.sourceNode = getNode(D, E.sourceId); return false } function drag(a) { if (!E.dragok) { return } if (!B.enableDnd) { return } if (E.createClone) { if (!E.clone) { E.clone = createClone(E.sourceEl); $(E.clone).appendTo('body') } E.clone.style.left = (a.pageX + 5) + "px"; E.clone.style.top = (a.pageY) + "px" } var b = getDroppableTargetEl(a.clientX, a.clientY); if (!b) { hideDragHelpers(); E.targetEl = null; E.targetId = null; E.targetNode = null; E.canDrop = false; return } if (b.id == E.targetId) { return } E.canDrop = false; window.clearTimeout(E.openDelayTimeout); E.targetEl = b; E.targetId = b.id; E.targetNode = getNode(D, E.targetId); log('source:' + (E.sourceNode && E.sourceNode.text ? E.sourceNode.text : E.sourceId)); log('target:' + (E.targetNode && E.targetNode.text ? E.targetNode.text : E.targetId)); log('isAncester:' + isAncester(E.sourceNode, E.targetId)); var c = $('#' + E.targetId); if (isAncester(E.sourceNode, E.targetId)) { showRejectDragHelper(); return } if (E.targetId == E.sourceId) { hideDragHelpers(); return } if (B.canDrop) { var d = E.sourceNode != null; var e = d ? E.sourceNode : E.sourceEl; var f = E.targetNode != null; var g = f ? E.targetNode : E.targetEl; var h = B.canDrop(a, D, d, e, f, g); if (h === true) { showAcceptDragHelper(); E.canDrop = true; E.openDelayTimeout = window.setTimeout(function () { openCloseNode(D, E.targetId, 'open'); renderNode(E.targetNode, 'open') }, 600); return } else if (h === false) { showRejectDragHelper(); return } } if (c.hasClass('easytree-reject')) { showRejectDragHelper() } else if (c.hasClass('easytree-accept')) { showAcceptDragHelper(); E.canDrop = true; E.openDelayTimeout = window.setTimeout(function () { openCloseNode(D, E.targetId, 'open'); renderNode(E.targetNode, 'open') }, 600) } else { hideDragHelpers() } return false } function dragEnd(a) { var b = E.sourceNode != null; var c = b ? E.sourceNode : E.sourceEl; var d = E.targetNode != null; var e = d ? E.targetNode : E.targetEl; var f = E.canDrop; hideDragHelpers(); $('#_st_clone_').remove(); if (c === null || e === null) { resetDnd(E); return false } if (B.dropping) { var g = B.dropping(a, D, b, c, d, e, f); if (g === false) { resetDnd(E); return } } if (E.targetNode && E.sourceNode && f) { if (!E.targetNode.children) { E.targetNode.children = [] } removeNode(D, E.sourceId); E.targetNode.children.push(E.sourceNode) } if (f) { if (B.dropped) { B.dropped(a, D, b, c, d, e) } buildTree(D) } resetDnd(E); return false } function createClone(a) { $(a).remove(".easytree-expander"); var b = $(a).clone().remove(".easytree-expander").removeClass('easytree-drag-source')[0]; var c = b.children[0]; if (c && c.className == 'easytree-expander') { b.removeChild(c) } b.style.display = 'block'; b.style.position = "absolute"; b.style.opacity = 0.5; b.id = '_st_clone_'; b.style.zIndex = 1000; return b } function getDroppableTargetEl(a, b) { var c = document.elementFromPoint(a, b); while (c) { if (c.className.indexOf('easytree-droppable') > -1) { return c } c = c.parentElement } return null } function resetDnd(a) { a.canDrop = false; a.createClone = true; a.clone = null; a.dragok = false; a.openDelayTimeout = null; a.targetEl = null; a.targetId = null; a.targetNode = null; a.sourceEl = null; a.sourceId = null; a.sourceNode = null } function getElementId(a) { while (a != null) { if (a.id) { return a.id } a = a.parentElement } return null } function getNode(a, b) { var i = 0; for (i = 0; i < a.length; i++) { var n = a[i]; var t = n.text; if (n.id == b) { return n } var c = n.children && n.children.length > 0; if (c) { var d = getNode(n.children, b); if (d) { return d } } } return null } function isAncester(a, b) { var i = 0; if (!a || !a.children || a.children.length == 0) { return false } for (i = 0; i < a.children.length; i++) { var n = a.children[i]; var t = n.text; if (n.id == b) { return true } var c = n.children && n.children.length > 0; if (c) { var d = isAncester(n, b); if (d) { return d } } } return false } function removeNode(a, b) { var i = 0; for (i = 0; i < a.length; i++) { var n = a[i]; var t = n.text; if (n.id == b) { a.splice(i, 1); return } var c = n.children && n.children.length > 0; if (c) { removeNode(n.children, b) } } } function openCloseNode(a, b, c) { var i = 0; for (i = 0; i < a.length; i++) { var n = a[i]; var t = n.text; if (n.id == b) { n.isExpanded = c == "open"; return } var d = n.children && n.children.length > 0; if (d) { openCloseNode(n.children, b, c) } } } function unactivateAll(a) { var i = 0; for (i = 0; i < a.length; i++) { var n = a[i]; n.isActive = false; $('#' + n.id).removeClass('easytree-active'); var b = n.children && n.children.length > 0; if (b) { unactivateAll(n.children) } } } function unsourceAll(a) { var i = 0; for (i = 0; i < a.length; i++) { var n = a[i]; $('#' + n.id).removeClass('easytree-drag-source'); var b = n.children && n.children.length > 0; if (b) { unsourceAll(n.children) } } } function sort(f) { var i = 0; f = f.sort(function (a, b) { var c = a.text.toLowerCase(); var d = b.text.toLowerCase(); if (!c) { c = "a" } if (!d) { d = "a" } if (B.ordering.toLowerCase().indexOf('folder') > -1 && a.isFolder) { c = "______" + c } if (B.ordering.toLowerCase().indexOf('folder') > -1 && b.isFolder) { d = "______" + d } var e = B.ordering.indexOf(" DESC") == -1 ? 1 : -1; if (c < d) { return -1 * e } if (c > d) { return 1 * e } return 0 }); for (i = 0; i < f.length; i++) { var n = f[i]; var g = n.children && n.children.length > 0; if (g) { sort(n.children) } } return f } function giveUniqueIds(a, b, c) { var i = 0; if (!b) { b = 0; c = "_st_node_" + c + "_" } for (i = 0; i < a.length; i++) { var n = a[i]; if (!n.id) { n.id = c + i.toString() } var d = n.children && n.children.length > 0; if (d) { giveUniqueIds(n.children, b + 1, c + i + "_") } } } function buildTree(a) { if (!a) { return } var b = new Date(); if (B.building) { var c = B.building(a); if (c === false) { return false } } var d = new Date(); if (B.ordering) { a = sort(a) } var e = new Date(); var f = Math.floor(Math.random() * 10000); giveUniqueIds(a, 0, f); var g = new Date(); D = a; var h = getNodesAsHtml(a, 0, true); var i = new Date(); C[0].innerHTML = h; var k = new Date(); $(C.selector + " .easytree-node").on("click", a, nodeClick); $(C.selector + " .easytree-expander").on("click", a, toggleNodeEvt); $(C.selector + " .easytree-icon").on("dblclick", a, toggleNodeEvt); $(C.selector + " .easytree-title").on("dblclick", a, toggleNodeEvt); var l = new Date(); if (B.enableDnd) { $(document).on("mousedown", dragStart); $(document).on("mousemove", drag); $(document).on("mouseup", dragEnd) } var m = new Date(); if (B.built) { B.built(a) } var n = new Date(); if (B.stateChanged) { var j = getMinifiedJson(a); B.stateChanged(a, j) } var o = new Date(); var p = d - b; var q = e - d; var r = g - e; var s = i - g; var t = k - i; var u = l - k; var v = m - l; var w = n - m; var x = o - n; var y = o - b } function getNodesAsHtml(a, b, c) { var d = ''; var i = 0; var e = ""; if (b == 0) { e += "ui-easytree easytree-container easytree-focused" } var f = b < B.minOpenLevels; var g = b == 0 || c || f ? "" : " style='display:none' "; d += '<ul tabindex="0" class="' + e + '" ' + g + '">'; for (i = 0; i < a.length; i++) { var n = a[i]; if (f === true) { n.isExpanded = true } var h = i == a.length - 1; var j = getSpanCss(n, h); d += '<li>'; d += '<span id="' + n.id + '" class="' + j + ' ">'; d += f ? '' : '<span class="easytree-expander"></span>'; d += getIconHtml(n); d += getTitleHtml(n); d += '</span>'; if (n.children && n.children.length > 0) { d += getNodesAsHtml(n.children, b + 1, n.isExpanded) } d += '</li>' } d += '</ul>'; return d } function getSpanCss(a, b) { var c = a.children && a.children.length > 0; var d = "easytree-node "; if (B.enableDnd) { d += " easytree-draggable " } if (a.liClass) { d += a.liClass } if (a.isFolder && B.enableDnd) { d += " easytree-droppable easytree-accept " } else if (B.enableDnd) { d += " easytree-droppable easytree-reject " } if (a.isActive && B.allowActivate) { d += " easytree-active " } d += getExpCss(a, b); var e = a.isExpanded ? "e" : "c"; if (a.isFolder) { e += "f" } d += " easytree-ico-" + e; return d } function getExpCss(a, b) { var c = a.children && a.children.length > 0; var d = ""; if (!c && a.isLazy) { d = "c" } else if (!c) { d = "n" } else if (a.isExpanded) { d = "e" } else { d = "c" } if (b) { d += "l" } return " easytree-exp-" + d } function getIconHtml(a) { var b = ''; if (B.disableIcons) { return b } if (a.uiIcon) { return '<span class="easytree-custom-icon ui-icon ' + a.uiIcon + '"></span>' } if (a.iconUrl) { return '<span><img src="' + a.iconUrl + '" /></span>' } return '<span class="easytree-icon"></span>' } function getTitleHtml(a) { var b = ''; var c = a.tooltip ? 'title="' + a.tooltip + '"' : ""; var d = "easytree-title"; if (a.textCss) { d += " " + a.textCss } b += '<span ' + c + ' class="' + d + '">'; if (a.href) { b += '<a href="' + a.href + '" '; if (a.hrefTarget) { b += ' target="' + a.hrefTarget + '" ' } b += '>' } b += a.text; if (a.href) { b += '</a>' } b += '</span>'; return b } function renderNode(a, b) { if (!a) { return } var c = $('#' + a.id).attr('class'); var d = c.indexOf('easytree-exp-'); if (d > -1) { var e = c.indexOf(' ', d); var f = e > -1 ? c.substring(d, e) : c.substring(d); $('#' + a.id).removeClass(f); $('#' + a.id).addClass(getExpCss(a, false)) } var g = $('#' + a.id).parents('li').first(); var h = g.children('ul').first(); var i = parseInt(B.slidingTime, 10); if (b == "close") { h.slideUp(i) } else { h.slideDown(i) } } function hideDragHelpers() { $("#easytree-reject").hide(); $("#easytree-accept").hide() } function showAcceptDragHelper() { $("#easytree-accept").show(); $("#easytree-reject").hide() } function showRejectDragHelper() { $("#easytree-reject").show(); $("#easytree-accept").hide() } function getMinifiedJson(a) { var j = JSON.stringify ? JSON.stringify(a) : 'Please import json2.js'; while (j.indexOf(',"children":[]') > -1) { j = j.replace(',"children":[]', '') } while (j.indexOf('"liClass":"",') > -1) { j = j.replace('"liClass":"",', '') } while (j.indexOf('"textCss":"",') > -1) { j = j.replace('"textCss":"",', '') } while (j.indexOf('"isExpanded":false,') > -1) { j = j.replace('"isExpanded":false,', '') } while (j.indexOf('"isActive":false,') > -1) { j = j.replace('"isActive":false,', '') } while (j.indexOf('"isFolder":false,') > -1) { j = j.replace('"isFolder":false,', '') } while (j.indexOf('"isLazy":false,') > -1) { j = j.replace('"isLazy":false,', '') } return j } function init() { initDragHelpers(); resetDnd(E); $(document).on("mousemove", function (a) { var b = a.pageY; var c = a.pageX; document.getElementById('easytree-reject').style.top = (b + 10) + 'px'; document.getElementById('easytree-reject').style.left = (c + 17) + 'px'; document.getElementById('easytree-accept').style.top = (b + 10) + 'px'; document.getElementById('easytree-accept').style.left = (c + 17) + 'px' }) } function initDragHelpers() { if (!$("#easytree-reject").length) { var a = '<div id="easytree-reject" class="easytree-drag-helper easytree-drop-reject">'; a += '<span class="easytree-drag-helper-img"></span>'; a += '</div>'; $('body').append(a) } if (!$("#easytree-accept").length) { var b = '<div id="easytree-accept" class="easytree-drag-helper easytree-drop-accept">'; b += '<span class="easytree-drag-helper-img"></span>'; b += '</div>'; $('body').append(b) } } function ajaxService(d, e, f) { $.ajax({ url: d, type: "POST", contentType: "application/json; charset=utf-8", data: e, success: f, error: function (a, b, c) { alert("Error: " + a.responseText) } }) } function convertInputDataToJson(a) { var b = null; if (typeof a == 'object') { b = a } else if (typeof a == 'string') { a = $.trim(a); if (a.indexOf('[') == 0 || a.indexOf('{') == 0) { b = $.parseJSON(a) } else { b = convertHtmlToJson(a) } } return b } function convertHtmlToJson(b) { var i = 0; var c = $(b); var d = []; var e = c.children().each(function (a) { d.push(convertHtmlToNode(this)) }); return d } function convertHtmlToNode(b) { var c = $(b); var d = {}; var e = c.data(); d.isActive = c.hasClass('isActive'); c.removeClass('isActive'); d.isFolder = c.hasClass('isFolder'); c.removeClass('isFolder'); d.isExpanded = c.hasClass('isExpanded'); c.removeClass('isExpanded'); d.isLazy = c.hasClass('isLazy'); c.removeClass('isLazy'); d.uiIcon = e.uiicon; d.liClass = c.attr('class'); d.id = c.attr('id'); var f = c.children('a'); if (f) { d.href = f.attr('href'); d.hrefTarget = f.attr('target') } var g = c.children('img'); if (g) { d.iconUrl = g.attr('src') } d.textCss = ''; var h = c.children('span'); if (h && h.attr('class')) { d.textCss += h.attr('class') + ' ' } h = f.children('span'); if (h && h.attr('class')) { d.textCss += h.attr('class') + ' ' } h = g.children('span'); if (h && h.attr('class')) { d.textCss += h.attr('class') + ' ' } d.text = getNodeValue(c[0]); d.tooltip = c.attr('title'); d.children = []; var i = c.children('ul').children('li').each(function (a) { d.children.push(convertHtmlToNode(this)) }); return d } function getNodeValue(a) { var i = 0; for (i = 0; i < a.childNodes.length; i++) { var b = a.childNodes[i]; while (b) { if (b.nodeType == 3 && $.trim(b.nodeValue).length > 0) { return $.trim(b.nodeValue) } b = b.firstChild } } return '' } this.init(z, A); function log(a) { if (!a) { a = 'null' } console.log(a) } } }(jQuery));