User:Zhaofeng Li/vada/plugin/store.js

if(typeof window!="undefined"){ //inside Vada DOM (function{		var store={};		store.about={			name:"App Store",			description:"Easy installer of Vada plugins",			namespace:"store",			userscript:"User:A930913/vada/plugin/store.js",		}		store.load=function(params){			var apps=params.apps;			var s=$(" ");			s.attr("id","apps");			var c=$(" ");			c.text("Installed apps:");			var ilist = $("");			c.append(ilist);			apps.forEach(function(itm){ var o=$(" ").val(itm.name); s.append(o); if(params.myapps.indexOf(itm.userscript)!=-1){ ilist.append("/g,"")+"' title='"+itm.description.replace(/<.+?>/g,"")+"' value='"+itm.userscript+"'>"+itm.name+""); }			});			var b=$(" ");			b.text("Uninstall selected apps");			b.click(store.uninstall);			c.append(b);			var d=$(" ");			var info=" App Store Search the catalogue and install new apps below. ";			d.html([info,'',s,' ',c]);			$("#diffcontainer").html(d);			$("#appinp").keyup(function{ // call store.list to list apps store.list($("#appinp").val,apps); /*				var app; for(var i=0;i<apps.length;i++){ if(apps[i].name==e.target.value){ app=apps[i]; break; }				}				if(app){ store.show(app); }				*/			});			store.list("",apps); // list everything		};		store.list=function(searchterm,apps){ // display a list based on the search term			var matchedapps=[]; // list of apps to show			if(searchterm.length == 0){ // no search term is entered				matchedapps=apps; // show everything			}else{ // a search term is entered				for(var i=0;i<apps.length;i++){ // loop thru the app list					if(apps[i].name.toLowerCase.indexOf(searchterm.toLowerCase) !== -1) { // matched the search term						matchedapps.push(apps[i]); // add it to the list					}				}			}			$("#applist").empty;			for(var i=0;i");					entry.html([button," "+app.name+" "+app.description+" "]);					$("#applist").append(entry);				}			}		};		/*		store.show=function(app){			var b=$(" ");			b.text("Purchase free app");			b.click(function{ window.vada.call("plugins.store.install",{app:app.userscript}); });			$("#appdisplay").html([" "+app.name+" "+app.description+" Free app is free ",b]);		};		*/		store.uninstall=function{			var rm=[];			$("[name=unapp]:checked").each(function{ rm.push(this.value); });			window.vada.call("plugins.store.uninstall",{rm:rm});		};		store.uninstalled=function{			alert("Page needs to be reloaded to finish uninstalling.");		};		store.run=function(params){			importScript(params.app);			alert("App successfully installed");		};		window.vada.loadPlugin(store);		var blob = new Blob(['\ #applist li{\ display: block;\ margin-top: 5px;\ padding: 5px;\ border-bottom: 1px solid #ccc;\ }\		#applist button{\ float: left;\ margin-right: 10px;\ }\		.appname{\ font-weight: bold;\ }\		.appname, .appdesc{\ display: block;\ }\		.appprice{\ \		}\		.appname:before{\ content:\'Name: \';\ opacity:0.3;\ }\		.appdesc:before{\ content:\'Description: \';\ opacity:0.3;\ }\		.appprice:before{\ content:\'Price: \';\ opacity:0.3;\ }\		#installed{\ font-family:sans-serif;\ display:table-cell;\ }\		#appcontainer{\ display:table;\ width:100%;\ padding:10px;\ box-sizing:border-box;\ background:#eee;\ }\		'], {type: 'text/css'});		var css=document.createElement("link");		css.setAttribute("rel","stylesheet");		css.setAttribute("href",window.URL.createObjectURL(blob));		document.head.appendChild(css);	}); } else { //inside Vada process vada.plugins.store={ start:function{ vada.events.trigger("plugins.stop",{}); vada.events.addListener("plugins.stop",vada.plugins.store.stop); var myapps=vada.plugins.store.getMyApps; vada.dom.call("plugins.store.load",{apps:JSON.parse(new vada.Page("User:A930913/vada/plugin/store.js/plugins.js").text),myapps:myapps}); },		stop:function{ vada.events.removeListener("plugins.stop",vada.plugins.store.stop); },		getMyApps:function{ var vjs=(function{try{return vada.plugins.store.mypage.text;}catch(e){return "";}}); return(function(vjs){				var r=/^importScript\("(.*?)"\);( \/\/\[\[.*?\]\])?$/gm;				var apps=[];				while(m=r.exec(vjs)){try{apps.push(m[1]);}catch(e){return false;}}				return apps;			})(vjs); },		install:function(params){ var myapps=vada.plugins.store.getMyApps; if(myapps.indexOf(params.app)!=-1)return;//Don't double install. myapps.push(params.app); vada.plugins.store.save(myapps); vada.dom.call("plugins.store.run",{app:params.app}); },		uninstall:function(params){ var myapps=vada.plugins.store.getMyApps; for(var i in params.rm){ myapps.splice(myapps.indexOf(params.rm[i]),1); }			vada.plugins.store.save(myapps); vada.dom.call("plugins.store.uninstalled",{}); },		save:function(apps){ vada.plugins.store.mypage.text="/* This page is automatically updated from the app store. Do not change unless you know what you're doing. */\n\n"; for(var i in apps){ vada.plugins.store.mypage.text+='importScript("'+apps[i]+'"); //'+apps[i]+'\n'; }			vada.plugins.store.mypage.save("Updating installed apps"); }	}	vada.plugins.store.button=new vada.dom.menu.Button("store_start"); vada.plugins.store.button.text="App Store"; vada.plugins.store.button.click=vada.plugins.store.start; vada.dom.menu.buttons.vada.children.push(vada.plugins.store.button); vada.dom.menu.redraw; vada.plugins.store.mypage=new vada.Page("User:"+(vada.api({action:"query",meta:"userinfo"}).query.userinfo.name)+"/vada.js"); }