newCommon.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. const DEFAULT = 2;
  2. const ONDEMAND = 4;
  3. const AUTO = 8;
  4. const FAST = 16;
  5. const PASSIVE = 32;
  6. /***
  7. * DataObject DO
  8. * @param cname
  9. * @param serverJsonObj
  10. * @constructor
  11. */
  12. var DataObject = function(cname, serverJsonObj){
  13. this.ctlName = cname;
  14. this.jsonObj = serverJsonObj;
  15. this.getVal = function (key) {
  16. return jsonObj[key];
  17. };
  18. this.serVal = function (key, val) {
  19. jsonObj[key] = val;
  20. };
  21. };
  22. /***
  23. * DataObjectList
  24. * @param name
  25. * @constructor
  26. */
  27. var DataObjectList = function(name){
  28. this.ctlName = name;
  29. this.list = null;
  30. this.isSorted = false;
  31. this.preSort = [];
  32. /***
  33. * sorting
  34. * @param comparator (a,b) =====> a - b
  35. */
  36. this.sortBy = function(comparator){
  37. this.preSort = new Array();
  38. for (let i = 0; i < this.list.length; i++) this.preSort.push(list[i]);
  39. quickSort(this.preSort, 0, this.preSort.length - 1, function(l, r){});
  40. };
  41. this.clearData = function () {
  42. this.isSorted = false;
  43. this.preSort = null;
  44. this.list = null;
  45. }
  46. this.len = function() {
  47. if(this.list == null) return 0;
  48. return this.list.length;
  49. };
  50. this.mergeJsonData = function (data, extdata) {
  51. this.list = new Array();
  52. for (let i = 0; i < data.length; i++) this.addObject(data[i]);
  53. };
  54. this.addObject = function (jsonObj) {
  55. this.list.push(new DataObject(this.ctlName, jsonObj));
  56. };
  57. this.assignJsonData = function (data) {
  58. this.clearData();
  59. this.mergeJsonData(data);
  60. };
  61. this.getRaw = function (index) {
  62. if(this.list == null) return null;
  63. return this.list[index];
  64. };
  65. this.getSorted = function (index) {
  66. if(this.list == null) return null;
  67. if(!this.isSorted) return this.getRaw(index);
  68. return this.preSort[index];
  69. };
  70. this.quickSort = function (a,left,right,comp){
  71. if(left>right){ //一定要有这个判断,因为有递归left和i-1,若没有这个判断条件,该函数会进入无限死错位递归
  72. return;
  73. }
  74. var i=left,
  75. j=right,
  76. pivot=a[left]; //基准总是取序列开头的元素
  77. while(i!=j){
  78. while((comp(a[j],pivot) > 0)&&i<j){j--}
  79. while((comp(a[j],pivot) <= 0)&&i<j){i++}
  80. if(i<j){ //如果i==j跳出外层while
  81. var t=a[i];
  82. a[i]=a[j];
  83. a[j]=t;
  84. }
  85. }
  86. a[left]=a[i];//交换基准数和k位置上的数
  87. a[i]=pivot;
  88. quicksort(a,left,i-1,comp);
  89. quicksort(a,i+1,right,comp);
  90. };
  91. };
  92. /***
  93. * DOService
  94. * @param controlName
  95. * @constructor
  96. */
  97. var DataObjectService = function(controlName, syncType, extparm){
  98. this.ctlName = controlName;
  99. // 2=default 4=on-demand, 8=auto, 16:fast, 32=passive
  100. this.syncType = syncType;
  101. this.extParm = extparm;
  102. this.isDefault = ((syncType & DEFAULT) !== 0);
  103. this.isOnDemand = ((syncType & ONDEMAND) !== 0);
  104. this.isAuto = ((syncType & AUTO) !== 0);
  105. this.isFast = ((syncType & FAST) !== 0);
  106. this.isPassive = ((syncType & PASSIVE) !== 0);
  107. this.failedCount= 0;
  108. this.list = new DataObjectList(controlName);
  109. this.autoUpdateTimer = 0;
  110. this.autoInterval = 1000;
  111. this.callerThis = this;
  112. this.setUpInterval = function() {
  113. //URL + exparm
  114. if(this.autoUpdateTimer != 0) clearInterval(this.autoUpdateTimer);
  115. this.autoUpdateTimer = setInterval(this.selfUpdateTick, this.autoInterval, this);
  116. setTimeout(this.selfUpdateTick, 1000, this);
  117. }
  118. this.init = function(){
  119. this.appUrl = controlName + "/list" + extparm;
  120. if(!this.isFast || this.isOnDemand){
  121. this.autoInterval = 300;
  122. }
  123. if(this.isFast) {
  124. this.appUrl = controlName + "/get" + extparm;
  125. }
  126. if(this.isAuto){
  127. // timer
  128. this.setUpInterval();
  129. }
  130. };
  131. this.stopTimers = function () {
  132. clearInterval(this.autoUpdateTimer);
  133. this.autoUpdateTimer = 0;
  134. };
  135. /***
  136. * Automatica Update Tick, where obj is a replacement for "this" where "this" might be incorrect
  137. * @param obj : DataObjectService
  138. */
  139. this.selfUpdateTick = function(obj){
  140. get_data(obj.appUrl, obj.onDataReceived, obj);
  141. };
  142. this.onDemandUpdate = function(obj){
  143. get_data(obj.appUrl, obj.onDataReceived, obj);
  144. };
  145. this.onDataReceived = function(data, sta){
  146. let obj = this.callerThis;
  147. if(obj == undefined){console.error("Cannot get callerThis in onDataReceived, please check"); console.error(this)}
  148. let output = first_parse(this, data);
  149. if (output.list == undefined || output.list.length == 0) {
  150. obj.failedCount++;
  151. if(obj.failedCount > 100){
  152. obj.failedCount = 0;
  153. obj.stopTimers();
  154. console.log("[排行榜返回为空git clone https://github.com/pentaho/mondrian-tck.git次数过多],暂停刷新");
  155. }
  156. if(obj.failedCount == 2) {
  157. send_alert("排行榜多次返回没有数据,请尝试重新加载! [" + obj.appUrl +"]");
  158. }
  159. }else{
  160. obj.list.assignJsonData(output.list);
  161. }
  162. };
  163. this.getList = function () {
  164. return this.list;
  165. };
  166. this.passiveUpdate = function(data){
  167. this.failedCount = 0;
  168. this.list.assignJsonData(data);
  169. };
  170. this.init();
  171. };
  172. var SinglePage = function () {
  173. this.modules = [];
  174. };
  175. var Module = function(chartContainerSelector, dataCtlName){
  176. this.selector = chartContainerSelector;
  177. this.dataCtl = dataCtlName;
  178. this.activeData = null;
  179. this.lastActive = 0;
  180. this.tickInterval = 1000;
  181. /***
  182. * bindData to UI
  183. * @param data DataObjectList
  184. */
  185. this.setData = function (data) {
  186. if(data.ctlName == this.dataCtl){
  187. console.log(`-------- Module ${this.selector} got data ----------- `);
  188. this.activeData = data;
  189. this.drawData(this.activeData);
  190. }else{
  191. console.log(`-------- Module ${this.selector} cannot use data of ${data.ctlName} ----------- `);
  192. }
  193. };
  194. this.init = function () {
  195. // do init
  196. this.setupInterval();
  197. };
  198. this.tick = function () {
  199. // do tick
  200. };
  201. this.setupInterval = function () {
  202. // do tick
  203. if(this.tickInterval != 0) clearInterval(this.tickInterval);
  204. this.tickInterval = setInterval(this.tick, this.tickInterval);
  205. };
  206. this.drawData = function () {
  207. // do drawData
  208. };
  209. };