1 line
16 KiB
JavaScript
1 line
16 KiB
JavaScript
"use strict";function reduceOpenWeather(t){var e=moment(1e3*t.dt),i=t.weather[0];return{timestamp:t.dt,icon:"wi-owm-"+i.id,summary:i.description,tempHigh:parseInt(t.temp.max,10),tempLow:parseInt(t.temp.min,10),datelong:e.format(),time:t.dt,date:e.format("D/M"),day:e.format("ddd")}}function reduceDarkSky(t){var e=moment(1e3*t.time);return{timestamp:t.time,icon:weatherIcons.get(t.icon),summary:t.summary,tempHigh:parseInt(t.temperatureHigh,10),tempLow:parseInt(t.temperatureLow,10),datelong:e.format(),time:t.time,date:e.format("D/M"),day:e.format("ddd")}}function randomAmount(t){var e,i="";for(e=0;e<t;e++)i+=this.alpha.random();return i}function newClick(t){var e=(this.left.random()+" "+this.right.random()+" "+this.numberCluster()).split(" ").join(this.whitespace.random()),i=(this.randomAmount(5)+" "+this.randomAmount(5)).split(" ").join(this.whitespace.random()),n=this.passwordTemplate({long:e,short:i});this.passwordOut.removeClass("mui--hide"),this.passwordOut.empty().append(n)}function numberCluster(){return this.numbers.random()+this.numbers.random()+this.numbers.random()}var SOCKETMANAGER,EventModel,EventView,BitcoinModel,Bitcoin,FxModel,FxView,_typeof,TrainModel,TrainView,WCollection,WView,PasswordView,popitout,popitoutSmall,WEBSOCKET=function(t){var e=["localhost","silvrtree.co.uk"],i="9000";"https:"===document.location.protocol?(e="wss://"+e[1],i=""):(e="ws://"+e[0],i="9000"),this.socket=null,this.timer=0,this.clock=null,this.startWebSocket=function(){var t,n=""===i?e:e+":"+i;t=window.MozWebSocket?MozWebSocket:WebSocket,this.socket=new t(n,"stream"),this.socket.onopen=this.handleWebsocketOnOpen.bind(this),this.socket.onmessage=this.handleWebsocketMessage.bind(this),this.socket.onclose=this.handleWebsocketClose.bind(this),this.socket.onerror=function(t){}},this.send=function(t){this.socket.send(t)},this.handleData=function(e){t.trigger("message",e)},this.handleWebsocketOnOpen=function(){this.retry=0,this.clock=new Date},this.handleWebsocketMessage=function(t){var e=void 0;try{e=JSON.parse(t.data)}catch(t){}e&&this.handleData.call(this,e)},this.handleWebsocketClose=function(){var t;new Date,t=this,this.timer=setTimeout(function(){t.startWebSocket()},15e3)},this.startWebSocket()};SOCKETMANAGER=Backbone.Model.extend({initialize:function(){_.bindAll(this,"turnOn","turnOff"),this.listeningID=null,this.listening=!1,this.webSocket=new WEBSOCKET(this),this.on("message",function(t){this.listening&&this.checkItem(t)})},turnOn:function(){this.listening=!0},turnOff:function(){this.listening=!1},listenFor:function(t){this.listeningID=this.deviceId.indexOf(t)},checkItem:function(t){t.hasOwnProperty("id")&&("btc"===t.id&&void 0!==this.btc&&this.btc.btcFromSocket(t.data),"balance"===t.id&&void 0!==this.btc&&this.btc.balanceFromSocket(t.data))},setBTC:function(t){this.btc=t},setTrain:function(t){this.train=t},getUpdate:function(){this.webSocket.send("update")}}),EventModel=Backbone.Model.extend({initialize:function(){this.update()},getDays:function(t,e){var i=void 0,n=void 0;return i=t.getTime(),n=e.getTime(),(n-i)/864e5},update:function(){var t,e=new Date,i=36e5-e.getTime()%36e5,n={};n.days=Math.ceil(this.getDays(e,this.get("event"))),n.weeks=Math.ceil(this.getDays(e,this.get("event"))/7),this.set("data",n),t=function(){this.update()},setTimeout(t.bind(this),i+10)}}),EventView=Backbone.View.extend({tagName:"div",initialize:function(){_.bindAll(this,"render"),this.model.bind("change",this.render),this.id="e_"+Math.random().toString(36).substr(2,9),this.$events=$("#events"),this.$myEvent=null,this.$el=this.$events,this.initView(),this.render()},render:function(){var t=this.model.get("label"),e=this.model.get("data"),i=t+" "+e.days+" days / "+e.weeks+" weeks";this.$myEvent.empty().append(i)},initView:function(){var t="<div class='mui-col-xs-12 mui-col-md-3' id=\""+this.id+'"></div>';this.$html=$(t),this.$events.append(this.$html),this.$myEvent=$("#"+this.id)}}),BitcoinModel=Backbone.Model.extend({initialize:function(){this.set("url","/btc"),this.set("balanceUrl","/balance");var t={lastGBP:0,lastUSD:0,lows:{gbp:0,usd:0},highs:{gbp:0,usd:0},eclass:"",balance:0,trend:0};this.set("btcdata",t),this.set("balance",0),this.update(),this.updateHourly()},update:function(){this.getBTC(),(new Date).getTime()},updateHourly:function(){this.getBalance(),(new Date).getTime()},recalc:function(){var t=this.get("btcdata"),e=t.lastGBP,i=void 0,n=t.gbp,s=t.usd,a=t.lows,o=t.highs,r=t.eclass,l=t.balance,d=t.trend;void 0!==d&&null!==d||(d=1),void 0!==n&&(0!==t.lastGBP?r=n>e?"up":"down":(a.gbp=n,a.usd=s,o.gbp=n,o.usd=s),e=n,i=s,n<a.gbp&&(a.gbp=n),s<a.usd&&(a.usd=s),o.gbp<n&&(o.gbp=n),o.usd<s&&(o.usd=s),t={lastGBP:e,lastUSD:i,lows:a,highs:o,eclass:r,balance:l,trend:d}),t.stub=Math.random(Number.MAX_SAFE_INTEGER).toString(32),this.set("btcdata",t)},getBTC:function(){var t,e;t=this,e=this.get("url"),$.ajax({type:"GET",url:e,data:"",dataType:"json",timeout:1e4,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"PUT, GET, POST, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type"},success:function(e){var i=e.bpi.GBP.rate_float,n=e.bpi.USD.rate_float,s=e.trend,a=t.get("btcdata");a.gbp=i,a.usd=n,a.trend=s,t.set("btcdata",a),t.recalc()},error:function(t,e){}})},getBalance:function(){var t=this,e=this.get("balanceUrl");$.ajax({type:"GET",url:e,data:"",dataType:"json",timeout:1e4,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"PUT, GET, POST, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type"},success:function(e){var i=e.balance;t.set("balance",i),t.recalc()},error:function(t,e){}})},btcFromSocket:function(t){var e,i,n,s,a=this;e=t.bpi.GBP.rate_float,i=t.bpi.USD.rate_float,n=t.trend,(s=a.get("btcdata")).gbp=e,s.usd=i,s.trend=n,a.set("btcdata",s),a.recalc()},balanceFromSocket:function(t){var e,i=this;e=t.balance,i.set("balance",e),i.recalc()}}),Bitcoin=Backbone.View.extend({tagName:"div",initialize:function(){_.bindAll(this,"render"),this.model.bind("change",this.render),this.$btc=$("#btc"),this.$trend=$("#trend")},render:function(){var t=this.model.get("btcdata"),e=this.model.get("balance"),i=parseFloat(t.lastGBP)*parseFloat(e),n="High: $"+parseFloat(t.highs.usd.toFixed(2))+" / Low $"+parseFloat(t.lows.usd.toFixed(2)),s="";s=t.trend>1?"trendUp":t.trend<1?"trendDown":"",this.$trend.removeClass(),this.$trend.addClass(s),this.$btc.removeClass(),this.$btc.addClass(t.eclass),this.$btc.html("$"+parseFloat(t.lastUSD.toFixed(2))+" / £"+parseFloat(t.lastGBP.toFixed(2))+" <div>₿"+e+" £"+parseFloat(i.toFixed(2))+"</div>"),this.$btc.prop("title",n)}}),FxModel=Backbone.Model.extend({initialize:function(){this.set("url","/fx"),this.set("fxdata",{}),this.update()},update:function(){var t,e;this.getFX(),t=9e5-(new Date).getTime()%9e5,e=function(){this.update()},setTimeout(e.bind(this),t+10)},getFX:function(){var t=this.get("url"),e=this;$.ajax({type:"GET",url:t,data:"",dataType:"json",timeout:1e4,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"PUT, GET, POST, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type"},success:function(t){var i,n,s={};void 0!==t.rates&&(n=(i=1/t.rates.GBP)*t.rates.SEK,s={usd:1,gbp:t.rates.GBP,sek:t.rates.SEK,gpbe:i,sekex:n}),e.set("fxdata",s)},error:function(t,e){}})}}),FxView=Backbone.View.extend({tagName:"div",initialize:function(){_.bindAll(this,"render"),this.model.bind("change",this.render),this.$fx=$("#fx")},render:function(){var t=this.model.get("fxdata");this.$fx.html("£1 = $"+parseFloat(t.gpbe.toFixed(2))+" = "+parseFloat(t.sekex.toFixed(2))+" SEK")}}),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},TrainModel=Backbone.Model.extend({initialize:function(){this.get("from"),this.get("to");var t="/getnexttraintimes?from="+this.get("from")+"&to="+this.get("to"),e="/gettrains?from="+this.get("from")+"&to="+this.get("to"),i=this.get("from")+this.get("to");this.set("url",t),this.set("routeUrl",e),this.set("target",i),this.set("visible",!1),this.set("trainData",{eta:"OFF",sta:"OFF"}),this.update()},update:function(){var t,e=new Date,i=e.getHours(),n=i<6?36e5:6e4,s=n-e.getTime()%n;i>=6?this.getTrain():this.set("trainData",{eta:"OFF",sta:"OFF"}),t=function(){this.update()},setTimeout(t.bind(this),s+10)},getTrain:function(){var t=this.get("url"),e=this;$.ajax({type:"GET",url:t,data:"",dataType:"json",timeout:1e4,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"PUT, GET, POST, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type"},success:function(t){e.set("trainData",t)},error:function(t,e){}})},getRoute:function(){var t=this.get("routeUrl"),e=this;!0===this.get("visible")?this.set("visible",!1):$.ajax({type:"GET",url:t,data:"",dataType:"json",timeout:1e4,headers:{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"PUT, GET, POST, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type"},success:function(t){e.set("route",t),e.set("visible",!0)},error:function(t,e){}})}}),TrainView=Backbone.View.extend({tagName:"div",initialize:function(){_.bindAll(this,"render"),this.model.bind("change",this.render),this.$trains=$("#trains"),this.$traininfo=$("#traininfo"),this.$traintext=$("#trainResults"),this.$el=this.$trains,this.initView()},events:{click:"showTrains"},render:function(){var t,e,i,n,s,a,o,r,l,d,c,h,u,m,p,b,f,g,v,w,y,T,C,k,A=this.model.get("trainData"),S=this.model.get("visible"),x=this.model.get("route"),$="on time"===A.eta.toLowerCase()?A.sta:A.eta,E="on time"===A.eta.toLowerCase()?"ontime":"delayed";if(this.$button.html($),this.$button.removeClass("delayed").removeClass("ontime").addClass(E),S){if(t="<div>"+x.locationName+" TO "+x.filterLocationName+'</div>\n <table class="mui-table mui-table-bordered">\n <tr><th>Destination</th>\n <th>Time</th>\n <th>Status</th>\n <th>Platform</th></tr>\n ',e=[],"object"===_typeof(x.trainServices)&&null!==x.trainServices){i=!0,n=!1,s=void 0;try{for(a=x.trainServices[Symbol.iterator]();!(i=(o=a.next()).done);i=!0)d=null!==(l=(r=o.value).destination[0]).via?"<em>"+l.via+"</em>":"",c=null!==r.platform?r.platform:"💠",h=null!==r.sta?r.sta:"D "+r.std,u=null!==r.eta?r.eta:r.etd,e.push({location:l.locationName,time:h,status:u,platform:c,cancel:r.cancelReason,type:"train"}),t=r.isCancelled?t+"<tr><td>"+l.locationName+" "+d+"</td><td>"+h+'</td>\n <td colspan="2">❌ '+r.cancelReason+"</td></tr>":t+"<tr><td>"+l.locationName+" "+d+"</td>\n <td>"+h+"</td>\n <td>"+u+"</td>\n <td>"+c+"</td>\n </tr>"}catch(t){n=!0,s=t}finally{try{!i&&a.return&&a.return()}finally{if(n)throw s}}}if("object"===_typeof(x.busServices)&&null!==x.busServices){m=!0,p=!1,b=void 0;try{for(f=x.busServices[Symbol.iterator]();!(m=(g=f.next()).done);m=!0)y=null!==(w=(v=g.value).destination[0]).via?"<em>"+w.via+"</em>":"",T=null!==v.platform?v.platform:"",C=null!==v.sta?v.sta:"D "+v.std,k=null!==v.eta?v.eta:v.etd,e.push({location:w.locationName,time:C,status:k,platform:T,cancel:v.cancelReason,type:"bus"}),t=t+"<tr><td>🚌 "+w.locationName+" "+y+"</td><td>"+C+"</td><td>"+k+"</td><td>"+T+"</td></tr>"}catch(t){p=!0,b=t}finally{try{!m&&f.return&&f.return()}finally{if(p)throw b}}}t+="</table>",this.$traintext.empty().html(t),this.$traintext.removeClass("mui--hide").addClass("mui--show")}else this.$traintext.removeClass("mui--show").addClass("mui--hide")},initView:function(){var t,e,i,n=this,s=this.model.get("target"),a="<div class='mui-col-xs-12 mui-col-md-6'>"+s.toUpperCase()+': <button class="mui-btn mui-btn--flat" id="'+s+'"></button></div>';this.$html=$(a),this.$html.on("click",function(){n.model.getRoute()}),this.$trains.append(this.$html),this.$button=$("#"+s),e="on time"===(t="OFF")?"ontime":"delayed",this.$button.html(t),this.$button.removeClass("delayed").removeClass("ontime").addClass(e),i="click #$(target)",this.events[i]="showTrains"},showTrains:function(){}}),WCollection=Backbone.Collection.extend({url:"/weather",parse:function(t){return t.list.map(function(t){return reduceOpenWeather(t)})}}),WView=Backbone.View.extend({tagName:"div",template:_.template('\n <% _.each(data, function(item) {%>\n <div class="card mui--z1 mui-col-md-6 mui-col-lg-4">\n <div class="mui-col-md-3">\n <div class="mui--text-accent mui--text-title day mui--text-center"><%= item.day %></div>\n <div class="mui--text-dark-secondary mui--text-subhead mui--text-center"><%= item.date %></div>\n </div>\n <div class="mui-col-md-9">\n <div>\n <i class="mui--text-headline wi <%= item.icon %>"></i>\n <span class="mui--text-display1 temp<%=item.tempHigh %>"><%= item.tempHigh %>°</span> /\n <span class="mui--text-headline temp<%=item.tempLow %>"><%= item.tempLow %>°</span></div>\n <div class="mui--text-caption summary"><%= item.summary %></div>\n </div>\n </div>\n <% }); \n %>'),initialize:function(){_.bindAll(this,"render"),this.collection=new WCollection,this.listenTo(this.collection,"reset sync",_.debounce(_.bind(this.render),128)),this.collection.fetch()},render:function(){if(0!==this.collection.length){var t={data:this.collection.toJSON()};this.$el.html(this.template(t))}}}),Array.prototype.random=function(){return this[Math.floor(Math.random()*this.length)]},PasswordView=Backbone.View.extend({el:"#passwords",passwordTemplate:_.template("<div>Long: <%=long%></div><div>Short: <%=short%></div>"),initialize:function(){this.alpha=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"],this.whitespace=[".","~","#","!","$","+","-","+"],this.numbers=["0","1","2","3","4","5","6","7","8","9"],this.left=["Alabama","Alaska","Arizona","Maryland","Nevada","Mexico","Texas","Utah","Glasgow","Inverness","Edinburgh","Dumbarton","Balloch","Renton","Cardross","Dundee","Paisley","Hamilton","Greenock","Falkirk","Irvine","Renfrew","Erskine","London","Hammersmith","Islington","Silver","Black","Yellow","Purple","White","Pink","Red","Orange","Brown","Green","Blue","Amber","Aqua","Azure","Bronze","Coral","Copper","Crimson","Cyan","Ginger","Gold","Indigo","Jade"],this.right=["Aganju","Cygni","Akeron","Antares","Aragoth","Ardus","Carpenter","Cooper","Dahin","Capella","Endriago","Gallina","Fenris","Freya","Glenn","Grissom","Jotunheim","Kailaasa","Lagarto","Muspelheim","Nifleheim","Primus","Vega","Ragnarok","Shepard","Slayton","Tarsis","Mercury","Venus","Mars","Earth","Terra","Jupiter","Saturn","Uranus","Neptune","Pluto","Europa","Ganymede","Callisto","Titan","Juno","Eridanus","Scorpius","Crux","Cancer","Taurus","Lyra","Andromeda","Virgo","Aquarius","Cygnus","Corvus","Taurus","Draco","Perseus","Pegasus","Gemini","Columbia","Bootes","Orion","Deneb","Merope","Agate","Amber","Beryl","Calcite","Citrine","Coral","Diamond","Emerald","Garnet","Jade","Lapis","Moonstone","Obsidian","Onyx","Opal","Pearl","Quartz","Ruby","Sapphire","Topaz","Iron","Lead","Nickel","Copper","Zinc","Tin","Manes","Argon","Neon","Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliett","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whisky","Xray","Yankee","Zulu"],this.passwordOut=this.$("#passwordOut"),_.bindAll(this,"newClick")},events:{"click #newPassword":"newClick"},numberCluster:numberCluster,randomAmount:randomAmount,newClick:newClick}),document.title="Slack",popitout=function(t){var e=window.open(t,"name","height=600,width=570");return window.focus&&e.focus(),!1},popitoutSmall=function(t){var e=window.open(t,"name","height=400,width=520");return window.focus&&e.focus(),!1}; |