From 6c8ae2065a0c06d559def5e51da27430ec20c707 Mon Sep 17 00:00:00 2001 From: knowgod Date: Fri, 14 Aug 2015 09:16:36 +0300 Subject: [PATCH 1/2] Configurable collapser marks instead of "+'/"-" jQuery('#json_view').JSONView(batchJson, { marks:{ '+':'++', '-':'--', } }); --- dist/jquery.jsonview.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/dist/jquery.jsonview.js b/dist/jquery.jsonview.js index bb50a32..8c20eaa 100644 --- a/dist/jquery.jsonview.js +++ b/dist/jquery.jsonview.js @@ -1,7 +1,7 @@ /*! -jQuery JSONView. -Licensed under the MIT License. + jQuery JSONView. + Licensed under the MIT License. */ (function(jQuery) { var $, Collapser, JSONFormatter, JSONView; @@ -133,13 +133,20 @@ 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); 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); @@ -160,7 +167,7 @@ Licensed under the MIT License. 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) { @@ -174,7 +181,7 @@ Licensed under the MIT License. ellipsis.className = 'ellipsis'; ellipsis.innerHTML = ' … '; target.parentNode.insertBefore(ellipsis, target); - return collapser.innerHTML = '+'; + return collapser.innerHTML = this.marks['+']; }; Collapser.toggle = function(collapser, options) { @@ -212,12 +219,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); } }, From e2a569fb9647129535c32759d3590f50a3701db6 Mon Sep 17 00:00:00 2001 From: knowgod Date: Fri, 14 Aug 2015 09:47:57 +0300 Subject: [PATCH 2/2] Able to attach callbacks on expand/collapse: jQuery('#json_view').JSONView(batchJson, { on_expand: function(collapser){ console.log(['on_expand', collapser]); }, on_collapse: function(collapser){ console.log(['on_collapse', collapser]); } }); --- dist/jquery.jsonview.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/dist/jquery.jsonview.js b/dist/jquery.jsonview.js index 8c20eaa..dd8eef7 100644 --- a/dist/jquery.jsonview.js +++ b/dist/jquery.jsonview.js @@ -143,6 +143,13 @@ 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'; @@ -154,7 +161,7 @@ })(this)); item.insertBefore(collapser, item.firstChild); if (options.collapsed) { - return this.collapse(collapser); + return this.collapse(collapser, true); } }; @@ -164,18 +171,24 @@ 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 = 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'; @@ -257,7 +270,9 @@ defaultOptions = { collapsed: false, nl2br: false, - recursive_collapser: false + recursive_collapser: false, + on_collapse: null, + on_expand: null }; options = $.extend(defaultOptions, options); formatter = new JSONFormatter({