Browse Source

added eslint and fix on rank

jason.lu 6 years ago
parent
commit
d11dc0211f
5 changed files with 309 additions and 201 deletions
  1. 3 0
      .eslintrc.js
  2. 195 191
      js/newCommon.js
  3. 101 5
      js/newRedrankvue.js
  4. 4 4
      lazhang.html
  5. 6 1
      package.json

+ 3 - 0
.eslintrc.js

@@ -0,0 +1,3 @@
+module.exports = {
+    "extends": "airbnb-base"
+};

+ 195 - 191
js/newCommon.js

@@ -5,232 +5,236 @@ const FAST = 16;
 const PASSIVE = 32;
 
 
-/***
+/** *
  * DataObject DO
  * @param cname
  * @param serverJsonObj
  * @constructor
  */
-var DataObject = function(cname, serverJsonObj){
-    this.ctlName = cname;
-    this.jsonObj = serverJsonObj;
-    this.getVal = function (key) {
-        return jsonObj[key];
-    };
-    this.serVal = function (key, val) {
-        jsonObj[key] = val;
-    };
-
+const DataObject = function (cname, serverJsonObj) {
+  this.ctlName = cname;
+  this.jsonObj = serverJsonObj;
+  this.getVal = function (key) {
+    return this.sonObj[key];
+  };
+  this.serVal = function (key, val) {
+    this.jsonObj[key] = val;
+  };
 };
 
-/***
+/** *
  * DataObjectList
  * @param name
  * @constructor
  */
-var DataObjectList = function(name){
-    this.ctlName = name;
-    this.list = null;
-    this.isSorted = false;
-    this.preSort = [];
-    /***
+const DataObjectList = function (name) {
+  this.ctlName = name;
+  this.list = null;
+  this.isSorted = false;
+  this.preSort = [];
+  this.comparisonBasis = null;
+  /** *
      * sorting
      * @param comparator (a,b)  =====>  a - b
      */
-    this.sortBy = function(comparator){
-        this.preSort = new Array();
-        for (let i = 0; i < this.list.length; i++) this.preSort.push(list[i]);
-        quickSort(this.preSort, 0, this.preSort.length - 1, function(l, r){});
-    };
-
-    this.clearData = function () {
-        this.isSorted = false;
-        this.preSort = null;
-        this.list = null;
+  this.sortBy = function (comparator) {
+    this.comparisonBasis = comparator;
+    this.preSort = [];
+    for (let i = 0; i < this.list.length; i++) {
+      this.preSort.push(this.list[i]);
     }
+    this.quickSort(this.preSort, 0, this.preSort.length - 1, this.comparisonBasis);
+  };
+
+  this.clearData = function () {
+    this.isSorted = false;
+    this.preSort = null;
+    this.list = null;
+  };
+
+  this.len = function () {
+    if (this.list == null) return 0;
+    return this.list.length;
+  };
+
+  this.mergeJsonData = function (data, extdata) {
+    this.list = [];
+    for (let i = 0; i < data.length; i++) this.addObject(data[i]);
+    if (this.comparisonBasis != null) this.sortBy(this.comparisonBasis);
+  };
+
+  this.addObject = function (jsonObj) {
+    this.list.push(new DataObject(this.ctlName, jsonObj));
+  };
+
+  this.assignJsonData = function (data) {
+    this.clearData();
+    this.mergeJsonData(data);
+  };
+  this.getRaw = function (index) {
+    if (this.list == null) return null;
+    return this.list[index];
+  };
+  this.getSorted = function (index) {
+    if (this.list == null) return null;
+    if (!this.isSorted) return this.getRaw(index);
+    return this.preSort[index];
+  };
+  this.quickSort = function (a, left, right, comp) {
+    var inputVar = a;
+    if (left > right) { // 一定要有这个判断,因为有递归left和i-1,若没有这个判断条件,该函数会进入无限死错位递归
+      return;
+    }
+    let i = left;
+    let j = right;
+    const pivot = inputVar[left]; // 基准总是取序列开头的元素
+
+    while (i !== j) {
+      while ((comp(inputVar[j], pivot) > 0) && i < j) { j--; }
+      while ((comp(inputVar[j], pivot) <= 0) && i < j) { i++; }
+      if (i < j) { // 如果i==j跳出外层while
+        const t = inputVar[i];
+        inputVar[i] = inputVar[j];
+        inputVar[j] = t;
+      }
+    }
+
+    inputVar[left] = inputVar[i];// 交换基准数和k位置上的数
+    inputVar[i] = pivot;
 
-    this.len = function() {
-        if(this.list == null) return 0;
-        return this.list.length;
-    };
-
-    this.mergeJsonData = function (data, extdata) {
-        this.list = new Array();
-        for (let i = 0; i < data.length; i++) this.addObject(data[i]);
-    };
-
-    this.addObject = function (jsonObj) {
-        this.list.push(new DataObject(this.ctlName, jsonObj));
-    };
-
-    this.assignJsonData = function (data) {
-        this.clearData();
-        this.mergeJsonData(data);
-    };
-    this.getRaw = function (index) {
-        if(this.list == null) return null;
-        return this.list[index];
-    };
-    this.getSorted = function (index) {
-        if(this.list == null) return null;
-        if(!this.isSorted) return this.getRaw(index);
-        return this.preSort[index];
-    };
-    this.quickSort = function (a,left,right,comp){
-        if(left>right){ //一定要有这个判断,因为有递归left和i-1,若没有这个判断条件,该函数会进入无限死错位递归
-            return;
-        }
-
-        var i=left,
-            j=right,
-            pivot=a[left]; //基准总是取序列开头的元素
-
-        while(i!=j){
-            while((comp(a[j],pivot) > 0)&&i<j){j--}
-            while((comp(a[j],pivot) <= 0)&&i<j){i++}
-            if(i<j){  //如果i==j跳出外层while
-                var t=a[i];
-                a[i]=a[j];
-                a[j]=t;
-            }
-        }
-
-        a[left]=a[i];//交换基准数和k位置上的数
-        a[i]=pivot;
-
-        quicksort(a,left,i-1,comp);
-        quicksort(a,i+1,right,comp);
-    };
+    this.quicksort(inputVar, left, i - 1, comp);
+    this.quicksort(inputVar, i + 1, right, comp);
+  };
 };
 
-/***
+/** *
  * DOService
  * @param controlName
  * @constructor
  */
-var DataObjectService = function(controlName, syncType, extparm){
-    this.ctlName    = controlName;
-    // 2=default 4=on-demand, 8=auto, 16:fast, 32=passive
-    this.syncType   = syncType;
-    this.extParm    = extparm;
-    this.isDefault  =  ((syncType & DEFAULT)  !== 0);
-    this.isOnDemand = ((syncType & ONDEMAND)  !== 0);
-    this.isAuto =     ((syncType & AUTO)  !== 0);
-    this.isFast =     ((syncType & FAST) !== 0);
-    this.isPassive  = ((syncType & PASSIVE) !== 0);
-    this.failedCount= 0;
-    this.list       = new DataObjectList(controlName);
-    this.autoUpdateTimer = 0;
-    this.autoInterval = 1000;
-    this.callerThis = this;
-
-    this.setUpInterval = function() {
-        //URL + exparm
-        if(this.autoUpdateTimer != 0) clearInterval(this.autoUpdateTimer);
-        this.autoUpdateTimer = setInterval(this.selfUpdateTick, this.autoInterval, this);
-        setTimeout(this.selfUpdateTick, 1000, this);
+const DataObjectService = function (controlName, syncType, extparm) {
+  this.ctlName = controlName;
+  // 2=default 4=on-demand, 8=auto, 16:fast, 32=passive
+  this.syncType = syncType;
+  this.extParm = extparm;
+  this.isDefault = ((syncType & DEFAULT) !== 0);
+  this.isOnDemand = ((syncType & ONDEMAND) !== 0);
+  this.isAuto = ((syncType & AUTO) !== 0);
+  this.isFast = ((syncType & FAST) !== 0);
+  this.isPassive = ((syncType & PASSIVE) !== 0);
+  this.failedCount = 0;
+  this.list = new DataObjectList(controlName);
+  this.autoUpdateTimer = 0;
+  this.autoInterval = 1000;
+  this.callerThis = this;
+
+  this.setUpInterval = function () {
+    // URL + exparm
+    if (this.autoUpdateTimer !== 0) clearInterval(this.autoUpdateTimer);
+    this.autoUpdateTimer = setInterval(this.selfUpdateTick, this.autoInterval, this);
+    setTimeout(this.selfUpdateTick, 1000, this);
+  };
+
+  this.init = function () {
+    this.appUrl = `${controlName}/list${extparm}`;
+    if (!this.isFast || this.isOnDemand) {
+      this.autoInterval = 300;
     }
 
-    this.init = function(){
-        this.appUrl = controlName + "/list" + extparm;
-        if(!this.isFast || this.isOnDemand){
-            this.autoInterval = 300;
-        }
-
-        if(this.isFast) {
-            this.appUrl = controlName + "/get" + extparm;
-        }
+    if (this.isFast) {
+      this.appUrl = `${controlName}/get${extparm}`;
+    }
 
-        if(this.isAuto){
-            // timer
-            this.setUpInterval();
-        }
-    };
+    if (this.isAuto) {
+      // timer
+      this.setUpInterval();
+    }
+  };
 
-    this.stopTimers = function () {
-        clearInterval(this.autoUpdateTimer);
-        this.autoUpdateTimer = 0;
-    };
+  this.stopTimers = function () {
+    clearInterval(this.autoUpdateTimer);
+    this.autoUpdateTimer = 0;
+  };
 
-    /***
+  /** *
      * Automatica Update Tick, where obj is a replacement for "this" where "this" might be incorrect
      * @param obj : DataObjectService
      */
-    this.selfUpdateTick = function(obj){
-        get_data(obj.appUrl, obj.onDataReceived, obj);
-    };
-
-    this.onDemandUpdate = function(obj){
-        get_data(obj.appUrl, obj.onDataReceived, obj);
-    };
-
-    this.onDataReceived = function(data, sta){
-        let obj = this.callerThis;
-        if(obj == undefined){console.error("Cannot get callerThis in onDataReceived, please check"); console.error(this)}
-        let output = first_parse(this, data);
-        if (output.list == undefined || output.list.length == 0) {
-            obj.failedCount++;
-            if(obj.failedCount > 100){
-                obj.failedCount = 0;
-                obj.stopTimers();
-                console.log("[排行榜返回为空git clone https://github.com/pentaho/mondrian-tck.git次数过多],暂停刷新");
-            }
-            if(obj.failedCount == 2) {
-                send_alert("排行榜多次返回没有数据,请尝试重新加载! [" + obj.appUrl +"]");
-            }
-        }else{
-            obj.list.assignJsonData(output.list);
-        }
-    };
-
-    this.getList = function () {
-        return this.list;
-    };
-
-    this.passiveUpdate = function(data){
-        this.failedCount = 0;
-        this.list.assignJsonData(data);
-    };
-
-    this.init();
+  this.selfUpdateTick = function (obj) {
+    get_data(obj.appUrl, obj.onDataReceived, obj);
+  };
+
+  this.onDemandUpdate = function (obj) {
+    get_data(obj.appUrl, obj.onDataReceived, obj);
+  };
+
+  this.onDataReceived = function (data, sta) {
+    const obj = this.callerThis;
+    if (obj === undefined) { console.error('Cannot get callerThis in onDataReceived, please check'); console.error(this); }
+    const output = first_parse(this, data);
+    if (output.list === undefined || output.list.length === 0) {
+      obj.failedCount++;
+      if (obj.failedCount > 100) {
+        obj.failedCount = 0;
+        obj.stopTimers();
+        console.log('[排行榜返回为空git clone https://github.com/pentaho/mondrian-tck.git次数过多],暂停刷新');
+      }
+      if (obj.failedCount === 2) {
+        send_alert(`排行榜多次返回没有数据,请尝试重新加载! [${obj.appUrl}]`);
+      }
+    } else {
+      obj.list.assignJsonData(output.list);
+    }
+  };
+
+  this.getList = function () {
+    return this.list;
+  };
+
+  this.passiveUpdate = function (data) {
+    this.failedCount = 0;
+    this.list.assignJsonData(data);
+  };
+
+  this.init();
 };
 
-var SinglePage = function () {
-    this.modules = [];
+const SinglePage = function () {
+  this.modules = [];
 };
 
-var Module = function(chartContainerSelector, dataCtlName){
-    this.selector = chartContainerSelector;
-    this.dataCtl = dataCtlName;
-    this.activeData = null;
-    this.lastActive = 0;
-    this.tickInterval = 1000;
-    /***
+const Module = function (chartContainerSelector, dataCtlName) {
+  this.selector = chartContainerSelector;
+  this.dataCtl = dataCtlName;
+  this.activeData = null;
+  this.lastActive = 0;
+  this.tickInterval = 1000;
+  /** *
      * bindData to UI
      * @param data DataObjectList
      */
-    this.setData = function (data) {
-        if(data.ctlName == this.dataCtl){
-            console.log(`-------- Module ${this.selector} got data ----------- `);
-            this.activeData = data;
-            this.drawData(this.activeData);
-        }else{
-            console.log(`-------- Module ${this.selector} cannot use data of ${data.ctlName} ----------- `);
-        }
-    };
-    this.init = function () {
-        // do init
-        this.setupInterval();
-    };
-    this.tick = function () {
-        // do tick
-    };
-    this.setupInterval = function () {
-        // do tick
-        if(this.tickInterval != 0) clearInterval(this.tickInterval);
-        this.tickInterval = setInterval(this.tick, this.tickInterval);
-    };
-    this.drawData = function () {
-        // do drawData
-    };
+  this.setData = function (data) {
+    if (data.ctlName === this.dataCtl) {
+      console.log(`-------- Module ${this.selector} got data ----------- `);
+      this.activeData = data;
+      this.drawData(this.activeData);
+    } else {
+      console.log(`-------- Module ${this.selector} cannot use data of ${data.ctlName} ----------- `);
+    }
+  };
+  this.init = function () {
+    // do init
+    this.setupInterval();
+  };
+  this.tick = function () {
+    // do tick
+  };
+  this.setupInterval = function () {
+    // do tick
+    if (this.tickInterval !== 0) clearInterval(this.tickInterval);
+    this.tickInterval = setInterval(this.tick, this.tickInterval);
+  };
+  this.drawData = function () {
+    // do drawData
+  };
 };

+ 101 - 5
js/newRedrankvue.js

@@ -26,14 +26,14 @@ RedRankTable.showPart = function(slide, part, data){
     var partList = [];
     for (var i = lpos; i < rpos; i++) {
         var one = data.getSorted(i);
-        if(one == null || one == undefined) {
+        if(one == null || one === undefined) {
             partList[partList.length] = {passNum : 0, failNum: 0, failRate: 0, workerId: 0, fcDay: new Date().toLocaleDateString()};
         } else {
             partList[partList.length] = one.jsonObj;
-            partList[partList.length].failRate = partList[partList.length].failRate/100.0;
+            partList[partList.length - 1].failRate = partList[partList.length - 1].failRate/100.0;
         }
         partList[partList.length - 1].workerName = getUserName(partList[partList.length - 1].workerId);
-        if("[用户:"+0+"]" == partList[partList.length - 1].workerName){
+        if("[用户:"+0+"]" === partList[partList.length - 1].workerName){
             partList[partList.length - 1].workerName = "-";
         }
         partList[partList.length - 1].color = true;
@@ -53,7 +53,7 @@ RedRankTable.showPart = function(slide, part, data){
 RedRankTable.tickInterval = 1000;
 RedRankTable.tick = function(){
     var totalDataLen = this.activeData != null ? this.activeData.len() : 0;
-    if(this.slides != Math.ceil(totalDataLen / this.slideLen)){
+    if(this.slides !== Math.ceil(totalDataLen / this.slideLen)){
         this.slides = Math.ceil(totalDataLen / this.slideLen);
         this.curSlide = 0;
     } else {
@@ -74,6 +74,36 @@ var newRedrankvue = new Vue({
     methods: {
         clicktitle: function () {
             alert("Hello! This is Title");
+        },
+        useId: function () {
+            this.useSort("Id");
+        },
+        usePass: function () {
+            this.useSort("Pass");
+        },
+        useFail: function () {
+            this.useSort("Fail");
+        },
+        useRate: function () {
+            this.useSort("Rate");
+        },
+        useSort: function (column) {
+            let sorter = function () {};
+            switch (column) {
+                case "Pass":
+                    return RedRankTablePlugin.sortPass;
+
+                case "Fail":
+                    return RedRankTablePlugin.sortFail;
+
+                case "Rate":
+                    return RedRankTablePlugin.sortRate;
+
+                case "Id":
+                    return RedRankTablePlugin.sortId;
+            }
+            RedRankTablePlugin.workerDailyService.list.sortBy(sorter);
+            console.warn(column);
         }
     }
 });
@@ -83,7 +113,73 @@ var RedRankTablePlugin = new (function() {
     this.workerDailyService = new DataObjectService("fcBiWorkerDaily", ONDEMAND | AUTO | FAST, "?type=1");
 
     this.tick = function(obj){
-        RedRankTable.setData(obj.workerDailyService.getList());
+        RedRankTable.setData(RedRankTablePlugin.workerDailyService.getList());
+    };
+
+    this.sortPass = function (l, r){
+        var q = {};
+        checkNull(l, r, q);
+        if(q.out !== -2) return q.out;
+        var t = calcCompare(l.jsonObj.passNum, r.jsonObj.passNum, q);
+        return t;
+    };
+
+    this.sortFail = function (l, r){
+        var q = {};
+        checkNull(l, r, q);
+        if(q.out !== -2) return q.out;
+        var t = calcCompare(l.jsonObj.failNum, r.jsonObj.failNum, q);
+        return t;
+    };
+
+    function checkNull(l, r, q) {
+        if (l.jsonObj == null || l.jsonObj === undefined) {
+            if (r.jsonObj == null || r.jsonObj === undefined) {
+                q.out = 0;
+                return;
+            }
+            q.out = -1;
+            return;
+        }
+        if (r.jsonObj == null || r.jsonObj === undefined) {
+            q.out = 1;
+            return;
+        }
+
+        if(r.jsonObj == null || r.jsonObj === undefined){
+            q = 1;
+            return;
+        }
+        q.out = -2;
+    }
+
+    function calcCompare(l, r, q) {
+        if (l > r) {
+            q = 1;
+        } else if (l === r) {
+            q = 0;
+        } else if (l < r) {
+            q = -1;
+        } else {
+            console.error("login error");
+            q = 0;
+        }
+        return q;
+    }
+
+    this.sortRate = function (l, r){
+        var q = {};
+        checkNull(l, r, q);
+        if(q.out !== -2) return q.out;
+        q = calcCompare(l.jsonObj.failRate, r.jsonObj.failRate, q);
+    };
+
+    this.sortId = function (l, r){
+        var q = {};
+        checkNull(l, r, q);
+        if(q.out !== -2) return q.out;
+        var t = calcCompare(l.jsonObj.workerId, r.jsonObj.workerId, q);
+        return t;
     };
 
     this.setup = function(obj){

+ 4 - 4
lazhang.html

@@ -158,10 +158,10 @@
                     <table class="table table-bordered text-center table-striped">
                         <thead>
                         <tr>
-                            <th>人员</th>
-                            <th>良品</th>
-                            <th>坏品</th>
-                            <th>不良率</th>
+                            <th @click="useId">人员</th>
+                            <th @click="usePass">良品</th>
+                            <th @click="useFail">坏品</th>
+                            <th @click="useRate">不良率</th>
                         </tr>
                         </thead>
                         <tbody class="table-rank-0">

+ 6 - 1
package.json

@@ -13,5 +13,10 @@
   "bugs": {
     "url": "https://github.com/gurayyarar/AdminBSBMaterialDesign/issues"
   },
-  "homepage": "https://github.com/gurayyarar/AdminBSBMaterialDesign#readme"
+  "homepage": "https://github.com/gurayyarar/AdminBSBMaterialDesign#readme",
+  "devDependencies": {
+    "eslint": "^5.10.0",
+    "eslint-config-airbnb-base": "^13.1.0",
+    "eslint-plugin-import": "^2.14.0"
+  }
 }