|
| 1 | + |
| 2 | +define(['require', 'jquery', 'validationUtil', 'jsonlint', 'waitDialog' |
| 3 | + , 'initApp' |
| 4 | + ], |
| 5 | +function(require, $, validationUtil, jsonlint, waitDialog |
| 6 | +// , init |
| 7 | +){ |
| 8 | + |
| 9 | + /** |
| 10 | + * App initializer (triggered on-doc-ready by jQuery; see below) |
| 11 | + * |
| 12 | + * @private |
| 13 | + * @type Function |
| 14 | + * @memberOf TestGrammarApp |
| 15 | + */ |
| 16 | + var _initAppOnDocReady = function() { |
| 17 | + |
| 18 | +// console.log('dom ready'); |
| 19 | + |
| 20 | + require(['jsonEditor', 'parseOptions'], function(edt, opt){ |
| 21 | + |
| 22 | + edt.setAutoValidationEnabled(true); |
| 23 | + |
| 24 | + initValidateControls(edt, opt); |
| 25 | + |
| 26 | + _hideLoader(); |
| 27 | + |
| 28 | + var locField = opt.isLoc() && opt.getLocField(); |
| 29 | + edt.validate(locField); |
| 30 | + edt.setAutoValidationEnabled(locField); |
| 31 | + }); |
| 32 | + }; |
| 33 | + //register with jQuery ondocready: |
| 34 | + $(_initAppOnDocReady); |
| 35 | + |
| 36 | + |
| 37 | + function initValidateControls(editor, options){ |
| 38 | + |
| 39 | + $("#reformat,#strict-mode,#extract-loc,#advanced-options,#convert-array,#loc-field").on('change', function(evt){ |
| 40 | + updateControls(options); |
| 41 | + var locField = options.getLocField(); |
| 42 | + editor.setAutoValidationEnabled(locField); |
| 43 | + editor.validate(options.isLoc() && locField); |
| 44 | + }); |
| 45 | + |
| 46 | + document.getElementById("button").onclick = function () { |
| 47 | + try { |
| 48 | + |
| 49 | + var isLoc = options.isLoc(); |
| 50 | + if (isLoc) { |
| 51 | + jsonlint.parser.setLocEnabled(options.getLocField());//enable extraction of location meta data |
| 52 | + } |
| 53 | + else { |
| 54 | + jsonlint.parser.setLocEnabled(false);//enable extraction of location meta data |
| 55 | + } |
| 56 | + |
| 57 | + if (options.isStrict()) { |
| 58 | + jsonlint.parser.setStrict(true);//enable extraction of location meta data |
| 59 | + } |
| 60 | + else { |
| 61 | + jsonlint.parser.setStrict(false);//enable extraction of location meta data |
| 62 | + } |
| 63 | + |
| 64 | + var result = jsonlint.parse(editor.getText()); |
| 65 | + if (result) { |
| 66 | + var resultEl = $("#result"); |
| 67 | + resultEl.find(".text").text("JSON is valid!"); |
| 68 | + resultEl.attr("class", "pass"); |
| 69 | + if (options.isReformat()) { |
| 70 | + var convFunc = isLoc && options.isConvertArrays()? convertArrayValues : null; |
| 71 | + editor.setText( JSON.stringify(result, convFunc, " ") ); |
| 72 | + } |
| 73 | + } |
| 74 | + } catch(e) { |
| 75 | + var resultEl = $("#result"); |
| 76 | + resultEl.find(".text").text(e); |
| 77 | + resultEl.attr("class", "fail"); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + updateControls(options); |
| 82 | + } |
| 83 | + |
| 84 | + function updateControls(options){ |
| 85 | + var isLoc = options.isLoc(); |
| 86 | + |
| 87 | + var enableConvertField = options.isReformat() && isLoc; |
| 88 | + $("#convert-array").prop('disabled', !enableConvertField); |
| 89 | + |
| 90 | + var isAdvanced = options.isAdvanced(); |
| 91 | + $('#advanved-options-set')[isAdvanced? 'show' : 'hide'](); |
| 92 | + |
| 93 | + var locInputField = $('#loc-field'); |
| 94 | + locInputField.prop('disabled', !isLoc); |
| 95 | + |
| 96 | + var parserLocField = jsonlint.parser.getLoc(); |
| 97 | + var optionsLocField = options.getLocField(); |
| 98 | + if(parserLocField && !optionsLocField){ |
| 99 | + locInputField.val(parserLocField); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * helper function for using as JSON.stringify converter: |
| 105 | + * do convert Arrays to Objects in order to make the |
| 106 | + * additional (non-standart Array member) with the location |
| 107 | + * information visible. |
| 108 | + * |
| 109 | + * @private |
| 110 | + * @type Function |
| 111 | + * @memberOf TestGrammarApp |
| 112 | + */ |
| 113 | + function convertArrayValues(n,v){ |
| 114 | + if(Array.isArray(v)){ |
| 115 | + var obj = {_type: 'Array'}; |
| 116 | + var loc = jsonlint.parser.getLoc(); |
| 117 | + obj[loc] = v[loc]; |
| 118 | + for(var i=0,s=v.length;i<s;++i) obj[i]=v[i]; |
| 119 | + return obj; |
| 120 | + } |
| 121 | + return v; |
| 122 | + } |
| 123 | + |
| 124 | + var _hideLoaderTimer; |
| 125 | + var _showLoaderTimer; |
| 126 | + /** |
| 127 | + * Shows a wait dialog. |
| 128 | + * |
| 129 | + * @private |
| 130 | + * @type Function |
| 131 | + * @memberOf TestGrammarApp |
| 132 | + */ |
| 133 | + function _showLoader(text, delay, func, argsArray) { |
| 134 | + |
| 135 | + clearTimeout(_hideLoaderTimer); |
| 136 | + |
| 137 | + if (!delay) { |
| 138 | + _showLoaderTimer = setTimeout(function() { |
| 139 | + waitDialog.show(text, 'app'); |
| 140 | + if(func){ |
| 141 | + func.apply(null, argsArray); |
| 142 | + } |
| 143 | + }, 50); |
| 144 | + |
| 145 | + } else { |
| 146 | + _showLoaderTimer = setTimeout(function() { |
| 147 | + |
| 148 | + waitDialog.show(text, 'app'); |
| 149 | + |
| 150 | + if(func){ |
| 151 | + setTimeout(function() { |
| 152 | + func.apply(null, argsArray); |
| 153 | + }, delay); |
| 154 | + } |
| 155 | +// } |
| 156 | + }, 50); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Hides the wait dialog |
| 163 | + * |
| 164 | + * @private |
| 165 | + * @type Function |
| 166 | + * @memberOf TestGrammarApp |
| 167 | + */ |
| 168 | + function _hideLoader() { |
| 169 | + |
| 170 | + clearTimeout(_showLoaderTimer); |
| 171 | + |
| 172 | + _hideLoaderTimer = setTimeout(function(){ |
| 173 | + waitDialog.hide('app'); |
| 174 | + }, 50); |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * HELPER for converting the current editor input (or the text argument) |
| 180 | + * into a JSON object. |
| 181 | + * |
| 182 | + * If the text is an invalid JSON definition, an error dialog will be shown |
| 183 | + * to the user. |
| 184 | + * |
| 185 | + * NOTE this helper should only be used in reaction to an explicit user action. |
| 186 | + * |
| 187 | + * @private |
| 188 | + * @type Function |
| 189 | + * @memberOf TestGrammarApp |
| 190 | + */ |
| 191 | + function _inputTextToJSON(view, text) { |
| 192 | + |
| 193 | + if(typeof text === 'undefined'){ |
| 194 | + text = view.getJsonGrammarText(); |
| 195 | + } |
| 196 | + |
| 197 | + var jsonObj = validationUtil.validateJson(text, function(err){ |
| 198 | + |
| 199 | +// var doSelectLine = function(){ |
| 200 | +// util.selectLine(lineNo, view.getEditor()); |
| 201 | +// setTimeout(function() { |
| 202 | +// util.selectLine(lineNo, view.getEditor()); |
| 203 | +// }, 500); |
| 204 | +// }; |
| 205 | + |
| 206 | + var msg = err.message; |
| 207 | + |
| 208 | + |
| 209 | +// _showErrorDialog('Error: Invalid JSON Fromat', |
| 210 | +// 'Error on parsing grammar text into a JSON object.', |
| 211 | +// '<pre>' + msg + '</pre>' |
| 212 | +//// , doSelectLine, doSelectLine |
| 213 | +// ); |
| 214 | + |
| 215 | + }); |
| 216 | + |
| 217 | + if(jsonObj){ |
| 218 | + return jsonObj; |
| 219 | + } |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + return {}; |
| 224 | +}); |
0 commit comments