apostrophe
Version:
The Apostrophe Content Management System.
1,496 lines (1,463 loc) • 119 kB
JavaScript
/*!
* JqTree 1.4.2
*
* Copyright 2017 Marco Braak
*
* 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(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 14);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var Position;
(function (Position) {
Position[Position["Before"] = 1] = "Before";
Position[Position["After"] = 2] = "After";
Position[Position["Inside"] = 3] = "Inside";
Position[Position["None"] = 4] = "None";
})(Position = exports.Position || (exports.Position = {}));
exports.position_names = {
before: Position.Before,
after: Position.After,
inside: Position.Inside,
none: Position.None
};
function getPositionName(position) {
for (var name_1 in exports.position_names) {
if (exports.position_names.hasOwnProperty(name_1)) {
if (exports.position_names[name_1] === position) {
return name_1;
}
}
}
return "";
}
exports.getPositionName = getPositionName;
function getPosition(name) {
return exports.position_names[name];
}
exports.getPosition = getPosition;
var Node = (function () {
function Node(o, is_root, node_class) {
if (is_root === void 0) { is_root = false; }
if (node_class === void 0) { node_class = Node; }
this.name = "";
this.setData(o);
this.children = [];
this.parent = null;
if (is_root) {
this.id_mapping = {};
this.tree = this;
this.node_class = node_class;
}
}
/*
Set the data of this node.
setData(string): set the name of the node
setdata(object): set attributes of the node
Examples:
setdata('node1')
setData({ name: 'node1', id: 1});
setData({ name: 'node2', id: 2, color: 'green'});
* This is an internal function; it is not in the docs
* Does not remove existing node values
*/
Node.prototype.setData = function (o) {
var _this = this;
var setName = function (name) {
if (name != null) {
_this.name = name;
}
};
if (!o) {
return;
}
else if (typeof o !== "object") {
setName(o);
}
else {
for (var key in o) {
if (o.hasOwnProperty(key)) {
var value = o[key];
if (key === "label") {
// You can use the 'label' key instead of 'name'; this is a legacy feature
setName(value);
}
else if (key !== "children") {
// You can't update the children using this function
this[key] = value;
}
}
}
}
};
/*
Create tree from data.
Structure of data is:
[
{
label: 'node1',
children: [
{ label: 'child1' },
{ label: 'child2' }
]
},
{
label: 'node2'
}
]
*/
Node.prototype.loadFromData = function (data) {
this.removeChildren();
for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
var o = data_1[_i];
var node = new this.tree.node_class(o);
this.addChild(node);
if (typeof o === "object" && o["children"]) {
node.loadFromData(o["children"]);
}
}
};
/*
Add child.
tree.addChild(
new Node('child1')
);
*/
Node.prototype.addChild = function (node) {
this.children.push(node);
node._setParent(this);
};
/*
Add child at position. Index starts at 0.
tree.addChildAtPosition(
new Node('abc'),
1
);
*/
Node.prototype.addChildAtPosition = function (node, index) {
this.children.splice(index, 0, node);
node._setParent(this);
};
/*
Remove child. This also removes the children of the node.
tree.removeChild(tree.children[0]);
*/
Node.prototype.removeChild = function (node) {
// remove children from the index
node.removeChildren();
this._removeChild(node);
};
/*
Get child index.
var index = getChildIndex(node);
*/
Node.prototype.getChildIndex = function (node) {
return $.inArray(node, this.children);
};
/*
Does the tree have children?
if (tree.hasChildren()) {
//
}
*/
Node.prototype.hasChildren = function () {
return this.children.length !== 0;
};
Node.prototype.isFolder = function () {
return this.hasChildren() || this.load_on_demand;
};
/*
Iterate over all the nodes in the tree.
Calls callback with (node, level).
The callback must return true to continue the iteration on current node.
tree.iterate(
function(node, level) {
console.log(node.name);
// stop iteration after level 2
return (level <= 2);
}
);
*/
Node.prototype.iterate = function (callback) {
var _iterate = function (node, level) {
if (node.children) {
for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
var child = _a[_i];
var result = callback(child, level);
if (result && child.hasChildren()) {
_iterate(child, level + 1);
}
}
}
};
_iterate(this, 0);
};
/*
Move node relative to another node.
Argument position: Position.BEFORE, Position.AFTER or Position.Inside
// move node1 after node2
tree.moveNode(node1, node2, Position.AFTER);
*/
Node.prototype.moveNode = function (moved_node, target_node, position) {
if (!moved_node.parent || moved_node.isParentOf(target_node)) {
// - Node is parent of target node
// - Or, parent is empty
return;
}
else {
moved_node.parent._removeChild(moved_node);
if (position === Position.After) {
if (target_node.parent) {
target_node.parent.addChildAtPosition(moved_node, target_node.parent.getChildIndex(target_node) + 1);
}
}
else if (position === Position.Before) {
if (target_node.parent) {
target_node.parent.addChildAtPosition(moved_node, target_node.parent.getChildIndex(target_node));
}
}
else if (position === Position.Inside) {
// move inside as first child
target_node.addChildAtPosition(moved_node, 0);
}
}
};
/*
Get the tree as data.
*/
Node.prototype.getData = function (include_parent) {
if (include_parent === void 0) { include_parent = false; }
function getDataFromNodes(nodes) {
return nodes.map(function (node) {
var tmp_node = {};
for (var k in node) {
if (["parent", "children", "element", "tree"].indexOf(k) ===
-1 &&
Object.prototype.hasOwnProperty.call(node, k)) {
var v = node[k];
tmp_node[k] = v;
}
}
if (node.hasChildren()) {
tmp_node["children"] = getDataFromNodes(node.children);
}
return tmp_node;
});
}
if (include_parent) {
return getDataFromNodes([this]);
}
else {
return getDataFromNodes(this.children);
}
};
Node.prototype.getNodeByName = function (name) {
return this.getNodeByCallback(function (node) { return node.name === name; });
};
Node.prototype.getNodeByCallback = function (callback) {
var result = null;
this.iterate(function (node) {
if (callback(node)) {
result = node;
return false;
}
else {
return true;
}
});
return result;
};
Node.prototype.addAfter = function (node_info) {
if (!this.parent) {
return null;
}
else {
var node = new this.tree.node_class(node_info);
var child_index = this.parent.getChildIndex(this);
this.parent.addChildAtPosition(node, child_index + 1);
if (typeof node_info === "object" &&
node_info["children"] &&
node_info["children"].length) {
node.loadFromData(node_info["children"]);
}
return node;
}
};
Node.prototype.addBefore = function (node_info) {
if (!this.parent) {
return null;
}
else {
var node = new this.tree.node_class(node_info);
var child_index = this.parent.getChildIndex(this);
this.parent.addChildAtPosition(node, child_index);
if (typeof node_info === "object" &&
node_info["children"] &&
node_info["children"].length) {
node.loadFromData(node_info["children"]);
}
return node;
}
};
Node.prototype.addParent = function (node_info) {
if (!this.parent) {
return null;
}
else {
var new_parent = new this.tree.node_class(node_info);
new_parent._setParent(this.tree);
var original_parent = this.parent;
for (var _i = 0, _a = original_parent.children; _i < _a.length; _i++) {
var child = _a[_i];
new_parent.addChild(child);
}
original_parent.children = [];
original_parent.addChild(new_parent);
return new_parent;
}
};
Node.prototype.remove = function () {
if (this.parent) {
this.parent.removeChild(this);
this.parent = null;
}
};
Node.prototype.append = function (node_info) {
var node = new this.tree.node_class(node_info);
this.addChild(node);
if (typeof node_info === "object" &&
node_info["children"] &&
node_info["children"].length) {
node.loadFromData(node_info["children"]);
}
return node;
};
Node.prototype.prepend = function (node_info) {
var node = new this.tree.node_class(node_info);
this.addChildAtPosition(node, 0);
if (typeof node_info === "object" &&
node_info["children"] &&
node_info["children"].length) {
node.loadFromData(node_info["children"]);
}
return node;
};
Node.prototype.isParentOf = function (node) {
var parent = node.parent;
while (parent) {
if (parent === this) {
return true;
}
parent = parent.parent;
}
return false;
};
Node.prototype.getLevel = function () {
var level = 0;
var node = this;
while (node.parent) {
level += 1;
node = node.parent;
}
return level;
};
Node.prototype.getNodeById = function (node_id) {
return this.id_mapping[node_id];
};
Node.prototype.addNodeToIndex = function (node) {
if (node.id != null) {
this.id_mapping[node.id] = node;
}
};
Node.prototype.removeNodeFromIndex = function (node) {
if (node.id != null) {
delete this.id_mapping[node.id];
}
};
Node.prototype.removeChildren = function () {
var _this = this;
this.iterate(function (child) {
_this.tree.removeNodeFromIndex(child);
return true;
});
this.children = [];
};
Node.prototype.getPreviousSibling = function () {
if (!this.parent) {
return null;
}
else {
var previous_index = this.parent.getChildIndex(this) - 1;
if (previous_index >= 0) {
return this.parent.children[previous_index];
}
else {
return null;
}
}
};
Node.prototype.getNextSibling = function () {
if (!this.parent) {
return null;
}
else {
var next_index = this.parent.getChildIndex(this) + 1;
if (next_index < this.parent.children.length) {
return this.parent.children[next_index];
}
else {
return null;
}
}
};
Node.prototype.getNodesByProperty = function (key, value) {
return this.filter(function (node) { return node[key] === value; });
};
Node.prototype.filter = function (f) {
var result = [];
this.iterate(function (node) {
if (f(node)) {
result.push(node);
}
return true;
});
return result;
};
Node.prototype.getNextNode = function (include_children) {
if (include_children === void 0) { include_children = true; }
if (include_children && this.hasChildren() && this.is_open) {
// First child
return this.children[0];
}
else {
if (!this.parent) {
return null;
}
else {
var next_sibling = this.getNextSibling();
if (next_sibling) {
// Next sibling
return next_sibling;
}
else {
// Next node of parent
return this.parent.getNextNode(false);
}
}
}
};
Node.prototype.getPreviousNode = function () {
if (!this.parent) {
return null;
}
else {
var previous_sibling = this.getPreviousSibling();
if (previous_sibling) {
if (!previous_sibling.hasChildren() ||
!previous_sibling.is_open) {
// Previous sibling
return previous_sibling;
}
else {
// Last child of previous sibling
return previous_sibling.getLastChild();
}
}
else {
return this.getParent();
}
}
};
Node.prototype.getParent = function () {
// Return parent except if it is the root node
if (!this.parent) {
return null;
}
else if (!this.parent.parent) {
// Root node -> null
return null;
}
else {
return this.parent;
}
};
Node.prototype.getLastChild = function () {
if (!this.hasChildren()) {
return null;
}
else {
var last_child = this.children[this.children.length - 1];
if (!last_child.hasChildren() || !last_child.is_open) {
return last_child;
}
else {
return last_child.getLastChild();
}
}
};
// Init Node from data without making it the root of the tree
Node.prototype.initFromData = function (data) {
var _this = this;
var addNode = function (node_data) {
_this.setData(node_data);
if (node_data["children"]) {
addChildren(node_data["children"]);
}
};
var addChildren = function (children_data) {
for (var _i = 0, children_data_1 = children_data; _i < children_data_1.length; _i++) {
var child = children_data_1[_i];
var node = new _this.tree.node_class("");
node.initFromData(child);
_this.addChild(node);
}
};
addNode(data);
};
Node.prototype._setParent = function (parent) {
this.parent = parent;
this.tree = parent.tree;
this.tree.addNodeToIndex(this);
};
Node.prototype._removeChild = function (node) {
this.children.splice(this.getChildIndex(node), 1);
this.tree.removeNodeFromIndex(node);
};
return Node;
}());
exports.Node = Node;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
function isInt(n) {
return typeof n === "number" && n % 1 === 0;
}
exports.isInt = isInt;
function isFunction(v) {
return typeof v === "function";
}
exports.isFunction = isFunction;
// Escape a string for HTML interpolation; copied from underscore js
function html_escape(text) {
return ("" + text)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/\//g, "/");
}
exports.html_escape = html_escape;
function getBoolString(value) {
if (value) {
return "true";
}
else {
return "false";
}
}
exports.getBoolString = getBoolString;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var SimpleWidget = (function () {
function SimpleWidget(el, options) {
this.$el = $(el);
var defaults = this.constructor.defaults;
this.options = $.extend({}, defaults, options);
}
SimpleWidget.register = function (widget_class, widget_name) {
var getDataKey = function () { return "simple_widget_" + widget_name; };
function getWidgetData(el, data_key) {
var widget = $.data(el, data_key);
if (widget && widget instanceof SimpleWidget) {
return widget;
}
else {
return null;
}
}
function createWidget($el, options) {
var data_key = getDataKey();
for (var _i = 0, _a = $el.get(); _i < _a.length; _i++) {
var el = _a[_i];
var existing_widget = getWidgetData(el, data_key);
if (!existing_widget) {
var widget = new widget_class(el, options);
if (!$.data(el, data_key)) {
$.data(el, data_key, widget);
}
// Call init after setting data, so we can call methods
widget._init();
}
}
return $el;
}
function destroyWidget($el) {
var data_key = getDataKey();
for (var _i = 0, _a = $el.get(); _i < _a.length; _i++) {
var el = _a[_i];
var widget = getWidgetData(el, data_key);
if (widget) {
widget.destroy();
}
$.removeData(el, data_key);
}
}
function callFunction($el, function_name, args) {
var result = null;
for (var _i = 0, _a = $el.get(); _i < _a.length; _i++) {
var el = _a[_i];
var widget = $.data(el, getDataKey());
if (widget && widget instanceof SimpleWidget) {
var widget_function = widget[function_name];
if (widget_function &&
typeof widget_function === "function") {
result = widget_function.apply(widget, args);
}
}
}
return result;
}
// tslint:disable-next-line: only-arrow-functions
$.fn[widget_name] = function (argument1) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var $el = this;
if (argument1 === undefined || typeof argument1 === "object") {
var options = argument1;
return createWidget($el, options);
}
else if (typeof argument1 === "string" && argument1[0] !== "_") {
var function_name = argument1;
if (function_name === "destroy") {
return destroyWidget($el);
}
else if (function_name === "get_widget_class") {
return widget_class;
}
else {
return callFunction($el, function_name, args);
}
}
};
};
SimpleWidget.prototype.destroy = function () {
this._deinit();
};
SimpleWidget.prototype._init = function () {
//
};
SimpleWidget.prototype._deinit = function () {
//
};
SimpleWidget.defaults = {};
return SimpleWidget;
}());
exports["default"] = SimpleWidget;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
var version_1 = __webpack_require__(4);
var drag_and_drop_handler_1 = __webpack_require__(5);
var elements_renderer_1 = __webpack_require__(6);
var key_handler_1 = __webpack_require__(7);
var mouse_widget_1 = __webpack_require__(8);
var save_state_handler_1 = __webpack_require__(9);
var scroll_handler_1 = __webpack_require__(10);
var select_node_handler_1 = __webpack_require__(11);
var simple_widget_1 = __webpack_require__(2);
var node_1 = __webpack_require__(0);
var util_1 = __webpack_require__(1);
var node_element_1 = __webpack_require__(12);
var JqTreeWidget = (function (_super) {
__extends(JqTreeWidget, _super);
function JqTreeWidget() {
return _super !== null && _super.apply(this, arguments) || this;
}
JqTreeWidget.prototype.toggle = function (node, slide_param) {
var slide = slide_param == null ? this.options.slide : slide_param;
if (node.is_open) {
this.closeNode(node, slide);
}
else {
this.openNode(node, slide);
}
return this.element;
};
JqTreeWidget.prototype.getTree = function () {
return this.tree;
};
JqTreeWidget.prototype.selectNode = function (node) {
this._selectNode(node, false);
return this.element;
};
JqTreeWidget.prototype.getSelectedNode = function () {
if (this.select_node_handler) {
return this.select_node_handler.getSelectedNode();
}
else {
return false;
}
};
JqTreeWidget.prototype.toJson = function () {
return JSON.stringify(this.tree.getData());
};
JqTreeWidget.prototype.loadData = function (data, parent_node) {
this._loadData(data, parent_node);
return this.element;
};
/*
signatures:
- loadDataFromUrl(url, parent_node=null, on_finished=null)
loadDataFromUrl('/my_data');
loadDataFromUrl('/my_data', node1);
loadDataFromUrl('/my_data', node1, function() { console.log('finished'); });
loadDataFromUrl('/my_data', null, function() { console.log('finished'); });
- loadDataFromUrl(parent_node=null, on_finished=null)
loadDataFromUrl();
loadDataFromUrl(node1);
loadDataFromUrl(null, function() { console.log('finished'); });
loadDataFromUrl(node1, function() { console.log('finished'); });
*/
JqTreeWidget.prototype.loadDataFromUrl = function (param1, param2, param3) {
if ($.type(param1) === "string") {
// first parameter is url
this._loadDataFromUrl(param1, param2, param3);
}
else {
// first parameter is not url
this._loadDataFromUrl(null, param1, param2);
}
return this.element;
};
JqTreeWidget.prototype.reload = function (on_finished) {
this._loadDataFromUrl(null, null, on_finished);
return this.element;
};
JqTreeWidget.prototype.getNodeById = function (node_id) {
return this.tree.getNodeById(node_id);
};
JqTreeWidget.prototype.getNodeByName = function (name) {
return this.tree.getNodeByName(name);
};
JqTreeWidget.prototype.getNodesByProperty = function (key, value) {
return this.tree.getNodesByProperty(key, value);
};
JqTreeWidget.prototype.getNodeByHtmlElement = function (element) {
return this._getNode($(element));
};
JqTreeWidget.prototype.getNodeByCallback = function (callback) {
return this.tree.getNodeByCallback(callback);
};
JqTreeWidget.prototype.openNode = function (node, param1, param2) {
var _this = this;
var parseParams = function () {
var on_finished;
var slide;
if (util_1.isFunction(param1)) {
on_finished = param1;
slide = null;
}
else {
slide = param1;
on_finished = param2;
}
if (slide == null) {
slide = _this.options.slide;
}
return [slide, on_finished];
};
var _a = parseParams(), slide = _a[0], on_finished = _a[1];
if (node) {
this._openNode(node, slide, on_finished);
}
return this.element;
};
JqTreeWidget.prototype.closeNode = function (node, slide_param) {
var slide = slide_param == null ? this.options.slide : slide_param;
if (node.isFolder()) {
new node_element_1.FolderElement(node, this).close(slide);
this._saveState();
}
return this.element;
};
JqTreeWidget.prototype.isDragging = function () {
if (this.dnd_handler) {
return this.dnd_handler.is_dragging;
}
else {
return false;
}
};
JqTreeWidget.prototype.refreshHitAreas = function () {
if (this.dnd_handler) {
this.dnd_handler.refresh();
}
return this.element;
};
JqTreeWidget.prototype.addNodeAfter = function (new_node_info, existing_node) {
var new_node = existing_node.addAfter(new_node_info);
if (new_node) {
this._refreshElements(existing_node.parent);
}
return new_node;
};
JqTreeWidget.prototype.addNodeBefore = function (new_node_info, existing_node) {
var new_node = existing_node.addBefore(new_node_info);
if (new_node) {
this._refreshElements(existing_node.parent);
}
return new_node;
};
JqTreeWidget.prototype.addParentNode = function (new_node_info, existing_node) {
var new_node = existing_node.addParent(new_node_info);
if (new_node) {
this._refreshElements(new_node.parent);
}
return new_node;
};
JqTreeWidget.prototype.removeNode = function (node) {
if (node.parent && this.select_node_handler) {
this.select_node_handler.removeFromSelection(node, true); // including children
node.remove();
this._refreshElements(node.parent);
}
return this.element;
};
JqTreeWidget.prototype.appendNode = function (new_node_info, parent_node_param) {
var parent_node = parent_node_param || this.tree;
var node = parent_node.append(new_node_info);
this._refreshElements(parent_node);
return node;
};
JqTreeWidget.prototype.prependNode = function (new_node_info, parent_node_param) {
var parent_node = !parent_node_param ? this.tree : parent_node_param;
var node = parent_node.prepend(new_node_info);
this._refreshElements(parent_node);
return node;
};
JqTreeWidget.prototype.updateNode = function (node, data) {
var id_is_changed = data.id && data.id !== node.id;
if (id_is_changed) {
this.tree.removeNodeFromIndex(node);
}
node.setData(data);
if (id_is_changed) {
this.tree.addNodeToIndex(node);
}
if (typeof data === "object" && data.children) {
node.removeChildren();
if (data.children.length) {
node.loadFromData(data.children);
}
}
this.renderer.renderFromNode(node);
this._selectCurrentNode();
return this.element;
};
JqTreeWidget.prototype.moveNode = function (node, target_node, position) {
var position_index = node_1.getPosition(position);
this.tree.moveNode(node, target_node, position_index);
this._refreshElements(null);
return this.element;
};
JqTreeWidget.prototype.getStateFromStorage = function () {
if (this.save_state_handler) {
return this.save_state_handler.getStateFromStorage();
}
};
JqTreeWidget.prototype.addToSelection = function (node) {
if (node && this.select_node_handler) {
this.select_node_handler.addToSelection(node);
this._getNodeElementForNode(node).select();
this._saveState();
}
return this.element;
};
JqTreeWidget.prototype.getSelectedNodes = function () {
if (!this.select_node_handler) {
return [];
}
else {
return this.select_node_handler.getSelectedNodes();
}
};
JqTreeWidget.prototype.isNodeSelected = function (node) {
if (!this.select_node_handler) {
return false;
}
else {
return this.select_node_handler.isNodeSelected(node);
}
};
JqTreeWidget.prototype.removeFromSelection = function (node) {
if (this.select_node_handler) {
this.select_node_handler.removeFromSelection(node);
this._getNodeElementForNode(node).deselect();
this._saveState();
}
return this.element;
};
JqTreeWidget.prototype.scrollToNode = function (node) {
if (this.scroll_handler) {
var $element = $(node.element);
var top_1 = $element.offset().top - this.$el.offset().top;
this.scroll_handler.scrollTo(top_1);
}
return this.element;
};
JqTreeWidget.prototype.getState = function () {
if (this.save_state_handler) {
return this.save_state_handler.getState();
}
};
JqTreeWidget.prototype.setState = function (state) {
if (this.save_state_handler) {
this.save_state_handler.setInitialState(state);
this._refreshElements(null);
}
return this.element;
};
JqTreeWidget.prototype.setOption = function (option, value) {
this.options[option] = value;
return this.element;
};
JqTreeWidget.prototype.moveDown = function () {
if (this.key_handler) {
this.key_handler.moveDown();
}
return this.element;
};
JqTreeWidget.prototype.moveUp = function () {
if (this.key_handler) {
this.key_handler.moveUp();
}
return this.element;
};
JqTreeWidget.prototype.getVersion = function () {
return version_1["default"];
};
JqTreeWidget.prototype.testGenerateHitAreas = function (moving_node) {
if (!this.dnd_handler) {
return [];
}
else {
this.dnd_handler.current_item = this._getNodeElementForNode(moving_node);
this.dnd_handler.generateHitAreas();
return this.dnd_handler.hit_areas;
}
};
JqTreeWidget.prototype._triggerEvent = function (event_name, values) {
var event = $.Event(event_name);
$.extend(event, values);
this.element.trigger(event);
return event;
};
JqTreeWidget.prototype._openNode = function (node, slide, on_finished) {
var _this = this;
if (slide === void 0) { slide = true; }
var doOpenNode = function (_node, _slide, _on_finished) {
var folder_element = new node_element_1.FolderElement(_node, _this);
folder_element.open(_on_finished, _slide);
};
if (node.isFolder()) {
if (node.load_on_demand) {
this._loadFolderOnDemand(node, slide, on_finished);
}
else {
var parent_1 = node.parent;
while (parent_1) {
// nb: do not open root element
if (parent_1.parent) {
doOpenNode(parent_1, false, null);
}
parent_1 = parent_1.parent;
}
doOpenNode(node, slide, on_finished);
this._saveState();
}
}
};
/*
Redraw the tree or part of the tree.
from_node: redraw this subtree
*/
JqTreeWidget.prototype._refreshElements = function (from_node) {
this.renderer.render(from_node);
this._triggerEvent("tree.refresh");
};
JqTreeWidget.prototype._getNodeElementForNode = function (node) {
if (node.isFolder()) {
return new node_element_1.FolderElement(node, this);
}
else {
return new node_element_1.NodeElement(node, this);
}
};
JqTreeWidget.prototype._getNodeElement = function ($element) {
var node = this._getNode($element);
if (node) {
return this._getNodeElementForNode(node);
}
else {
return null;
}
};
JqTreeWidget.prototype._containsElement = function (element) {
var node = this._getNode($(element));
return node != null && node.tree === this.tree;
};
JqTreeWidget.prototype._init = function () {
_super.prototype._init.call(this);
this.element = this.$el;
this.mouse_delay = 300;
this.is_initialized = false;
this.options.rtl = this._getRtlOption();
if (this.options.closedIcon === null) {
this.options.closedIcon = this._getDefaultClosedIcon();
}
this.renderer = new elements_renderer_1["default"](this);
if (save_state_handler_1["default"] != null) {
this.save_state_handler = new save_state_handler_1["default"](this);
}
else {
this.options.saveState = false;
}
if (select_node_handler_1["default"] != null) {
this.select_node_handler = new select_node_handler_1["default"](this);
}
if (drag_and_drop_handler_1.DragAndDropHandler != null) {
this.dnd_handler = new drag_and_drop_handler_1.DragAndDropHandler(this);
}
else {
this.options.dragAndDrop = false;
}
if (scroll_handler_1["default"] != null) {
this.scroll_handler = new scroll_handler_1["default"](this);
}
if (key_handler_1["default"] != null && select_node_handler_1["default"] != null) {
this.key_handler = new key_handler_1["default"](this);
}
this._initData();
this.element.click($.proxy(this._click, this));
this.element.dblclick($.proxy(this._dblclick, this));
if (this.options.useContextMenu) {
this.element.on("contextmenu", $.proxy(this._contextmenu, this));
}
};
JqTreeWidget.prototype._deinit = function () {
this.element.empty();
this.element.off();
if (this.key_handler) {
this.key_handler.deinit();
}
this.tree = new node_1.Node({}, true);
_super.prototype._deinit.call(this);
};
JqTreeWidget.prototype._mouseCapture = function (position_info) {
if (this.options.dragAndDrop && this.dnd_handler) {
return this.dnd_handler.mouseCapture(position_info);
}
else {
return false;
}
};
JqTreeWidget.prototype._mouseStart = function (position_info) {
if (this.options.dragAndDrop && this.dnd_handler) {
return this.dnd_handler.mouseStart(position_info);
}
else {
return false;
}
};
JqTreeWidget.prototype._mouseDrag = function (position_info) {
if (this.options.dragAndDrop && this.dnd_handler) {
var result = this.dnd_handler.mouseDrag(position_info);
if (this.scroll_handler) {
this.scroll_handler.checkScrolling();
}
return result;
}
else {
return false;
}
};
JqTreeWidget.prototype._mouseStop = function (position_info) {
if (this.options.dragAndDrop && this.dnd_handler) {
return this.dnd_handler.mouseStop(position_info);
}
else {
return false;
}
};
JqTreeWidget.prototype._initData = function () {
if (this.options.data) {
this._loadData(this.options.data, null);
}
else {
var data_url = this._getDataUrlInfo(null);
if (data_url) {
this._loadDataFromUrl(null, null, null);
}
else {
this._loadData([], null);
}
}
};
JqTreeWidget.prototype._getDataUrlInfo = function (node) {
var _this = this;
var data_url = this.options.dataUrl || this.element.data("url");
var getUrlFromString = function () {
var url_info = { url: data_url };
if (node && node.id) {
// Load on demand of a subtree; add node parameter
var data = { node: node.id };
// tslint:disable-next-line: no-string-literal
url_info["data"] = data;
}
else {
// Add selected_node parameter
var selected_node_id = _this._getNodeIdToBeSelected();
if (selected_node_id) {
var data = { selected_node: selected_node_id };
// tslint:disable-next-line: no-string-literal
url_info["data"] = data;
}
}
return url_info;
};
if ($.isFunction(data_url)) {
return data_url(node);
}
else if ($.type(data_url) === "string") {
return getUrlFromString();
}
else {
return data_url;
}
};
JqTreeWidget.prototype._getNodeIdToBeSelected = function () {
if (this.options.saveState && this.save_state_handler) {
return this.save_state_handler.getNodeIdToBeSelected();
}
else {
return null;
}
};
JqTreeWidget.prototype._initTree = function (data) {
var _this = this;
var doInit = function () {
if (!_this.is_initialized) {
_this.is_initialized = true;
_this._triggerEvent("tree.init");
}
};
this.tree = new this.options.nodeClass(null, true, this.options.nodeClass);
if (this.select_node_handler) {
this.select_node_handler.clear();
}
this.tree.loadFromData(data);
var must_load_on_demand = this._setInitialState();
this._refreshElements(null);
if (!must_load_on_demand) {
doInit();
}
else {
// Load data on demand and then init the tree
this._setInitialStateOnDemand(doInit);
}
};
// Set initial state, either by restoring the state or auto-opening nodes
// result: must load nodes on demand?
JqTreeWidget.prototype._setInitialState = function () {
var _this = this;
var restoreState = function () {
// result: is state restored, must load on demand?
if (!(_this.options.saveState && _this.save_state_handler)) {
return [false, false];
}
else {
var state = _this.save_state_handler.getStateFromStorage();
if (!state) {
return [false, false];
}
else {
var must_load_on_demand_1 = _this.save_state_handler.setInitialState(state);
// return true: the state is restored
return [true, must_load_on_demand_1];
}
}
};
var autoOpenNodes = function () {
// result: must load on demand?
if (_this.options.autoOpen === false) {
return false;
}
var max_level = _this._getAutoOpenMaxLevel();
var must_load_on_demand = false;
_this.tree.iterate(function (node, level) {
if (node.load_on_demand) {
must_load_on_demand = true;
return false;
}
else if (!node.hasChildren()) {
return false;
}
else {
node.is_open = true;
return level !== max_level;
}
});
return must_load_on_demand;
};
// tslint:disable-next-line: prefer-const
var _a = restoreState(), is_restored = _a[0], must_load_on_demand = _a[1];
if (!is_restored) {
must_load_on_demand = autoOpenNodes();
}
return must_load_on_demand;
};
// Set the initial state for nodes that are loaded on demand
// Call cb_finished when done
JqTreeWidget.prototype._setInitialStateOnDemand = function (cb_finished) {
var _this = this;
var restoreState = function () {
if (!(_this.options.saveState && _this.save_state_handler)) {
return false;
}
else {
var state = _this.save_state_handler.getStateFromStorage();
if (!state) {
return false;
}
else {
_this.save_state_handler.setInitialStateOnDemand(state, cb_finished);
return true;
}
}
};
var autoOpenNodes = function () {
var max_level = _this._getAutoOpenMaxLevel();
var loading_count = 0;
var loadAndOpenNode = function (node) {
loading_count += 1;
_this._openNode(node, false, function () {
loading_count -= 1;
openNodes();
});
};
var openNodes = function () {
_this.tree.iterate(function (node, level) {
if (node.load_on_demand) {
if (!node.is_loading) {
loadAndOpenNode(node);
}
return false;
}
else {
_this._openNode(node, false, null);
return level !== max_level;
}
});
if (loading_count === 0) {
cb_finished();
}
};
openNodes();
};
if (!restoreState()) {
autoOpenNodes();
}
};
JqTreeWidget.prototype._getAutoOpenMaxLevel = function () {
if (this.options.autoOpen === true) {
return -1;
}
else {
return parseInt(this.options.autoOpen, 10);
}
};
JqTreeWidget.prototype._click = function (e) {
var click_target = this._getClickTarget(e.target);
if (click_target) {
if (click_target.type === "button") {
this.toggle(click_target.node, this.options.slide);
e.preventDefault();
e.stopPropagation();
}
else if (click_target.type === "label") {
var node = click_target.node;
var event_1 = this._triggerEvent("tree.click", {
node: node,
click_event: e
});
if (!event_1.isDefaultPrevented()) {
this._selectNode(node, true);
}
}
}
};
JqTreeWidget.prototype._dblclick = function (e) {
var click_target = this._getClickTarget(e.target);
if (click_target && click_target.type === "label") {
this._triggerEvent("tree.dblclick", {
node: click_target.node,
click_event: e
});
}
};
JqTreeWidget.prototype._getClickTarget = function (element) {
var $target = $(element);
var $button = $target.closest(".jqtree-toggler");
if ($button.length) {
var node = this._getNode($button);
if (node) {
return {
type: "button",
node: node
};
}
}
else {
var $el = $target.closest(".jqtree-element");
if ($el.length) {
var node = this._getNode($el);
if (node) {
return {
type: "label",
node: node
};
}
}
}
return null;
};
JqTreeWidget.prototype._getNode = function ($element) {
var $li = $element.closest("li.jqtree_common");
if ($li.length === 0) {
return null;
}
else {
return $li.data("node");
}
};
JqTreeWidget.prototype._contextmenu = function (e) {
var $div = $(e.target).closest("ul.jqtree-tree .jqtree-element");
if ($div.length) {
var node = this._getNode($div);
if (node) {
e.preventDefault();
e.stopPropagation();
this._triggerEvent("tree.contextmenu", {
node: node,
click_event: e
});
return false;
}
}
return null;
};
JqTreeWidget.prototype._saveState = function () {
if (this.options.saveState && this.save_state_handler) {
this.save_state_handler.saveState();
}
};
JqTreeWidget.prototype._selectCurrentNode = function () {
var node = this.getSelect