HexViewWidget.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. define([
  18. "dojo/_base/declare",
  19. "dojo/_base/lang",
  20. "dojo/i18n",
  21. "dojo/i18n!./nls/common",
  22. "dojo/i18n!./nls/HexViewWidget",
  23. "dojo/_base/array",
  24. "dojo/store/Memory",
  25. "dojo/store/Observable",
  26. "dojo/request/iframe",
  27. "dijit/registry",
  28. "hpcc/_Widget",
  29. "hpcc/ESPWorkunit",
  30. "hpcc/ECLSourceWidget",
  31. "dojo/text!../templates/HexViewWidget.html",
  32. "dijit/form/NumberSpinner",
  33. "dijit/form/CheckBox"
  34. ],
  35. function (declare, lang, i18n, nlsCommon, nlsSpecific, arrayUtil, Memory, Observable, iframe,
  36. registry,
  37. _Widget, ESPWorkunit, ECLSourceWidget,
  38. template) {
  39. return declare("HexViewWidget", [_Widget], {
  40. templateString: template,
  41. baseClass: "HexViewWidget",
  42. i18n: lang.mixin(nlsCommon, nlsSpecific),
  43. borderContainer: null,
  44. widthField: null,
  45. hexView: null,
  46. wu: null,
  47. unknownChar: String.fromCharCode(8226),
  48. lineLength: 16,
  49. showEbcdic: false,
  50. bufferLength: 16 * 1024,
  51. buildRendering: function (args) {
  52. this.inherited(arguments);
  53. },
  54. postCreate: function (args) {
  55. this.inherited(arguments);
  56. this.borderContainer = registry.byId(this.id + "BorderContainer");
  57. this.widthField = registry.byId(this.id + "Width");
  58. this.hexView = registry.byId(this.id + "HexView");
  59. },
  60. startup: function (args) {
  61. this.inherited(arguments);
  62. },
  63. resize: function (args) {
  64. this.inherited(arguments);
  65. this.borderContainer.resize();
  66. },
  67. layout: function (args) {
  68. this.inherited(arguments);
  69. },
  70. _onWidthChange: function (event) {
  71. if (this.lineLength != event) {
  72. this.lineLength = event;
  73. this.displayHex();
  74. }
  75. },
  76. _onEbcdicChange: function (event) {
  77. if (this.showEbcdic != event) {
  78. this.showEbcdic = event;
  79. this.displayHex();
  80. }
  81. },
  82. init: function (params) {
  83. if (this.inherited(arguments))
  84. return;
  85. this.logicalFile = params.logicalFile;
  86. this.hexView.init({
  87. sourceMode: ""
  88. });
  89. var context = this;
  90. this.wu = ESPWorkunit.Create({
  91. onCreate: function () {
  92. context.wu.update({
  93. QueryText: context.getQuery()
  94. });
  95. context.watchWU();
  96. },
  97. onUpdate: function () {
  98. context.wu.submit();
  99. },
  100. onSubmit: function () {
  101. }
  102. });
  103. },
  104. watchWU: function () {
  105. if (this.watching) {
  106. this.watching.unwatch();
  107. }
  108. var context = this;
  109. this.watching = this.wu.watch(function (name, oldValue, newValue) {
  110. switch (name) {
  111. case "hasCompleted":
  112. if (newValue === true) {
  113. context.wu.fetchResults(function (results) {
  114. context.cachedResponse = "";
  115. arrayUtil.forEach(results, function (result, idx) {
  116. var store = result.getStore();
  117. var result = store.query({
  118. }, {
  119. start: 0,
  120. count: context.bufferLength
  121. }).then(function (response) {
  122. context.cachedResponse = response;
  123. context.displayHex();
  124. context.wu.doDelete();
  125. });
  126. });
  127. });
  128. }
  129. break;
  130. case "State":
  131. context.hexView.setText("..." + (context.wu.isComplete() ? context.i18n.fetchingresults : newValue) + "...");
  132. break;
  133. }
  134. });
  135. },
  136. isCharCodePrintable: function (charCode) {
  137. if (charCode < 32)
  138. return false;
  139. else if (charCode >= 127 && charCode <= 159)
  140. return false;
  141. else if (charCode === 173)
  142. return false;
  143. else if (charCode > 255)
  144. return false;
  145. return true;
  146. },
  147. isCharPrintable: function (char) {
  148. return this.isCharCodePrintable(char.charCodeAt(0));
  149. },
  150. displayHex: function() {
  151. var context = this;
  152. var formatRow = function (row, strRow, hexRow, length) {
  153. if (row) {
  154. for (var i = row.length; i < 4; ++i) {
  155. row = "0" + row;
  156. }
  157. for (var i = strRow.length; i < length; ++i) {
  158. strRow += context.unknownChar;
  159. }
  160. return row + " " + strRow + " " + hexRow + "\n";
  161. }
  162. return "";
  163. };
  164. var doc = "";
  165. var row = "";
  166. var hexRow = "";
  167. var strRow = "";
  168. var charIdx = 0;
  169. arrayUtil.some(this.cachedResponse, function (item, idx) {
  170. if (idx >= context.lineLength * 100) {
  171. return false;
  172. }
  173. if (idx % context.lineLength === 0) {
  174. doc += formatRow(row, strRow, hexRow, context.lineLength);
  175. row = "";
  176. hexRow = "";
  177. strRow = "";
  178. charIdx = 0;
  179. row = idx.toString(16);
  180. }
  181. if (charIdx % 8 === 0) {
  182. if (hexRow)
  183. hexRow += " ";
  184. }
  185. if (hexRow)
  186. hexRow += " ";
  187. hexRow += item.char;
  188. if (context.showEbcdic) {
  189. strRow += context.isCharPrintable(item.estr1) ? item.estr1 : context.unknownChar;
  190. } else {
  191. strRow += context.isCharPrintable(item.str1) ? item.str1 : context.unknownChar;
  192. }
  193. ++charIdx;
  194. });
  195. doc += formatRow(row, strRow, hexRow, context.lineLength);
  196. this.hexView.setText(doc);
  197. },
  198. getQuery: function () {
  199. return "data_layout := record\n" +
  200. " data1 char;\n" +
  201. "end;\n" +
  202. "data_dataset := dataset('" + this.logicalFile + "', data_layout, thor);\n" +
  203. "analysis_layout := record\n" +
  204. " data1 char;\n" +
  205. " string1 str1;\n" +
  206. " ebcdic string1 estr1;\n" +
  207. "end;\n" +
  208. "analysis_layout calcAnalysis(data_layout l) := transform\n" +
  209. " self.char := l.char;\n" +
  210. " self.str1 := transfer(l.char, string1);\n" +
  211. " self.estr1 := transfer(l.char, string1);\n" +
  212. "end;\n" +
  213. "analysis_dataset := project(data_dataset, calcAnalysis(left));\n" +
  214. "choosen(analysis_dataset, " + this.bufferLength + ");\n"
  215. }
  216. });
  217. });