-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
1 lines (1 loc) · 5.25 KB
/
app.js
1
"use strict";function onHashChange(){var a=window.location.hash.replace(/#\/?/,"");filters[a]?app.visibility=a:(window.location.hash="",app.visibility="all")}var Todo,filters,date,app;!function(){var c,a=window.location.search.substring(1),b={appId:"v00HoCy8wHryyOzGnUblUfgU-gzGzoHsz",appKey:"DLIavdnvjhhDRsPLnogSFvmj"};a&&(c=a.split("||"),b.appId=c[0],b.appKey=c[1]),AV.init(b)}(),Todo=AV.Object.extend("Todo"),filters={all:function(a){return a}},date=new Date,app=new Vue({data:{records:[],config:{type:"line",data:{},options:{responsive:!0,title:{display:!0,text:"加载中…"},animation:{duration:777},tooltips:{mode:"index",intersect:!1},hover:{mode:"nearest",intersect:!0},scales:{xAxes:[{display:!0,scaleLabel:{display:!0,labelString:"检测时间"}}],yAxes:[{display:!0,scaleLabel:{display:!0,labelString:"耗时(毫秒)"},ticks:{beginAtZero:!0}}],elements:{line:{tension:0}}},pan:{enabled:!1,mode:"xy"},zoom:{enabled:!0,mode:"y",rangeMin:{x:null,y:0},rangeMax:{x:null,y:2}}}},newTodo:"",editedTodo:null,visibility:"all",username:"",password:"",user:null,debuging:!!localStorage.getItem("debug"),cloudFunc:"",params:"",result:"",firstView:!1,startDate:date.getFullYear()+"-"+(1+date.getMonth())+"-"+date.getDate(),endDate:date.getFullYear()+"-"+(1+date.getMonth())+"-"+(1+date.getDate()),tomorrowDate:date.getFullYear()+"-"+(1+date.getMonth())+"-"+(1+date.getDate()),isLoading:!0,URL:"https://b.pingan.com.cn/static/act/koudai/qr_poster/index.shtml?debug=1"},created:function(){var a=AV.User.current();a&&(this.user=a.toJSON())},mounted:function(){this.isLoading=!1,console.log("mounted")},watch:{"user.objectId":{handler:function(a){a?this.fetchRecords(a):this.records=[]}},visitTime:function(){this.drawChart()},debuging:function(a){a?localStorage.setItem("debug","leancloud*"):localStorage.removeItem("debug"),location.reload()},startDate:function(){this.fetchRecords()},endDate:function(){this.fetchRecords()},URL:function(){this.fetchRecords()}},computed:{visitTime:function(){return this.firstView?"firstView":"repeatView"}},filters:{pluralize:function(a){return 1===a?"item":"items"}},methods:{resetZoom:function(){window.myChartInstance.resetZoom()},calcAverage:function(a){var b=a.reduce(function(a,b){return b+=a});return b/a.length},fetchRecords:function(){var b,c;return this.isLoading=!0,b=new AV.Query("Report"),this.URL.indexOf("qr_poster")>-1?(c=new AV.Query("Report"),b=AV.Query.or(b.equalTo("URL",this.URL),c.equalTo("URL",null))):b.equalTo("URL",this.URL),b.greaterThan("createdAt",new Date(this.startDate)).lessThan("createdAt",new Date(this.endDate)).descending("createdAt").limit(999),b.find().then(this.drawChart).catch(alert)},drawChart:function(a){var c,d,b=this;console.log("drawChart: ",a),this.isLoading=!1,c={labels:[],datasets:[{label:"domContentLoadedEventEnd",backgroundColor:"rgb(54, 162, 235)",borderColor:"rgb(54, 162, 235)",data:[],fill:!1},{label:"loadTime",backgroundColor:"rgb(255, 159, 64)",borderColor:"rgb(255, 159, 64)",data:[],fill:!1}]},a&&(this.records=a),this.records.map(function(a){a=a.toJSON(),c.labels.unshift(new Date(a.createdAt).toLocaleDateString()+new Date(a.createdAt).toLocaleTimeString()),c.datasets[0].data.unshift(a.average[b.visitTime].domContentLoadedEventEnd),c.datasets[1].data.unshift(a.average[b.visitTime].loadTime)}),this.config.data=c,this.config.options.title.text=this.firstView?"首次访问":"二次访问",this.config.options.title.text=this.firstView?"首次访问":"二次访问",window.myChartInstance?(window.myChartInstance.update(),console.log("myChartInstance.update called")):(d=document.getElementById("myChart"),window.myChartInstance=new Chart(d,this.config),console.log("new Chart(ctx, this.config) called"))},login:function(){AV.User.logIn(this.username,this.password).then(function(a){this.user=a.toJSON(),this.username=this.password="",window.myChartInstance=null}.bind(this)).catch(alert)},signup:function(){AV.User.signUp(this.username,this.password).then(function(a){this.user=a.toJSON(),this.username=this.password=""}.bind(this)).catch(alert)},logout:function(){AV.User.logOut(),this.user=null},addTodo:function(){var b,a=this.newTodo&&this.newTodo.trim();a&&(b=new AV.ACL,b.setPublicReadAccess(!1),b.setPublicWriteAccess(!1),b.setReadAccess(AV.User.current(),!0),b.setWriteAccess(AV.User.current(),!0),new Todo({content:a,done:!1,user:AV.User.current()}).setACL(b).save().then(function(a){this.records.push(a.toJSON())}.bind(this)).catch(alert),this.newTodo="")},removeTodo:function(a){AV.Object.createWithoutData("Todo",a.objectId).destroy().then(function(){this.records.splice(this.records.indexOf(a),1)}.bind(this)).catch(alert)},editTodo:function(a){this.beforeEditCache=a.content,this.editedTodo=a},cancelEdit:function(a){this.editedTodo=null,a.content=this.beforeEditCache},removeCompleted:function(){AV.Object.destroyAll(filters.completed(this.records).map(function(a){return AV.Object.createWithoutData("Todo",a.objectId)})).then(function(){this.records=filters.active(this.records)}.bind(this)).catch(alert)},run:function(){var b,a=this;this.result="处理中…",b=JSON.parse(this.params||"{}"),AV.Cloud.run(this.cloudFunc,b).then(function(b){var c=JSON.stringify(b,null,2);a.result="返回结果:\r\n"+c})}},directives:{"todo-focus":function(a,b){b&&a.focus()}}}),window.addEventListener("hashchange",onHashChange),onHashChange(),app.$mount(".todoapp");