/* ============================================================= * bootstrap-tree.js v0.3 * http://twitter.github.com/cutterbl/Bootstrap-Tree * * Inspired by Twitter Bootstrap, with credit to bits of code * from all over. * ============================================================= * Copyright 2012 Cutters Crossing. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================ */ !function ($) { "use strict"; // jshint ;_; var loading = " Loading ..."; /* TREE CLASS DEFINITION * ========================= */ var Tree = function (element, options) { this.$element = $(element) this.$tree = this.$element.closest(".tree") this.parentage = GetParentage(this.$element) this.options = $.extend({}, $.fn.tree.defaults, options) if (this.options.parent) { this.$parent = $(this.options.parent) } this.options.toggle && this.toggle() } Tree.prototype = { constructor: Tree , toggle: function () { var a, n, s , currentStatus = this.$element.hasClass("in") , eventName = (!currentStatus) ? "openbranch" : "closebranch" this.$parent[currentStatus ? "addClass" : "removeClass"]("closed") this.$element[currentStatus ? "removeClass" : "addClass"]("in") if (this.options.href) { this._load() } n = this.node() // 'Action' (open|close) event a = $.Event(eventName, { node: n }) // 'Select' event s = $.Event("nodeselect", { node: n }) this.$parent.trigger(a).trigger(s) } , _load: function () { var data = $.extend({}, this.options) , el = this.$element , $this = $(this) , options = this.options // some config data we don't need to pass in the post delete data.parent delete data.href delete data.callback $.post(options.href, data, function (d, s, x){ var doc, type = "html" if (options.callback) { // If a callback was defined in the data parameters var cb = window[options.callback].apply(el, [d, s, x]) // callbacks must return an object with 'doc' and 'type' keys doc = cb.doc || d type = cb.type || type } else { try { doc = $.parseJSON(d) type = "json" } catch (err) { doc = d } if (type !== "json") { try { doc = $.parseXML(d) type = "xml" } catch (err) { doc = d } } } switch (type) { case "html": el.html(doc) break default: $this[0]._buildOutput(doc, type, el) break } }, "html") } , _buildOutput: function (doc, type, parent) { var nodes = this._buildNodes(doc, type) parent.empty().append(this._createNodes(nodes)) } , _createNodes: function (nodes) { var els = [] , $this = $(this) $.each(nodes, function (ind, el) { var node = $("
  • ") , role = (el.leaf) ? "leaf" : "branch" , attributes = {} , anchor = $("") attributes.role = role if (!el.leaf) { var branch = $("