HexViewWidget.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/array",
  20. "dojo/_base/lang",
  21. "dojo/store/Memory",
  22. "dojo/store/Observable",
  23. "dojo/request/iframe",
  24. "dijit/registry",
  25. "hpcc/_Widget",
  26. "hpcc/ESPWorkunit",
  27. "hpcc/ECLSourceWidget",
  28. "dojo/text!../templates/HexViewWidget.html",
  29. "dijit/form/NumberSpinner",
  30. "dijit/form/CheckBox"
  31. ],
  32. function (declare, arrayUtil, lang, Memory, Observable, iframe,
  33. registry,
  34. _Widget, ESPWorkunit, ECLSourceWidget,
  35. template) {
  36. return declare("HexViewWidget", [_Widget], {
  37. templateString: template,
  38. baseClass: "HexViewWidget",
  39. borderContainer: null,
  40. widthField: null,
  41. hexView: null,
  42. wu: null,
  43. unknownChar: String.fromCharCode(8226),
  44. lineLength: 16,
  45. showEbcdic: false,
  46. bufferLength: 16 * 1024,
  47. buildRendering: function (args) {
  48. this.inherited(arguments);
  49. },
  50. postCreate: function (args) {
  51. this.inherited(arguments);
  52. this.borderContainer = registry.byId(this.id + "BorderContainer");
  53. this.widthField = registry.byId(this.id + "Width");
  54. this.hexView = registry.byId(this.id + "HexView");
  55. },
  56. startup: function (args) {
  57. this.inherited(arguments);
  58. },
  59. resize: function (args) {
  60. this.inherited(arguments);
  61. this.borderContainer.resize();
  62. },
  63. layout: function (args) {
  64. this.inherited(arguments);
  65. },
  66. _onWidthChange: function (event) {
  67. if (this.lineLength != event) {
  68. this.lineLength = event;
  69. this.displayHex();
  70. }
  71. },
  72. _onEbcdicChange: function (event) {
  73. if (this.showEbcdic != event) {
  74. this.showEbcdic = event;
  75. this.displayHex();
  76. }
  77. },
  78. init: function (params) {
  79. if (this.inherited(arguments))
  80. return;
  81. this.logicalFile = params.logicalFile;
  82. this.hexView.init({
  83. sourceMode: ""
  84. });
  85. var context = this;
  86. this.wu = ESPWorkunit.Create({
  87. onCreate: function () {
  88. context.wu.update({
  89. QueryText: context.getQuery()
  90. });
  91. context.watchWU();
  92. },
  93. onUpdate: function () {
  94. context.wu.submit();
  95. },
  96. onSubmit: function () {
  97. }
  98. });
  99. },
  100. watchWU: function () {
  101. if (this.watching) {
  102. this.watching.unwatch();
  103. }
  104. var context = this;
  105. this.watching = this.wu.watch(function (name, oldValue, newValue) {
  106. switch (name) {
  107. case "hasCompleted":
  108. if (newValue === true) {
  109. context.wu.fetchResults(function (results) {
  110. context.cachedResponse = "";
  111. arrayUtil.forEach(results, function (result, idx) {
  112. var store = result.getStore();
  113. var result = store.query({
  114. }, {
  115. start: 0,
  116. count: context.bufferLength
  117. }).then(function (response) {
  118. context.cachedResponse = response;
  119. context.displayHex();
  120. context.wu.doDelete();
  121. });
  122. });
  123. });
  124. }
  125. break;
  126. case "State":
  127. context.hexView.setText("..." + (context.wu.isComplete() ? "fetching results" : newValue) + "...");
  128. break;
  129. }
  130. });
  131. },
  132. isCharCodePrintable: function (charCode) {
  133. if (charCode < 32)
  134. return false;
  135. else if (charCode >= 127 && charCode <= 159)
  136. return false;
  137. else if (charCode === 173)
  138. return false;
  139. else if (charCode > 255)
  140. return false;
  141. return true;
  142. },
  143. isCharPrintable: function (char) {
  144. return this.isCharCodePrintable(char.charCodeAt(0));
  145. },
  146. displayHex: function() {
  147. var context = this;
  148. var formatRow = function (row, strRow, hexRow, length) {
  149. if (row) {
  150. for (var i = row.length; i < 4; ++i) {
  151. row = "0" + row;
  152. }
  153. for (var i = strRow.length; i < length; ++i) {
  154. strRow += context.unknownChar;
  155. }
  156. return row + " " + strRow + " " + hexRow + "\n";
  157. }
  158. return "";
  159. };
  160. var doc = "";
  161. var row = "";
  162. var hexRow = "";
  163. var strRow = "";
  164. var charIdx = 0;
  165. arrayUtil.some(this.cachedResponse, function (item, idx) {
  166. if (idx >= context.lineLength * 100) {
  167. return false;
  168. }
  169. if (idx % context.lineLength === 0) {
  170. doc += formatRow(row, strRow, hexRow, context.lineLength);
  171. row = "";
  172. hexRow = "";
  173. strRow = "";
  174. charIdx = 0;
  175. row = idx.toString(16);
  176. }
  177. if (charIdx % 8 === 0) {
  178. if (hexRow)
  179. hexRow += " ";
  180. }
  181. if (hexRow)
  182. hexRow += " ";
  183. hexRow += item.char;
  184. if (context.showEbcdic) {
  185. strRow += context.isCharPrintable(item.estr1) ? item.estr1 : context.unknownChar;
  186. } else {
  187. strRow += context.isCharPrintable(item.str1) ? item.str1 : context.unknownChar;
  188. }
  189. ++charIdx;
  190. });
  191. doc += formatRow(row, strRow, hexRow, context.lineLength);
  192. this.hexView.setText(doc);
  193. },
  194. getQuery: function () {
  195. return "data_layout := record\n" +
  196. " data1 char;\n" +
  197. "end;\n" +
  198. "data_dataset := dataset('" + this.logicalFile + "', data_layout, thor);\n" +
  199. "analysis_layout := record\n" +
  200. " data1 char;\n" +
  201. " string1 str1;\n" +
  202. " ebcdic string1 estr1;\n" +
  203. "end;\n" +
  204. "analysis_layout calcAnalysis(data_layout l) := transform\n" +
  205. " self.char := l.char;\n" +
  206. " self.str1 := transfer(l.char, string1);\n" +
  207. " self.estr1 := transfer(l.char, string1);\n" +
  208. "end;\n" +
  209. "analysis_dataset := project(data_dataset, calcAnalysis(left));\n" +
  210. "choosen(analysis_dataset, " + this.bufferLength + ");\n"
  211. }
  212. });
  213. });