themes_mapper.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. var App = require("app");
  18. App.themesMapper = App.QuickDataMapper.create({
  19. tabModel: App.Tab,
  20. sectionModel: App.Section,
  21. subSectionModel: App.SubSection,
  22. tabConfig: {
  23. "id": "name",
  24. "name": "name",
  25. "display_name": "display-name",
  26. "columns": "layout.tab-columns",
  27. "rows": "layout.tab-rows",
  28. "service_name": "service_name",
  29. "sections_key": "sections"
  30. },
  31. sectionConfig: {
  32. "id": "name",
  33. "name": "name",
  34. "display_name": "display-name",
  35. "row_index": "row-index",
  36. "column_index": "column-index",
  37. "row_span": "row-span",
  38. "column_span": "column-span",
  39. "section_columns": "section-columns",
  40. "section_rows": "section-rows",
  41. "tab_id": "tab_id"
  42. },
  43. subSectionConfig: {
  44. "id": "name",
  45. "name": "name",
  46. "display_name": "display-name",
  47. "border": "border",
  48. "row_index": "row-index",
  49. "column_index": "column-index",
  50. "column_span": "column-span",
  51. "row_span": "row-span",
  52. "configProperties": "config_properties",
  53. "section_id": "section_id"
  54. },
  55. map: function (json) {
  56. var tabs = [];
  57. json.items.forEach(function(item) {
  58. this.mapThemeLayouts(item, tabs);
  59. this.mapThemeConfigs(item);
  60. this.mapThemeWidgets(item);
  61. }, this);
  62. App.store.loadMany(this.get("tabModel"), tabs);
  63. App.store.commit();
  64. },
  65. /**
  66. * Bootstrap tab objects and link sections with subsections.
  67. *
  68. * @param {Object} json - json to parse
  69. * @param {Object[]} tabs - tabs list
  70. */
  71. mapThemeLayouts: function(json, tabs) {
  72. var serviceName = Em.get(json, "ThemeInfo.service_name");
  73. Em.getWithDefault(json, "ThemeInfo.theme_data.Theme.configuration.layouts", []).forEach(function(layout) {
  74. if (layout.tabs) {
  75. layout.tabs.forEach(function(tab) {
  76. var parsedTab = this.parseIt(tab, this.get("tabConfig"));
  77. parsedTab.id = serviceName + "_" + tab.name;
  78. parsedTab.service_name = serviceName;
  79. if (Em.get(tab, "layout.sections")) {
  80. var sections = [];
  81. Em.get(tab, "layout.sections").forEach(function(section) {
  82. var parsedSection = this.parseIt(section, this.get("sectionConfig"));
  83. parsedSection.tab_id = parsedTab.id;
  84. if (section.subsections) {
  85. var subSections = [];
  86. section.subsections.forEach(function(subSection) {
  87. var parsedSubSection = this.parseIt(subSection, this.get("subSectionConfig"));
  88. parsedSubSection.section_id = parsedSection.id;
  89. subSections.push(parsedSubSection);
  90. }, this);
  91. App.store.loadMany(this.get("subSectionModel"), subSections);
  92. parsedSection.sub_sections = subSections.mapProperty("id");
  93. }
  94. sections.push(parsedSection);
  95. }, this);
  96. App.store.loadMany(this.get("sectionModel"), sections);
  97. parsedTab.sections = sections.mapProperty("id");
  98. }
  99. tabs.push(parsedTab);
  100. }, this);
  101. }
  102. }, this);
  103. },
  104. /**
  105. * create tie between <code>stackConfigProperty<code> and <code>subSection<code>
  106. *
  107. * @param {Object} json - json to parse
  108. */
  109. mapThemeConfigs: function(json) {
  110. Em.getWithDefault(json, "ThemeInfo.theme_data.Theme.configuration.placement.configs", []).forEach(function(configLink) {
  111. var configId = this.getConfigId(configLink);
  112. var subSectionId = configLink["subsection-name"];
  113. var subSection = App.SubSection.find(subSectionId);
  114. var configProperty = App.StackConfigProperty.find(configId);
  115. if (configProperty && subSection) {
  116. subSection.get('configProperties').pushObject(configProperty);
  117. configProperty.set('subSection', subSection);
  118. } else {
  119. console.warn('there is no such property: ' + configId + '. Or subsection: ' + subSectionId);
  120. }
  121. }, this);
  122. },
  123. /**
  124. * add widget object to <code>stackConfigProperty<code>
  125. *
  126. * @param {Object} json - json to parse
  127. */
  128. mapThemeWidgets: function(json) {
  129. Em.getWithDefault(json, "ThemeInfo.theme_data.Theme.configuration.widgets", []).forEach(function(widget) {
  130. var configId = this.getConfigId(widget);
  131. var configProperty = App.StackConfigProperty.find(configId);
  132. if (configProperty) {
  133. configProperty.set('widget', widget.widget);
  134. } else {
  135. console.warn('there is no such property: ' + configId);
  136. }
  137. }, this);
  138. },
  139. /**
  140. * transform info from json to config id
  141. * @param {Object} json
  142. * @returns {string|null}
  143. */
  144. getConfigId: function(json) {
  145. if (json && json.config && typeof json.config === "string") {
  146. var split = json.config.split("/");
  147. return App.config.configId(split[1], split[0]);
  148. } else {
  149. console.warn('getConfigId: invalid input data');
  150. return null;
  151. }
  152. },
  153. /**
  154. * generates Advanced tabs for selected or all services
  155. * @param {[string]} [serviceNames=null]
  156. */
  157. generateAdvancedTabs: function(serviceNames) {
  158. serviceNames = Em.isArray(serviceNames) ? serviceNames : App.StackService.find().mapProperty('serviceName');
  159. var advancedTabs = [];
  160. serviceNames.forEach(function(serviceName) {
  161. advancedTabs.pushObject({
  162. id: serviceName + '_advanced',
  163. name: 'advanced',
  164. display_name: 'Advanced',
  165. is_advanced: true,
  166. service_name: serviceName
  167. });
  168. });
  169. App.store.commit();
  170. App.store.loadMany(this.get("tabModel"), advancedTabs);
  171. App.store.commit();
  172. }
  173. });