theme.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.configTheme = Em.Object.create({
  20. /**
  21. * Resolve config theme conditions
  22. * in order to correctly calculate config errors number of service
  23. * @param {Array} configs
  24. */
  25. resolveConfigThemeConditions: function (configs) {
  26. App.ThemeCondition.find().forEach(function (configCondition) {
  27. var _configs = Em.A(configCondition.get('configs'));
  28. if (configCondition.get("resource") === 'config' && _configs.length > 0) {
  29. var isConditionTrue = this.calculateConfigCondition(configCondition.get("if"), configs);
  30. var action = isConditionTrue ? configCondition.get("then") : configCondition.get("else");
  31. if (configCondition.get('id')) {
  32. var valueAttributes = action.property_value_attributes;
  33. if (valueAttributes && !Em.none(valueAttributes['visible'])) {
  34. var themeResource;
  35. if (configCondition.get('type') === 'subsection') {
  36. themeResource = App.SubSection.find().findProperty('name', configCondition.get('name'));
  37. } else if (configCondition.get('type') === 'subsectionTab') {
  38. themeResource = App.SubSectionTab.find().findProperty('name', configCondition.get('name'));
  39. } else if (configCondition.get('type') === 'config') {
  40. //simulate section wrapper for condition type "config"
  41. themeResource = Em.Object.create({
  42. configProperties: [App.config.configId(configCondition.get('configName'), configCondition.get('fileName'))]
  43. });
  44. }
  45. if (themeResource) {
  46. themeResource.get('configProperties').forEach(function (_configId) {
  47. configs.forEach(function (item) {
  48. if (App.config.configId(item.name, item.filename) === _configId) {
  49. // if config has already been hidden by condition with "subsection" or "subsectionTab" type
  50. // then ignore condition of "config" type
  51. if (configCondition.get('type') === 'config' && item.hiddenBySection) return false;
  52. item.hiddenBySection = !valueAttributes['visible'];
  53. }
  54. });
  55. }, this);
  56. }
  57. }
  58. }
  59. }
  60. });
  61. },
  62. /**
  63. *
  64. * @param {string} ifStatement
  65. * @param {Array} serviceConfigs
  66. * @returns {boolean}
  67. */
  68. calculateConfigCondition: function(ifStatement, serviceConfigs) {
  69. // Split `if` statement if it has logical operators
  70. var ifStatementRegex = /(&&|\|\|)/;
  71. var IfConditions = ifStatement.split(ifStatementRegex);
  72. var allConditionResult = [];
  73. IfConditions.forEach(function(_condition){
  74. var condition = _condition.trim();
  75. if (condition === '&&' || condition === '||') {
  76. allConditionResult.push(_condition);
  77. } else {
  78. var splitIfCondition = condition.split('===');
  79. var ifCondition = splitIfCondition[0];
  80. var result = splitIfCondition[1] || "true";
  81. var parseIfConditionVal = ifCondition;
  82. var regex = /\$\{.*?\}/g;
  83. var configStrings = ifCondition.match(regex);
  84. configStrings.forEach(function (_configString) {
  85. var configObject = _configString.substring(2, _configString.length - 1).split("/");
  86. var config = serviceConfigs.filterProperty('filename', configObject[0] + '.xml').findProperty('name', configObject[1]);
  87. if (config) {
  88. var configValue = Em.get(config, 'value');
  89. parseIfConditionVal = parseIfConditionVal.replace(_configString, configValue);
  90. }
  91. }, this);
  92. var conditionResult = window.eval(JSON.stringify(parseIfConditionVal.trim())) === result.trim();
  93. allConditionResult.push(conditionResult);
  94. }
  95. }, this);
  96. return Boolean(window.eval(allConditionResult.join('')));
  97. }
  98. });