ESPUtil.js 7.0 KB

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