PackageSourceWidget.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/i18n",
  20. "dojo/i18n!./nls/hpcc",
  21. "dojo/dom",
  22. "dijit/layout/_LayoutWidget",
  23. "dijit/_TemplatedMixin",
  24. "dijit/_WidgetsInTemplateMixin",
  25. "dijit/layout/BorderContainer",
  26. "dijit/layout/ContentPane",
  27. "dijit/registry",
  28. "hpcc/WsPackageMaps",
  29. "dojo/text!../templates/PackageSourceWidget.html"
  30. ],
  31. function (declare, lang, i18n, nlsHPCC, dom,
  32. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin,
  33. BorderContainer, ContentPane, registry,
  34. WsPackageMaps, template) {
  35. return declare("PackageSourceWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  36. templateString: template,
  37. baseClass: "PackageSourceWidget",
  38. i18n: nlsHPCC,
  39. borderContainer: null,
  40. editor: null,
  41. readOnly: true,
  42. buildRendering: function (args) {
  43. this.inherited(arguments);
  44. },
  45. postCreate: function (args) {
  46. this.inherited(arguments);
  47. this.borderContainer = registry.byId(this.id + "BorderContainer");
  48. },
  49. startup: function (args) {
  50. this.inherited(arguments);
  51. },
  52. resize: function (args) {
  53. this.inherited(arguments);
  54. this.borderContainer.resize();
  55. },
  56. layout: function (args) {
  57. this.inherited(arguments);
  58. },
  59. init: function (params) {
  60. if (this.initalized)
  61. return;
  62. this.initalized = true;
  63. this.editor = CodeMirror.fromTextArea(document.getElementById(this.id + "XMLCode"), {
  64. tabMode: "indent",
  65. matchBrackets: true,
  66. gutter: true,
  67. lineNumbers: true,
  68. mode: this.isXmlContent ? "xml" : "ecl",
  69. readOnly: this.readOnly,
  70. gutter: this.isXmlContent ? true : false,
  71. onGutterClick: CodeMirror.newFoldFunction(CodeMirror.tagRangeFinder)
  72. });
  73. dom.byId(this.id + "XMLContent").style.backgroundColor = this.readOnly ? 0xd0d0d0 : 0xffffff;
  74. var context = this;
  75. if (this.isXmlContent) {
  76. WsPackageMaps.getPackageMapById(params, {
  77. load: function (response) {
  78. context.editor.setValue(response);
  79. },
  80. error: function (errMsg, errStack) {
  81. context.showErrors(errMsg, errStack);
  82. }
  83. });
  84. }
  85. else {
  86. WsPackageMaps.validatePackage(params, {
  87. load: function (response) {
  88. var responseText = context.validateResponseToText(response);
  89. //console.log(responseText);
  90. if (responseText == '')
  91. context.editor.setValue("(Empty)");
  92. else
  93. context.editor.setValue(responseText);
  94. },
  95. error: function (errMsg, errStack) {
  96. context.showErrors(errMsg, errStack);
  97. }
  98. });
  99. }
  100. },
  101. showErrors: function (errMsg, errStack) {
  102. dojo.publish("hpcc/brToaster", {
  103. Severity: "Error",
  104. Source: errMsg,
  105. Exceptions: [{ Message: errStack }]
  106. });
  107. },
  108. addArrayToText: function (arrayTitle, arrayItems, text) {
  109. if ((arrayItems.Item != undefined) && (arrayItems.Item.length > 0)) {
  110. text += arrayTitle + ":\n";
  111. for (i=0;i<arrayItems.Item.length;i++)
  112. text += " " + arrayItems.Item[i] + "\n";
  113. text += "\n";
  114. }
  115. return text;
  116. },
  117. validateResponseToText: function (response) {
  118. var text = "";
  119. if (!lang.exists("Errors", response) || (response.Errors.length < 1))
  120. text += this.i18n.NoErrorFound;
  121. else
  122. text = this.addArrayToText(this.i18n.Errors, response.Errors, text);
  123. if (!lang.exists("Warnings", response) || (response.Warnings.length < 1))
  124. text += this.i18n.NoWarningFound;
  125. else
  126. text = this.addArrayToText(this.i18n.Warnings, response.Warnings, text);
  127. text += "\n";
  128. text = this.addArrayToText(this.i18n.QueriesNoPackage, response.queries.Unmatched, text);
  129. text = this.addArrayToText(this.i18n.PackagesNoQuery, response.packages.Unmatched, text);
  130. text = this.addArrayToText(this.i18n.FilesNoPackage, response.files.Unmatched, text);
  131. return text;
  132. }
  133. });
  134. });