Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Add callback and marks options #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions dist/jquery.jsonview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/*!
jQuery JSONView.
Licensed under the MIT License.
jQuery JSONView.
Licensed under the MIT License.
*/
(function(jQuery) {
var $, Collapser, JSONFormatter, JSONView;
Expand Down Expand Up @@ -133,21 +133,35 @@ Licensed under the MIT License.
})();
(typeof module !== "undefined" && module !== null) && (module.exports = JSONFormatter);
Collapser = (function() {
function Collapser() {}
function Collapser() {
}

Collapser.marks = {
'+': '+',
'-': '-'
};

Collapser.bindEvent = function(item, options) {
$.extend(this.marks, options.marks);
if (options['on_expand']) {
this.on_expand = options['on_expand'];
}
if (options['on_collapse']) {
this.on_collapse = options['on_collapse'];
}

var collapser;
collapser = document.createElement('div');
collapser.className = 'collapser';
collapser.innerHTML = options.collapsed ? '+' : '-';
collapser.innerHTML = options.collapsed ? this.marks['+'] : this.marks['-'];
collapser.addEventListener('click', (function(_this) {
return function(event) {
return _this.toggle(event.target, options);
};
})(this));
item.insertBefore(collapser, item.firstChild);
if (options.collapsed) {
return this.collapse(collapser);
return this.collapse(collapser, true);
}
};

Expand All @@ -157,24 +171,30 @@ Licensed under the MIT License.
if (target.style.display === '') {
return;
}
if ('function' === typeof this.on_expand) {
this.on_expand(collapser);
}
ellipsis = target.parentNode.getElementsByClassName('ellipsis')[0];
target.parentNode.removeChild(ellipsis);
target.style.display = '';
return collapser.innerHTML = '-';
return collapser.innerHTML = this.marks['-'];
};

Collapser.collapse = function(collapser) {
Collapser.collapse = function(collapser, no_callback) {
var ellipsis, target;
target = this.collapseTarget(collapser);
if (target.style.display === 'none') {
return;
}
if (!no_callback && 'function' === typeof this.on_collapse) {
this.on_collapse(collapser);
}
target.style.display = 'none';
ellipsis = document.createElement('span');
ellipsis.className = 'ellipsis';
ellipsis.innerHTML = ' … ';
target.parentNode.insertBefore(ellipsis, target);
return collapser.innerHTML = '+';
return collapser.innerHTML = this.marks['+'];
};

Collapser.toggle = function(collapser, options) {
Expand Down Expand Up @@ -212,12 +232,12 @@ Licensed under the MIT License.
$ = jQuery;
JSONView = {
collapse: function(el) {
if (el.innerHTML === '-') {
if (el.innerHTML === Collapser.marks['-']) {
return Collapser.collapse(el);
}
},
expand: function(el) {
if (el.innerHTML === '+') {
if (el.innerHTML === Collapser.marks['+']) {
return Collapser.expand(el);
}
},
Expand Down Expand Up @@ -250,7 +270,9 @@ Licensed under the MIT License.
defaultOptions = {
collapsed: false,
nl2br: false,
recursive_collapser: false
recursive_collapser: false,
on_collapse: null,
on_expand: null
};
options = $.extend(defaultOptions, options);
formatter = new JSONFormatter({
Expand Down