/* * jQuery OrgChart Plugin * https://github.com/dabeng/OrgChart * * Copyright 2016, dabeng * https://github.com/dabeng * * Licensed under the MIT license: * http://www.opensource.org/licenses/MIT */ 'use strict'; (function (factory) { if (typeof module === 'object' && typeof module.exports === 'object') { factory(require('jquery'), window, document); } else { factory(jQuery, window, document); } }(function ($, window, document, undefined) { var OrgChart = function (elem, opts) { this.$chartContainer = $(elem); this.opts = opts; this.defaultOptions = { 'nodeTitle': 'name', 'nodeId': 'id', 'toggleSiblingsResp': false, 'visibleLevel': 999, 'chartClass': '', 'exportButton': false, 'exportFilename': 'OrgChart', 'exportFileextension': 'png', 'parentNodeSymbol': 'fa-caret-down',/* fa-users */ 'draggable': false, 'direction': 't2b', 'pan': false, 'zoom': false, 'zoominLimit': 7, 'zoomoutLimit': 0.5 }; }; // OrgChart.prototype = { // init: function (opts) { var that = this; this.options = $.extend({}, this.defaultOptions, this.opts, opts); // build the org-chart var $chartContainer = this.$chartContainer; if (this.$chart) { this.$chart.remove(); } var data = this.options.data; var $chart = this.$chart = $('
', { 'data': { 'options': this.options }, 'class': 'orgchart' + (this.options.chartClass !== '' ? ' ' + this.options.chartClass : '') + (this.options.direction !== 't2b' ? ' ' + this.options.direction : ''), 'click': function(event) { if (!$(event.target).closest('.node').length) { $chart.find('.node.focused').removeClass('focused'); } } }); if (typeof MutationObserver !== 'undefined') { this.triggerInitEvent(); } if ($.type(data) === 'object') { if (data instanceof $) { // ul datasource this.buildHierarchy($chart, this.buildJsonDS(data.children()), 0, this.options); } else { // local json datasource this.buildHierarchy($chart, this.options.ajaxURL ? data : this.attachRel(data, '00')); } } else { $chart.append(''); $.ajax({ 'url': data, 'dataType': 'json' }) .done(function(data, textStatus, jqXHR) { that.buildHierarchy($chart, that.options.ajaxURL ? data : that.attachRel(data, '00'), 0, that.options); }) .fail(function(jqXHR, textStatus, errorThrown) { console.log(errorThrown); }) .always(function() { $chart.children('.spinner').remove(); }); } $chartContainer.append($chart); // append the export button if (this.options.exportButton && !$chartContainer.find('.oc-export-btn').length) { this.attachExportButton(); } if (this.options.pan) { this.bindPan(); } if (this.options.zoom) { this.bindZoom(); } return this; }, // triggerInitEvent: function () { var that = this; var mo = new MutationObserver(function (mutations) { mo.disconnect(); initTime: for (var i = 0; i < mutations.length; i++) { for (var j = 0; j < mutations[i].addedNodes.length; j++) { if (mutations[i].addedNodes[j].classList.contains('orgchart')) { if (that.options.initCompleted && typeof that.options.initCompleted === 'function') { that.options.initCompleted(that.$chart); var initEvent = $.Event('init.orgchart'); that.$chart.trigger(initEvent); break initTime; } } } } }); mo.observe(this.$chartContainer[0], { childList: true }); }, // attachExportButton: function () { var that = this; var $exportBtn = $('