PackageSourceWidget.js 6.0 KB

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