ESPUtil.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 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/array",
  19. "dojo/Stateful",
  20. "dijit/registry",
  21. ], function (declare, arrayUtil, Stateful,
  22. registry) {
  23. var SingletonData = declare([Stateful], {
  24. // Attributes ---
  25. _hasCompletedSetter: function (hasCompleted) {
  26. if (this.hasCompleted !== hasCompleted) {
  27. this.hasCompleted = hasCompleted;
  28. if (!this.hasCompleted) {
  29. if (this.startMonitor) {
  30. this.startMonitor();
  31. }
  32. } else {
  33. if (this.stopMonitor) {
  34. this.stopMonitor();
  35. }
  36. }
  37. }
  38. },
  39. // Methods ---
  40. constructor: function (args) {
  41. this.changedCount = 0;
  42. this._changedCache = {};
  43. },
  44. getData: function () {
  45. if (this instanceof SingletonData) {
  46. return (SingletonData)(this);
  47. }
  48. return this;
  49. },
  50. updateData: function (response) {
  51. var changed = false;
  52. for (var key in response) {
  53. var json = dojo.toJson(response[key]);
  54. if (this._changedCache[key] !== json) {
  55. this._changedCache[key] = json;
  56. this.set(key, response[key]);
  57. changed = true;
  58. }
  59. }
  60. if (changed) {
  61. this.set("changedCount", this.get("changedCount") + 1);
  62. }
  63. }
  64. });
  65. var Monitor = declare(null, {
  66. isMonitoring: function () {
  67. return this._timer && this._timer > 0;
  68. },
  69. startMonitor: function (aggressive) {
  70. if (this.isMonitoring())
  71. return;
  72. this._timerTickCount = aggressive ? 0 : Math.floor((Math.random() * 40) + 70);
  73. this._timer = 1000;
  74. this.onMonitor();
  75. },
  76. stopMonitor: function () {
  77. this._timerTickCount = 0;
  78. this._timer = 0;
  79. },
  80. onMonitor: function () {
  81. this._timerTickCount++;
  82. if (this.hasCompleted) {
  83. this.stopMonitor();
  84. return;
  85. } else {
  86. if (this._timerTickCount === 1) {
  87. this.refresh(true);
  88. } else if (this._timerTickCount < 5 && this._timerTickCount % 1 === 0) {
  89. this.refresh();
  90. } else if (this._timerTickCount < 30 && this._timerTickCount % 5 === 0) {
  91. this.refresh();
  92. } else if (this._timerTickCount < 60 && this._timerTickCount % 10 === 0) {
  93. this.refresh();
  94. } else if (this._timerTickCount < 120 && this._timerTickCount % 30 === 0) {
  95. this.refresh(true);
  96. } else if (this._timerTickCount % 60 === 0) {
  97. this.refresh(true);
  98. }
  99. }
  100. var context = this;
  101. if (this._timer) {
  102. setTimeout(function () {
  103. context.onMonitor();
  104. }, this._timer);
  105. } else {
  106. this._timerTickCount = 0;
  107. }
  108. }
  109. });
  110. return {
  111. Singleton: SingletonData,
  112. Monitor: Monitor,
  113. FormHelper: declare(null, {
  114. getISOString: function (dateField, timeField) {
  115. var d = registry.byId(this.id + dateField).attr("value");
  116. var t = registry.byId(this.id + timeField).attr("value");
  117. if (d) {
  118. if (t) {
  119. d.setHours(t.getHours());
  120. d.setMinutes(t.getMinutes());
  121. d.setSeconds(t.getSeconds());
  122. }
  123. return d.toISOString();
  124. }
  125. return "";
  126. }
  127. }),
  128. GridHelper: declare(null, {
  129. workunitsGridObserver: [],
  130. onSelectionChanged: function (callback) {
  131. this.on("dgrid-select", function (event) {
  132. callback(event);
  133. });
  134. this.on("dgrid-deselect", function (event) {
  135. callback(event);
  136. });
  137. },
  138. onContentChanged: function (callback) {
  139. var context = this;
  140. this.on("dgrid-page-complete", function (event) {
  141. callback();
  142. if (context.workunitsGridObserver[event.page]) {
  143. context.workunitsGridObserver[event.page].cancel();
  144. }
  145. context.workunitsGridObserver[event.page] = event.results.observe(function (object, removedFrom, insertedInto) {
  146. callback(object, removedFrom, insertedInto);
  147. }, true);
  148. });
  149. this.on("dgrid-children-complete", function (event) {
  150. callback();
  151. });
  152. },
  153. setSelection: function (arrayOfIDs) {
  154. this.clearSelection();
  155. var context = this;
  156. arrayUtil.forEach(arrayOfIDs, function (item, idx) {
  157. if (idx === 0) {
  158. var row = context.row(item);
  159. if (row.element) {
  160. row.element.scrollIntoView();
  161. }
  162. }
  163. context.select(item);
  164. });
  165. },
  166. setSelected: function (items) {
  167. this.clearSelection();
  168. var context = this;
  169. arrayUtil.forEach(items, function (item, idx) {
  170. if (idx === 0) {
  171. var row = context.row(item);
  172. if (row.element) {
  173. row.element.scrollIntoView();
  174. }
  175. }
  176. context.select(context.store.getIdentity(item));
  177. });
  178. },
  179. getSelected: function() {
  180. var retVal = [];
  181. for (var id in this.selection) {
  182. retVal.push(this.store.get(id));
  183. }
  184. return retVal;
  185. }
  186. })
  187. };
  188. });