PackageSourceWidget.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2013 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/lang",
  19. "dojo/dom",
  20. "dijit/layout/_LayoutWidget",
  21. "dijit/_TemplatedMixin",
  22. "dijit/_WidgetsInTemplateMixin",
  23. "dijit/layout/BorderContainer",
  24. "dijit/layout/ContentPane",
  25. "dijit/registry",
  26. "hpcc/WsPackageMaps",
  27. "dojo/text!../templates/PackageSourceWidget.html",
  28. ],
  29. function (declare, lang, dom,
  30. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin,
  31. BorderContainer, ContentPane, registry,
  32. WsPackageMaps, template) {
  33. return declare("PackageSourceWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  34. templateString: template,
  35. baseClass: "PackageSourceWidget",
  36. borderContainer: null,
  37. editor: null,
  38. readOnly: true,
  39. buildRendering: function (args) {
  40. this.inherited(arguments);
  41. },
  42. postCreate: function (args) {
  43. this.inherited(arguments);
  44. this.borderContainer = registry.byId(this.id + "BorderContainer");
  45. },
  46. startup: function (args) {
  47. this.inherited(arguments);
  48. },
  49. resize: function (args) {
  50. this.inherited(arguments);
  51. this.borderContainer.resize();
  52. },
  53. layout: function (args) {
  54. this.inherited(arguments);
  55. },
  56. init: function (params) {
  57. if (this.initalized)
  58. return;
  59. this.initalized = true;
  60. this.editor = CodeMirror.fromTextArea(document.getElementById(this.id + "XMLCode"), {
  61. tabMode: "indent",
  62. matchBrackets: true,
  63. gutter: true,
  64. lineNumbers: true,
  65. mode: this.isXmlContent ? "xml" : "ecl",
  66. readOnly: this.readOnly,
  67. gutter: this.isXmlContent ? true : false,
  68. onGutterClick: CodeMirror.newFoldFunction(CodeMirror.tagRangeFinder)
  69. });
  70. dom.byId(this.id + "XMLContent").style.backgroundColor = this.readOnly ? 0xd0d0d0 : 0xffffff;
  71. var context = this;
  72. if (this.isXmlContent) {
  73. WsPackageMaps.getPackageMapById(params, {
  74. load: function (response) {
  75. context.editor.setValue(response);
  76. },
  77. error: function (errMsg, errStack) {
  78. context.showErrors(errMsg, errStack);
  79. }
  80. });
  81. }
  82. else {
  83. WsPackageMaps.validatePackage(params, {
  84. load: function (response) {
  85. var responseText = context.validateResponseToText(response);
  86. //console.log(responseText);
  87. if (responseText == '')
  88. context.editor.setValue("(Empty)");
  89. else
  90. context.editor.setValue(responseText);
  91. },
  92. error: function (errMsg, errStack) {
  93. context.showErrors(errMsg, errStack);
  94. }
  95. });
  96. }
  97. },
  98. showErrors: function (errMsg, errStack) {
  99. dojo.publish("hpcc/brToaster", {
  100. Severity: "Error",
  101. Source: errMsg,
  102. Exceptions: [{ Message: errStack }]
  103. });
  104. },
  105. addArrayToText: function (arrayTitle, arrayItems, text) {
  106. if ((arrayItems.Item != undefined) && (arrayItems.Item.length > 0)) {
  107. text += arrayTitle + ":\n";
  108. for (i=0;i<arrayItems.Item.length;i++)
  109. text += " " + arrayItems.Item[i] + "\n";
  110. text += "\n";
  111. }
  112. return text;
  113. },
  114. validateResponseToText: function (response) {
  115. var text = "";
  116. if (!lang.exists("Errors", response) || (response.Errors.length < 1))
  117. text += "No errors found\n";
  118. else
  119. text = this.addArrayToText("Error(s)", response.Errors, text);
  120. if (!lang.exists("Warnings", response) || (response.Warnings.length < 1))
  121. text += "No warnings found\n";
  122. else
  123. text = this.addArrayToText("Warning(s)", response.Warnings, text);
  124. text += "\n";
  125. text = this.addArrayToText("Queries without matching package", response.queries.Unmatched, text);
  126. text = this.addArrayToText("Packages without matching queries", response.packages.Unmatched, text);
  127. text = this.addArrayToText("Files without matching package definitions", response.files.Unmatched, text);
  128. return text;
  129. }
  130. });
  131. });