From 61c0be91397c236ba1b67adc76f7fdd11ad52688 Mon Sep 17 00:00:00 2001 From: Meutel Date: Tue, 5 Sep 2017 17:35:10 +0200 Subject: [PATCH] prod mode: use minified assets --- README.md | 5 ++++- assets/js/bouquins.min.js | 23 +++++++++++++++++++++++ bouquins/bouquins.go | 9 ++++++++- main.go | 3 ++- templates/footer.html | 4 ++-- templates/header.html | 10 +++++----- 6 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 assets/js/bouquins.min.js diff --git a/README.md b/README.md index dfe8fa3..65330c9 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,7 @@ Bouquins in Go * tests * auth downloads * csrf -* vue.js dev/prod + +## Minify JS + +https://www.danstools.com/javascript-minify/ diff --git a/assets/js/bouquins.min.js b/assets/js/bouquins.min.js new file mode 100644 index 0000000..b14de92 --- /dev/null +++ b/assets/js/bouquins.min.js @@ -0,0 +1,23 @@ +var bus=new Vue();var BOOKS='books',AUTHORS='authors',SERIES='series';var BOUQUINS_TYPES={books:{icon:'book',singular:'livre',plural:'livres',tab_cols:[{id:'title',name:'Titre',sort:'title'},{id:'authors',name:'Auteur(s)'},{id:'series',name:'Serie'}]},authors:{icon:'user',singular:'auteur',plural:'auteurs',tab_cols:[{id:'author_name',name:'Nom',sort:'name'},{id:'count',name:'Livre(s)'}]},series:{icon:'list',singular:'serie',plural:'series',tab_cols:[{id:'serie_name',name:'Nom',sort:'name'},{id:'count',name:'Livre(s)'},{id:'authors',name:'Auteur(s)'}]}};function ty(type){if(BOUQUINS_TYPES[type])return BOUQUINS_TYPES[type] +console.log("ERROR: Unknown type: "+type);return{}} +function icon(type){return ty(type).icon;} +function iconClass(type){return'glyphicon glyphicon-'+icon(type);} +function url(type,id){if(id)return ty(type)?'/'+type+'/'+id:'';return ty(type)?'/'+type+'/':'';} +function label(type,count){return count==1?ty(type).singular:ty(type).plural;} +function stdError(code,resp){console.log('ERROR '+code+': '+resp);} +function sendQuery(url,error,success){var xmh=new XMLHttpRequest();var v;xmh.onreadystatechange=function(){v=xmh.responseText;if(xmh.readyState===4&&xmh.status===200){var res;try{res=JSON.parse(v);}catch(err){if(null!==error) +error(err.name,err.message);} +if(null!==success) +success(res);}else if(xmh.readyState===4){if(null!==error) +error(xmh.status,v);}};xmh.open('GET',url,true);xmh.setRequestHeader('Accept','application/json');xmh.send(null);} +Vue.component('results-list',{template:'#results-list-template',props:['results','count','type'],methods:{url:function(item){return url(this.type,item.id);},label:function(item){switch(this.type){case BOOKS:return item.title;case AUTHORS:case SERIES:return item.name;default:return'';}},iconClass:function(){return iconClass(this.type);},countlabel:function(){return label(this.type,this.count);}}});Vue.component('results',{template:'#results-template',props:['results','cols','sort_by','order_desc'],methods:{sortBy:function(col){bus.$emit('sort-on',col);}}});Vue.component('result-cell',{render:function(h){return h('td',this.cellContent(h));},props:['item','col'],methods:{link:function(h,type,text,id){return[h('span',{attrs:{class:iconClass(type)}},''),' ',h('a',{attrs:{href:url(type,id)}},text)];},badge:function(h,num){return h('span',{attrs:{class:'badge'}},num);},cellContent:function(h){switch(this.col.id){case'author_name':return this.link(h,AUTHORS,this.item.name,this.item.id);case'serie_name':return this.link(h,SERIES,this.item.name,this.item.id);case'count':return this.item.count;case'title':return this.link(h,BOOKS,this.item.title,this.item.id);case'authors':var elts=[];var authors=this.item.authors;if(authors){for(i=0;i1)bus.$emit('update-page',-1);},nextPage:function(){if(this.more)bus.$emit('update-page',1);}}});if(document.getElementById("index")){new Vue({el:'#index',data:{url:'',page:0,perpage:20,more:false,sort_by:null,order_desc:false,cols:[],results:[]},methods:{sortBy:function(col){if(this.sort_by==col){if(this.order_desc){this.order_desc=false;this.sort_by=null;}else{this.order_desc=true;}}else{this.order_desc=false;this.sort_by=col;} +this.updateResults();},updatePage:function(p){this.page+=p;this.updateResults();},order:function(query){return query+(this.order_desc?'&order=desc':'');},sort:function(query){return query+(this.sort_by?'&sort='+this.sort_by:'');},paginate:function(query){return query+'?page='+this.page+'&perpage='+this.perpage;},params:function(url){return this.order(this.sort(this.paginate(url)));},updateResults:function(){sendQuery(this.params(this.url),stdError,this.loadResults);},showSeries:function(){this.url=url(SERIES);this.updateResults();},showAuthors:function(){this.url=url(AUTHORS);this.updateResults();},showBooks:function(){this.url=url(BOOKS);this.updateResults();},loadCols:function(type){this.cols=ty(type).tab_cols;},loadResults(resp){this.results=[];this.more=resp.more;this.loadCols(resp.type);if(resp.results){this.results=resp.results;if(this.page==0)this.page=1;}else{this.page=0;}}},mounted:function(){bus.$on('sort-on',this.sortBy);bus.$on('update-page',this.updatePage);}});} +if(document.getElementById("author")){new Vue({el:'#author',data:{tab:BOOKS},methods:{showBooks:function(){this.tab=BOOKS;},showAuthors:function(){this.tab=AUTHORS;},showSeries:function(){this.tab=SERIES;}}});} +if(document.getElementById("search")){new Vue({el:'#search',data:{urlParams:[],authors:[],books:[],series:[],authorsCount:0,booksCount:0,seriesCount:0,q:'',which:'all',all:false,perpage:10},methods:{searchParams:function(url){var res=url+'?perpage='+this.perpage;for(var i=0;i - + + diff --git a/templates/header.html b/templates/header.html index c4af0bf..f925d33 100644 --- a/templates/header.html +++ b/templates/header.html @@ -4,11 +4,11 @@ {{ if .Title }}{{ .Title }} | {{ end }}Bouquins - - - - - + + + + +