HexViewWidget.js 9.1 KB

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