|
1 |
| -/*! JHson.js v1.1.0 | (c) Bunoon 2024 | MIT License */ |
| 1 | +/*! JHson.js v1.2.0 | (c) Bunoon 2024 | MIT License */ |
2 | 2 | (function() {
|
3 |
| - var _parameter_Document = null, _parameter_Window = null, _parameter_JSON = null, _parameter_Math = null, _public = {}, _configuration = {}, _elements_Type = {}, _string = {empty:"", space:" ", newLine:"\n"}, _json = {text:"#text", cssStyle:"$", attribute:"@", children:"&children",}, _value = {notFound:-1}, _attribute_Name_Options = "data-jhson-options"; |
| 3 | + var _parameter_Document = null, _parameter_Window = null, _parameter_JSON = null, _parameter_Math = null, _public = {}, _configuration = {}, _elements_Type = {}, _string = {empty:"", space:" ", newLine:"\n", variableStart:"{{", variableEnd:"}}", variableDefault:"|"}, _json = {text:"#text", cssStyle:"$", attribute:"@", children:"&children",}, _value = {notFound:-1}, _attribute_Name_Options = "data-jhson-js"; |
4 | 4 | function render() {
|
5 | 5 | var tagTypes = _configuration.domElementTypes, tagTypesLength = tagTypes.length;
|
6 | 6 | for (var tagTypeIndex = 0; tagTypeIndex < tagTypesLength; tagTypeIndex++) {
|
|
49 | 49 | fireCustomTrigger(bindingOptions.onRenderComplete, bindingOptions.element);
|
50 | 50 | }
|
51 | 51 | function buildAttributeOptions(newOptions) {
|
52 |
| - var options = !isDefinedObject(newOptions) ? {} : newOptions, optionPropertyDefaults = getDefaultHtmlProperties(); |
| 52 | + var options = getDefaultObject(newOptions, {}), optionPropertyDefaults = getDefaultHtmlProperties(); |
53 | 53 | options.json = getDefaultString(options.json, optionPropertyDefaults.json);
|
54 | 54 | options.templateData = getDefaultObject(options.templateData, optionPropertyDefaults.templateData);
|
55 | 55 | options.removeOriginalAttributes = getDefaultBoolean(options.removeOriginalAttributes, optionPropertyDefaults.removeOriginalAttributes);
|
|
182 | 182 | function writeHtml(element, properties) {
|
183 | 183 | if (isDefinedObject(element) && isDefinedString(properties.json)) {
|
184 | 184 | var convertedJsonObject = getObjectFromString(properties.json), writingScope = {css:{}, templateDataKeys:[], templateDataKeysLength:0, templateDataKeysProcessed:[]};
|
185 |
| - if (properties.clearCssFromHead) { |
186 |
| - clearCssStyleTagsFromHead(); |
187 |
| - } |
188 |
| - if (isDefinedObject(properties.templateData)) { |
189 |
| - setupWritingScopeTemplateDataKeys(properties, writingScope); |
190 |
| - } |
191 | 185 | if (convertedJsonObject.parsed && isDefinedObject(convertedJsonObject.result)) {
|
| 186 | + if (properties.clearCssFromHead) { |
| 187 | + clearCssStyleTagsFromHead(); |
| 188 | + } |
| 189 | + if (isDefinedObject(properties.templateData)) { |
| 190 | + setupWritingScopeTemplateDataKeys(properties, writingScope); |
| 191 | + } |
192 | 192 | for (var key in convertedJsonObject.result) {
|
193 | 193 | if (key === element.nodeName.toLowerCase()) {
|
194 | 194 | if (properties.removeOriginalAttributes) {
|
|
202 | 202 | writeNode(element, convertedJsonObject.result[key], properties, writingScope);
|
203 | 203 | }
|
204 | 204 | }
|
205 |
| - } |
206 |
| - if (properties.addCssToHead) { |
207 |
| - writeCssStyleTag(writingScope); |
208 |
| - } |
209 |
| - if (properties.logTemplateDataWarnings) { |
210 |
| - checkedForUnusedTemplateData(writingScope); |
| 205 | + processRemainingVariablesForDefaults(element); |
| 206 | + if (properties.addCssToHead) { |
| 207 | + writeCssStyleTag(writingScope); |
| 208 | + } |
| 209 | + if (properties.logTemplateDataWarnings) { |
| 210 | + checkedForUnusedTemplateData(writingScope); |
| 211 | + } |
211 | 212 | }
|
212 | 213 | }
|
213 | 214 | return _public;
|
|
268 | 269 | if (writingScope.templateDataKeysLength > 0) {
|
269 | 270 | for (var templateDataKeyIndex = 0; templateDataKeyIndex < writingScope.templateDataKeysLength; templateDataKeyIndex++) {
|
270 | 271 | var templateDataKey = writingScope.templateDataKeys[templateDataKeyIndex];
|
271 |
| - if (properties.templateData.hasOwnProperty(templateDataKey) && element.innerHTML.indexOf(templateDataKey) > _value.notFound) { |
272 |
| - element.innerHTML = replaceAll(element.innerHTML, templateDataKey, properties.templateData[templateDataKey]); |
273 |
| - if (writingScope.templateDataKeysProcessed.indexOf(templateDataKey) === _value.notFound) { |
274 |
| - writingScope.templateDataKeysProcessed.push(templateDataKey); |
| 272 | + if (properties.templateData.hasOwnProperty(templateDataKey)) { |
| 273 | + var templateDataKeyReplacement = properties.templateData[templateDataKey]; |
| 274 | + if (element.innerHTML.indexOf(templateDataKey) > _value.notFound) { |
| 275 | + element.innerHTML = replaceAll(element.innerHTML, templateDataKey, templateDataKeyReplacement); |
| 276 | + if (writingScope.templateDataKeysProcessed.indexOf(templateDataKey) === _value.notFound) { |
| 277 | + writingScope.templateDataKeysProcessed.push(templateDataKey); |
| 278 | + } |
| 279 | + } else { |
| 280 | + templateDataKey = templateDataKey.replace(_string.variableEnd, _string.empty) + _string.space + _string.variableDefault; |
| 281 | + var startIndex = element.innerHTML.indexOf(templateDataKey); |
| 282 | + if (startIndex > _value.notFound) { |
| 283 | + var endIndex = element.innerHTML.indexOf(_string.variableEnd, startIndex); |
| 284 | + if (endIndex > _value.notFound) { |
| 285 | + var variable = element.innerHTML.substring(startIndex, endIndex + _string.variableEnd.length); |
| 286 | + element.innerHTML = replaceAll(element.innerHTML, variable, templateDataKeyReplacement); |
| 287 | + } |
| 288 | + } |
275 | 289 | }
|
276 | 290 | }
|
277 | 291 | }
|
|
322 | 336 | }
|
323 | 337 | }
|
324 | 338 | }
|
| 339 | + function processRemainingVariablesForDefaults(element) { |
| 340 | + var remainingVariables = getTemplateVariables(element.innerHTML), remainingVariablesLength = remainingVariables.length; |
| 341 | + for (var remainingVariableIndex = 0; remainingVariableIndex < remainingVariablesLength; remainingVariableIndex++) { |
| 342 | + var variable = remainingVariables[remainingVariableIndex]; |
| 343 | + if (variable.indexOf(_string.variableDefault) > _value.notFound) { |
| 344 | + var defaultValue = variable.replace(_string.variableStart, _string.empty).replace(_string.variableEnd, _string.empty).split(_string.variableDefault)[1]; |
| 345 | + if (isDefinedString(defaultValue)) { |
| 346 | + element.innerHTML = element.innerHTML.replace(variable, defaultValue.trim()); |
| 347 | + } |
| 348 | + } |
| 349 | + } |
| 350 | + } |
325 | 351 | function getTemplateVariables(data) {
|
326 | 352 | var result = [];
|
327 | 353 | if (isDefinedString(data)) {
|
328 | 354 | var startIndex = 0, endIndex = 0;
|
329 | 355 | while (startIndex > _value.notFound) {
|
330 |
| - startIndex = data.indexOf("{{", endIndex); |
| 356 | + startIndex = data.indexOf(_string.variableStart, endIndex); |
331 | 357 | if (startIndex > _value.notFound) {
|
332 |
| - endIndex = data.indexOf("}}", startIndex); |
| 358 | + endIndex = data.indexOf(_string.variableEnd, startIndex); |
333 | 359 | if (endIndex > _value.notFound) {
|
334 |
| - var variable = data.substring(startIndex, endIndex + 2); |
| 360 | + var variable = data.substring(startIndex, endIndex + _string.variableEnd.length); |
335 | 361 | result.push(variable);
|
336 | 362 | endIndex += 2;
|
337 | 363 | }
|
|
390 | 416 | return data.substring(0, start.length).toLowerCase() === start.toLowerCase();
|
391 | 417 | }
|
392 | 418 | function replaceAll(string, find, replace) {
|
393 |
| - return string.replace(new RegExp(find, "g"), replace); |
| 419 | + return string.replace(new RegExp(find.replace(_string.variableDefault, "[" + _string.variableDefault + "]"), "g"), replace); |
394 | 420 | }
|
395 | 421 | function getDefaultBoolean(value, defaultValue) {
|
396 | 422 | return isDefinedBoolean(value) ? value : defaultValue;
|
|
532 | 558 | };
|
533 | 559 | _public.setConfiguration = function(newConfiguration) {
|
534 | 560 | var configurationChanges = false;
|
535 |
| - for (var propertyName in newConfiguration) { |
536 |
| - if (newConfiguration.hasOwnProperty(propertyName) && _configuration.hasOwnProperty(propertyName) && newConfiguration[propertyName] !== _configuration[propertyName]) { |
537 |
| - _configuration[propertyName] = newConfiguration[propertyName]; |
538 |
| - configurationChanges = true; |
| 561 | + if (isDefinedObject(newConfiguration)) { |
| 562 | + for (var propertyName in newConfiguration) { |
| 563 | + if (newConfiguration.hasOwnProperty(propertyName) && _configuration.hasOwnProperty(propertyName) && newConfiguration[propertyName] !== _configuration[propertyName]) { |
| 564 | + _configuration[propertyName] = newConfiguration[propertyName]; |
| 565 | + configurationChanges = true; |
| 566 | + } |
| 567 | + } |
| 568 | + if (configurationChanges) { |
| 569 | + buildDefaultConfiguration(_configuration); |
539 | 570 | }
|
540 |
| - } |
541 |
| - if (configurationChanges) { |
542 |
| - buildDefaultConfiguration(_configuration); |
543 | 571 | }
|
544 | 572 | return _public;
|
545 | 573 | };
|
|
557 | 585 | _configuration.attributeNotSetErrorText = getDefaultString(_configuration.attributeNotSetErrorText, "The attribute '{{attribute_name}}' has not been set correctly.");
|
558 | 586 | }
|
559 | 587 | _public.getVersion = function() {
|
560 |
| - return "1.1.0"; |
| 588 | + return "1.2.0"; |
561 | 589 | };
|
562 | 590 | (function(documentObject, windowObject, jsonObject, mathObject) {
|
563 | 591 | _parameter_Document = documentObject;
|
|
0 commit comments