1 /* 2 JWL - The JavaScript Widget Library version 0.8.7 3 Copyright (c) 2016 - 2020 The Zonebuilder <zone.builder@gmx.com> 4 http://sourceforge.net/projects/jwl-library/ 5 Licenses: GNU GPL2 or later; GNU LGPLv3 or later (http://sourceforge.net/p/jwl-library/wiki/License/) 6 */ 7 /** 8 @fileOverview JWL.components.jsonoptions configuration 9 */ 10 /* jshint browser: true, curly: true, eqeqeq: true, expr: true, funcscope: true, immed: true, latedef: true, loopfunc: true, 11 onevar: true, newcap: true, noarg: true, node: true, strict: true, trailing: true, undef: true, unused: vars, wsh: true */ 12 /* globals JUL */ 13 14 (function(global, module) { 15 'use strict'; 16 if (module && module.exports && typeof require === 'function') { require('jul'); } 17 var jul = new JUL.Instance({nsRoot: module && module.exports ? {JWL: module.exports} : global || null}); 18 var JWL = jul.ns('JWL'); 19 20 /* generated by JCS version 1.5.8 */ 21 22 /** 23 Options object stored as JSON 24 @namespace JWL.components.jsonoptions namespace 25 @name JWL.components.jsonoptions 26 */ 27 jul.ns('JWL.components.jsonoptions'); 28 29 jul.apply(jul.get('JWL.components.jsonoptions'), /** @lends JWL.components.jsonoptions */ { 30 /** 31 Component UI 32 @type Object 33 */ 34 ui: { 35 tag: 'div', cid: '.jsonoptions', css: 'jsonoptions', children: [ 36 {tag: 'a', cid: '.jsonoptions-show', css: 'fa fa-gear', href: '#', title: 'Options'}, 37 {tag: 'div', css: 'jsonoptions-opts-wrap', children: [ 38 {tag: 'div', css: 'jsonoptions-opts', children: [ 39 {tag: 'textarea', cid: '.jsonoptions-edit', cols: '30', css: 'jsonoptions-edit', rows: '10', wrap: 'off'}, 40 {tag: 'div', css: 'jsonoptions-buttons', children: [ 41 {tag: 'button', cid: '.jsonoptions-ok', html: 'OK'}, 42 {tag: 'button', cid: '.jsonoptions-cancel', html: 'Cancel'} 43 ]} 44 ]} 45 ]} 46 ] 47 }, 48 /** 49 Component logic 50 @type Object 51 */ 52 logic: { 53 '.jsonoptions': { 54 'data-options': '', listeners: { 55 saveoptions: function () { 56 this.saveOptions(); 57 }, 58 toggleoptions: function () { 59 this.showOptions(); 60 } 61 } 62 }, 63 '.jsonoptions-cancel': { 64 listeners: { 65 click: function () { 66 JWL.trigger(this, 'toggleoptions'); 67 } 68 } 69 }, 70 '.jsonoptions-ok': { 71 listeners: { 72 click: function () { 73 JWL.trigger(this, 'saveoptions'); 74 } 75 } 76 }, 77 '.jsonoptions-show': { 78 listeners: { 79 click: function (oEvent) { 80 oEvent = oEvent || event; 81 try { oEvent.preventDefault(); } catch(e) {} 82 JWL.trigger(this, 'toggleoptions'); 83 return false; 84 } 85 } 86 } 87 }, 88 /** 89 Component prototype members 90 @type Object 91 */ 92 prototype: { 93 /** 94 Toggles the display of options editing 95 @param {Boolean} [bHide] Set it to true to force hiding 96 */ 97 showOptions: function(bHide) { 98 var oOpts = JWL.get(this).querySelector('.jsonoptions-opts'); 99 var sClass = oOpts.getAttribute('class') || ''; 100 if (bHide || sClass.indexOf('shown') > -1) { 101 oOpts.setAttribute('class', sClass.replace('shown', '')); 102 } 103 else { 104 var sJson = this.getAttribute('data-options') || ''; 105 try { sJson = JUL.UI.obj2str(JSON.parse(sJson), true); } catch (e) {} 106 JWL.get(this).querySelector('.jsonoptions-edit').value = sJson; 107 oOpts.setAttribute('class', sClass + ' shown'); 108 } 109 }, 110 /** 111 Saves the edited options 112 */ 113 saveOptions: function() { 114 var sJson = JUL.trim(JWL.get(this).querySelector('.jsonoptions-edit').value); 115 if (sJson) { 116 try { sJson = JSON.stringify(JSON.parse(sJson)); } 117 catch (e) { window.alert('Text must be valid JSON.\n' + (e.description || e.message)); return; } 118 } 119 this.setAttribute('data-options', sJson); 120 JWL.trigger(this, 'optionschanged'); 121 this.showOptions(true); 122 } 123 }, 124 /** 125 Array of internal/external CSS for registering the custom element 126 @type Array|String 127 */ 128 css: ['lib/faws/css/font-awesome.min.css?v=0.8.5', 129 'lib/jwl/css/jsonoptions.css?v=0.8.5'] 130 }); 131 132 })(typeof global !== 'undefined' ? global : window, typeof module !== 'undefined' ? module : null); 133 134 /* end JWL.components.jsonoptions.js */ 135