parent
2f4f9154b6
commit
09a41da590
@ -0,0 +1,2 @@ |
||||
//download.js v4.21, by dandavis; 2008-2018. [MIT] see http://danml.com/download.html for tests/usage
|
||||
;(function(root,factory){typeof define=="function"&&define.amd?define([],factory):typeof exports=="object"?module.exports=factory():root.download=factory()})(this,function(){return function download(data,strFileName,strMimeType){var self=window,defaultMime="application/octet-stream",mimeType=strMimeType||defaultMime,payload=data,url=!strFileName&&!strMimeType&&payload,anchor=document.createElement("a"),toString=function(a){return String(a)},myBlob=self.Blob||self.MozBlob||self.WebKitBlob||toString,fileName=strFileName||"download",blob,reader;myBlob=myBlob.call?myBlob.bind(self):Blob,String(this)==="true"&&(payload=[payload,mimeType],mimeType=payload[0],payload=payload[1]);if(url&&url.length<2048){fileName=url.split("/").pop().split("?")[0],anchor.href=url;if(anchor.href.indexOf(url)!==-1){var ajax=new XMLHttpRequest;return ajax.open("GET",url,!0),ajax.responseType="blob",ajax.onload=function(e){download(e.target.response,fileName,defaultMime)},setTimeout(function(){ajax.send()},0),ajax}}if(/^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(payload)){if(!(payload.length>2096103.424&&myBlob!==toString))return navigator.msSaveBlob?navigator.msSaveBlob(dataUrlToBlob(payload),fileName):saver(payload);payload=dataUrlToBlob(payload),mimeType=payload.type||defaultMime}else if(/([\x80-\xff])/.test(payload)){var i=0,tempUiArr=new Uint8Array(payload.length),mx=tempUiArr.length;for(i;i<mx;++i)tempUiArr[i]=payload.charCodeAt(i);payload=new myBlob([tempUiArr],{type:mimeType})}blob=payload instanceof myBlob?payload:new myBlob([payload],{type:mimeType});function dataUrlToBlob(strUrl){var parts=strUrl.split(/[:;,]/),type=parts[1],indexDecoder=strUrl.indexOf("charset")>0?3:2,decoder=parts[indexDecoder]=="base64"?atob:decodeURIComponent,binData=decoder(parts.pop()),mx=binData.length,i=0,uiArr=new Uint8Array(mx);for(i;i<mx;++i)uiArr[i]=binData.charCodeAt(i);return new myBlob([uiArr],{type:type})}function saver(url,winMode){if("download"in anchor)return anchor.href=url,anchor.setAttribute("download",fileName),anchor.className="download-js-link",anchor.innerHTML="downloading...",anchor.style.display="none",anchor.addEventListener("click",function(e){e.stopPropagation(),this.removeEventListener("click",arguments.callee)}),document.body.appendChild(anchor),setTimeout(function(){anchor.click(),document.body.removeChild(anchor),winMode===!0&&setTimeout(function(){self.URL.revokeObjectURL(anchor.href)},250)},66),!0;if(/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//.test(navigator.userAgent))return/^data:/.test(url)&&(url="data:"+url.replace(/^data:([\w\/\-\+]+)/,defaultMime)),window.open(url)||confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")&&(location.href=url),!0;var f=document.createElement("iframe");document.body.appendChild(f),!winMode&&/^data:/.test(url)&&(url="data:"+url.replace(/^data:([\w\/\-\+]+)/,defaultMime)),f.src=url,setTimeout(function(){document.body.removeChild(f)},333)}if(navigator.msSaveBlob)return navigator.msSaveBlob(blob,fileName);if(self.URL)saver(self.URL.createObjectURL(blob),!0);else{if(typeof blob=="string"||blob.constructor===toString)try{return saver("data:"+mimeType+";base64,"+self.btoa(blob))}catch(y){return saver("data:"+mimeType+","+encodeURIComponent(blob))}reader=new FileReader,reader.onload=function(e){saver(this.result)},reader.readAsDataURL(blob)}return!0}}); |
@ -0,0 +1,46 @@ |
||||
/** |
||||
* jQuery PopMenu |
||||
*
|
||||
* Context menu (popup menu) plugin for web applications. |
||||
*
|
||||
* Copyright © 2014 Fajar Yoseph Chandra. All rights reserved. |
||||
*
|
||||
* @version 1.0.0 |
||||
* @author Fajar Chandra |
||||
* @since 2014.05.29 |
||||
*/ |
||||
PopMenu=function(menu,options){this.id="PopMenu_"+Math.round(Math.random()*1E7);this.data=PopMenu.defaults;this.$=$('<div id="'+this.id+'" class="PopMenu-Container" style="display: none;">'+'<ul class="PopMenu-Menu">'+"</ul>"+"</div>");this.$menu=this.$.children(".PopMenu-Menu");this.populateMenu(menu);this.$.on("click",function(){var instance=$(this).data("PopMenu.instance");instance.hide()});this.$.on("contextmenu",function(){return false});this.$.on("mousewheel DOMMouseScroll",function(e){e.stopPropagation(); |
||||
e.preventDefault();return false});this.$menu.on("mousewheel DOMMouseScroll",function(e){if(!$(this).hasClass("overflows"))return false;var marginTop=parseInt($(this).css("margin-top"));if((e.type="DOMMouseScroll"&&e.originalEvent.detail<0)||typeof WheelEvent!="undefined"&&e.originalEvent instanceof WheelEvent&&e.originalEvent.wheelDeltaY>0||typeof MouseEvent!="undefined"&&e.originalEvent instanceof MouseEvent&&e.originalEvent.detail<0||typeof Event!="undefined"&&e.originalEvent instanceof Event&& |
||||
e.originalEvent.wheelDelta>0){marginTop+=72;if(marginTop>0)marginTop=0}else if((e.type="DOMMouseScroll")||typeof WheelEvent!="undefined"&&e.originalEvent instanceof WheelEvent||typeof MouseEvent!="undefined"&&e.originalEvent instanceof MouseEvent||typeof Event!="undefined"&&e.originalEvent instanceof Event){marginTop-=72;if(marginTop+$(this).data("menu-height")<$(window).height())marginTop=$(window).height()-$(this).data("menu-height")}$(this).stop();$(this).animate({"margin-top":marginTop+"px"}, |
||||
100,"linear")});this.$menu.on("mousemove",function(e){if(!$(this).hasClass("overflows"))return false;var $instance=$(this).parent().data("PopMenu.instance");var marginTop=parseInt($(this).css("margin-top"));var duration=0;if(e.pageY-$(window).scrollTop()<$instance.data.scrollerSize){duration=-marginTop;marginTop=0}else if(e.pageY-$(window).scrollTop()>$(window).height()-$instance.data.scrollerSize){duration=marginTop-($(window).height()-$(this).data("menu-height"));marginTop=$(window).height()-$(this).data("menu-height")}else{if($(this).data("is-scrolling-with-scroller")){$(this).stop(); |
||||
$(this).data("is-scrolling-with-scroller",false)}return}$(this).stop();$(this).data("is-scrolling-with-scroller",true);$(this).animate({"margin-top":marginTop+"px"},duration*3,"linear",function(){$(this).data("is-scrolling-with-scroller",false)})});this.$.data("PopMenu.instance",this);this.options(options)};PopMenu.direction={RIGHT:2,LEFT:0,TOP:0,BOTTOM:1,HORIZONTAL:2,VERTICAL:1,RIGHT_BOTTOM:3,RIGHT_TOP:2,LEFT_BOTTOM:1,LEFT_TOP:0}; |
||||
PopMenu.defaults={effect:"fade",duration:200,contextMenu:false,tmpFx:undefined,direction:PopMenu.direction.RIGHT_BOTTOM,scrollerSize:24};PopMenu.prototype.options=function(options){if(arguments.length==0)return this.data;this.data=$.extend(true,{},this.data,options);this.effect(this.data.effect);this.duration(this.data.duration);this.contextMenu(this.data.contextMenu)}; |
||||
PopMenu.prototype.effect=function(effect){if(arguments.length==0)return this.data.effect;switch(effect){case "fade":case "slide":case "none":this.data.effect=effect;return true;default:return false}};PopMenu.prototype.duration=function(duration){if(arguments.length==0)return this.data.duration;this.data.duration=duration}; |
||||
PopMenu.prototype.contextMenu=function(value){if(arguments.length==0)return this.data.contextMenu;this.data.contextMenu=value;this.$.removeClass("PopMenu");this.$.removeClass("PopMenu-TopMenu");if(this.data.contextMenu){this.$.addClass("PopMenu");this.$menu.addClass("PopMenu-TopMenu");$("body").append(this.$)}}; |
||||
PopMenu.prototype.show=function(x,y,effect,duration){if(!this.contextMenu())return false;if(effect==null||effect=="default")effect=this.data.effect;if(duration===undefined)duration=this.data.duration;this.data.tmpFx=effect;this.$.css("visibility","hidden");this.$.show();var menuW=this.$menu.outerWidth();var menuH=this.$menu.outerHeight();this.$.hide();this.$.css("visibility","visible");if(x<0)x=0;if(x+menuW>$(window).width()){x-=menuW;if(x+menuW>$(window).width())x=$(window).width()-menuW}if(y<0)y= |
||||
0;if(y+menuH>$(window).height()){y-=menuH;if(y+menuH>$(window).height())y=$(window).height()-menuH}this.$menu.css("left",x+"px");this.$menu.css("top",y+"px");this.updateLabels();switch(effect){default:case "none":this.$.show();break;case "fade":this.$.fadeIn(duration);break;case "slide":this.$.slideDown(duration);break}}; |
||||
PopMenu.prototype.showAsSubmenu=function(direction,effect,duration){var $parent=this.$.parent(".PopMenu-Item");if($parent.length==0)return false;if(effect==null||effect=="default")effect=this.data.effect;if(duration===undefined)duration=this.data.duration;if(direction===undefined)direction=this.data.direction;this.data.tmpFx=effect;this.$menu.removeClass("overflows");this.$menu.css("margin-top",0);if((direction&PopMenu.direction.HORIZONTAL)==PopMenu.direction.RIGHT){this.$menu.css("left","100%"); |
||||
this.$menu.css("right","auto")}else{this.$menu.css("left","auto");this.$menu.css("right","100%")}if((direction&PopMenu.direction.VERTICAL)==PopMenu.direction.BOTTOM){this.$menu.css("top","0");this.$menu.css("bottom","auto")}else{this.$menu.css("top","auto");this.$menu.css("bottom","0")}this.$.css("visibility","hidden");this.$.show();var menuW=this.$menu.outerWidth();var menuH=this.$menu.outerHeight();var x=this.$menu.offset().left-$(window).scrollLeft();var y=this.$menu.offset().top-$(window).scrollTop(); |
||||
this.$menu.data("menu-height",menuH);this.$menu.data("menu-width",menuW);this.$.hide();this.$.css("visibility","visible");var parentH=$parent.outerHeight();if((direction&PopMenu.direction.HORIZONTAL)==PopMenu.direction.RIGHT&&x+menuW>$(window).width()){this.$menu.css("left","auto");this.$menu.css("right","100%");this.data.direction=PopMenu.direction.direction&~PopMenu.direction.HORIZONTAL|PopMenu.direction.LEFT}if((direction&PopMenu.direction.HORIZONTAL)==PopMenu.direction.LEFT&&x<0){this.$menu.css("left", |
||||
"100%");this.$menu.css("right","auto");this.data.direction=PopMenu.direction.direction&~PopMenu.direction.HORIZONTAL|PopMenu.direction.RIGHT}if((direction&PopMenu.direction.VERTICAL)==PopMenu.direction.BOTTOM&&y+menuH>$(window).height()){this.$menu.css("top","auto");this.$menu.css("bottom","0");if(y+parentH-menuH<0){this.$menu.css("top",-y+"px");this.$menu.css("bottom","auto");this.$menu.addClass("overflows")}}if((direction&PopMenu.direction.VERTICAL)==PopMenu.direction.TOP&&y<0){this.$menu.css("top", |
||||
"0");this.$menu.css("bottom","auto");if(y+parentH>$(window).height()){this.$menu.css("top",-y+"px");this.$menu.css("bottom","auto");this.$menu.addClass("overflows")}}$parent.addClass("selected");switch(effect){default:case "none":this.$.show();break;case "fade":this.$.fadeIn(duration);break;case "slide":this.$.slideDown(duration);break}}; |
||||
PopMenu.prototype.hide=function(effect,duration){if(effect==null)effect=this.data.tmpFx;if(duration===undefined)duration=this.data.duration;this.hideSubmenus(effect,duration);switch(effect){default:case "none":this.$.hide();break;case "fade":this.$.fadeOut(duration);break;case "slide":this.$.slideUp(duration);break}}; |
||||
PopMenu.prototype.hideSubmenus=function(effect,duration){this.$.find(".PopMenu-Item.selected").removeClass("selected");switch(effect){default:case "none":this.$.find(".PopMenu-Container").hide();break;case "fade":this.$.find(".PopMenu-Container").fadeOut(duration);break;case "slide":this.$.find(".PopMenu-Container").slideUp(duration);break}};PopMenu.prototype.populateMenu=function(menu){var instance=this;$.each(menu,function(index,item){instance.append(index,item)})}; |
||||
PopMenu.prototype.find=function(id){var $find=this.$menu.find("a[data-id="+id+"]");if($find.length==0)return null;else return $find.parent().data("PopMenu.instance")};PopMenu.prototype.append=function(id,item){item.id=id;var menuItem=new PopMenuItem(item);this.$menu.append(menuItem.$);return menuItem};PopMenu.prototype.insert=function(id,item,after){item.id=id;var $after=this.find(after).$;var menuItem=new PopMenuItem(item);$after.after(menuItem.$);return menuItem}; |
||||
PopMenu.prototype.prepend=function(id,item){item.id=id;var menuItem=new PopMenuItem(item);this.$menu.prepend(menuItem.$);return menuItem};PopMenu.prototype.remove=function(id){var item=this.find(id);if(item!=null){item.$.remove();delete item}};PopMenu.prototype.updateLabels=function(force){this.$.find("a.PopMenu-Link").each(function(){var instance=$(this).parent().data("PopMenu.instance");instance.updateLabel(force)})}; |
||||
PopMenu.prototype.selectedItem=function(){var selected=this.$.find(".PopMenu-Item.selected");if(selected.length>0)return selected.data("PopMenu.instance");else return null}; |
||||
PopMenuItem=function(options){this.data=PopMenuItem.defaults;this.callbacks={};this.$=$('<li class="PopMenu-Item">'+'<a class="PopMenu-Link" data-name="'+options.id+'">'+"</a>"+'<span class="PopMenu-Icon"></span>'+"</li>");this.$link=this.$.children(".PopMenu-Link");this.$icon=this.$.children(".PopMenu-Icon");this.options(options);this.$link.click(function(e){if($(this).parent().hasClass("disabled"))return false;$(this).trigger("action",e);if($(this).parent().hasClass("PopMenu-HasMenu"))e.stopPropagation()}); |
||||
this.$.mouseover(function(e){var instance=$(this).data("PopMenu.instance");var $container=$(this).parents(".PopMenu-Container:eq(0)");if($container.length>0){var container=$container.data("PopMenu.instance");if(container.selectedItem()!=instance){container.hideSubmenus();if(instance.submenu!=null)instance.submenu.showAsSubmenu(container.data.direction,container.data.effect,container.data.duration)}}else if(instance.submenu!=null)instance.submenu.showAsSubmenu(container.data.direction,container.data.effect, |
||||
container.data.duration);e.stopPropagation()});this.$.data("PopMenu.instance",this)};PopMenuItem.defaults={id:undefined,type:"item",label:undefined,key:undefined,action:undefined,href:undefined,target:undefined,icon:undefined,disabled:false,submenu:undefined,visible:true}; |
||||
PopMenuItem.prototype.on=function(event,callback){var instance=this;if(typeof this.callbacks[event]!="object"){this.callbacks[event]=[];this.$link.on(event,function(){var args=arguments;$.each(instance.callbacks[event],function(index,item){item.apply(instance,args)})})}this.callbacks[event].push(callback)}; |
||||
PopMenuItem.prototype.off=function(event){this.$link.off(event);delete this.callbacks[event];if(event=="click")this.$link.click(function(e){if($(this).parent().hasClass("disabled"))return false;$(this).trigger("action",e);if($(this).parent().hasClass("PopMenu-HasMenu"))e.stopPropagation()})}; |
||||
PopMenuItem.prototype.options=function(options){if(arguments.length==0)return this.data;this.data=$.extend(true,{},this.data,options);this.id(this.data.id);this.key(this.data.key);this.type(this.data.type);this.icon(this.data.icon);this.label(this.data.label);this.disable(this.data.disabled);this.visible(this.data.visible);this.href(this.data.href);this.target(this.data.target);if(typeof this.data.action=="function"){this.off("action");this.on("action",this.data.action)}this.data.action=undefined; |
||||
if(typeof this.data.submenu=="object"){if(this.data.submenu instanceof PopMenu==false)this.data.submenu=new PopMenu(this.data.submenu);this.$.append(this.data.submenu.$);this.$.addClass("PopMenu-HasMenu");this.submenu=this.data.submenu}else{this.$.removeClass("PopMenu-HasMenu");delete this.submenu}}; |
||||
PopMenuItem.prototype.icon=function(icon){if(arguments.length==0)return this.data.icon;this.$icon.css("background-image","none");this.$icon.attr("data-icon",null);if(icon==null)return;if(icon.indexOf(".")==-1)this.$icon.attr("data-icon",icon);else this.$icon.css("background-image",'url("'+icon+'")')};PopMenuItem.prototype.show=function(){this.data.visible=true;this.$.show()};PopMenuItem.prototype.hide=function(){this.data.visible=false;this.$.hide()}; |
||||
PopMenuItem.prototype.visible=function(value){if(arguments.length==0)return this.data.visible;if(value)return this.show();else return this.hide()};PopMenuItem.prototype.id=function(id){if(arguments.length==0)return this.data.id;this.data.id=id;this.$link.attr("data-id",this.data.id)};PopMenuItem.prototype.key=function(key){if(arguments.length==0)return this.data.key;this.data.key=key;this.$link.attr("data-key",this.data.key)}; |
||||
PopMenuItem.prototype.href=function(url){if(arguments.length==0)return this.data.href;this.data.href=url;if(this.data.href)this.$link.attr("href",this.data.href);else this.$link.attr("href",null)};PopMenuItem.prototype.target=function(target){if(arguments.length==0)return this.data.target;this.data.target=target;if(this.data.target)this.$link.attr("target",this.data.target);else this.$link.attr("target",null)}; |
||||
PopMenuItem.prototype.label=function(label){if(arguments.length==0)return this.data.label;this.data.label=label;this.updateLabel(true)};PopMenuItem.prototype.type=function(type){if(arguments.length==0)return this.data.type;var invalid=false;switch(type){case "item":this.$.removeClass();this.$.addClass("PopMenu-Item");break;case "separator":this.$.removeClass();this.$.addClass("PopMenu-Separator");break;default:invalid=true;break}if(!invalid)this.data.type=type}; |
||||
PopMenuItem.prototype.disable=function(disabled){if(disabled===undefined)return this.$.hasClass("disabled");if(disabled)this.$.addClass("disabled");else this.$.removeClass("disabled")};PopMenuItem.prototype.updateLabel=function(force){if(typeof this.data.label!="function"&&!force)return;if(typeof this.data.label=="function")var label=this.data.label();else var label=this.data.label;if(this.data.key!=null){pattern=new RegExp("("+this.data.key+")","i");label=label.replace(pattern,"<u>$1</u>")}this.$link.html(label)}; |
||||
(function($){var methods={};var defaults={effect:"default",duration:null};$.fn.popmenu=function(){if(arguments.length==0){if(this.data("PopMenu.contextmenu")==null)methods.init.call(this,{});return this.data("PopMenu.contextmenu").data("PopMenu.instance")}if(typeof arguments[0]==="object")return methods.init.call(this,arguments[0]);else if($.isFunction(methods[arguments[0]])){var shift=[].shift;var firstArg=shift.apply(arguments);return methods[firstArg].apply(this,arguments)}};methods.init=function(menu, |
||||
options){var $this=this;if(menu instanceof PopMenu==false)var menu=new PopMenu(menu);menu.contextMenu(true);return $this.each(function(){var $this=$(this);var newOptions=$.extend(true,{},defaults,options);$this.data("PopMenu.contextmenu",menu.$);$this.data("PopMenu.options",newOptions);$this.on("contextmenu",methods.show)})};methods.show=function(e){var $elm=$(this);var $menu=$elm.data("PopMenu.contextmenu");var instance=$menu.data("PopMenu.instance");var options=$elm.data("PopMenu.options");if(e!= |
||||
null){var pageX=e.pageX;var pageY=e.pageY}else{var pageX=$elm.offset().left;var pageY=$elm.offset().top}var scrollLeft=$(window).scrollLeft();var scrollTop=$(window).scrollTop();var menuX=pageX-scrollLeft;var menuY=pageY-scrollTop;instance.show(menuX,menuY,options.effect,options.duration);return false};methods.hide=function(e){var $menu=$(this).data("PopMenu.contextmenu");var options=$(this).data("PopMenu.options");var instance=$menu.data("PopMenu.instance");instance.hide(options.effect,options.duration)}})(jQuery); |
@ -0,0 +1,140 @@ |
||||
/** |
||||
* jQuery PopMenu |
||||
* |
||||
* Default stylesheet for jQuery PopMenu. |
||||
* |
||||
* @version 1.0.0 |
||||
* @author Fajar Chandra |
||||
* @since 2014.05.29 |
||||
*/ |
||||
|
||||
/* CONTAINER */ |
||||
|
||||
.PopMenu { |
||||
position: fixed; |
||||
top: 0; |
||||
left: 0; |
||||
right: 0; |
||||
bottom: 0; |
||||
z-index: 100000; |
||||
} |
||||
|
||||
/* MENU */ |
||||
|
||||
.PopMenu-Menu { |
||||
box-shadow: 2px 2px 2px #000; |
||||
box-shadow: 2px 2px 2px rgba(0,0,0,.5); |
||||
background: #eee; |
||||
background: linear-gradient(#fff, #e0e0e0); |
||||
border: 1px solid #aaa; |
||||
color: #333; |
||||
list-style: none; |
||||
margin: 0; |
||||
padding: 0; |
||||
font-size: 12px; |
||||
font-family: 'Arial', sans-serif; |
||||
cursor: default; |
||||
line-height: 1.2em; |
||||
position: absolute; |
||||
top: 0; |
||||
left: 100%; |
||||
z-index: 100000; |
||||
} |
||||
|
||||
|
||||
/* TOP LEVEL MENU */ |
||||
|
||||
.PopMenu-TopMenu { |
||||
top: 0; |
||||
left: 0; |
||||
} |
||||
|
||||
|
||||
/* MENU ITEM */ |
||||
|
||||
.PopMenu-Item { |
||||
position: relative; |
||||
} |
||||
|
||||
.PopMenu-Item > a { |
||||
position: relative; |
||||
color: #333; |
||||
text-decoration: none; |
||||
cursor: default; |
||||
display: block; |
||||
padding: 5px 15px 5px 30px; |
||||
min-width: 100px; |
||||
z-index: 1; |
||||
white-space: nowrap; |
||||
} |
||||
|
||||
.PopMenu-Item.selected, |
||||
.PopMenu-Item:hover { |
||||
background: #43ace8; |
||||
} |
||||
|
||||
.PopMenu-Item.selected > a, |
||||
.PopMenu-Item:hover > a { |
||||
color: #fff; |
||||
text-decoration: none; |
||||
} |
||||
|
||||
.PopMenu-Item.disabled { |
||||
background: transparent; |
||||
} |
||||
|
||||
.PopMenu-Item.disabled > a { |
||||
color: #aaa; |
||||
} |
||||
|
||||
/* MENU ICON */ |
||||
|
||||
.PopMenu-Icon { |
||||
position: absolute; |
||||
left: 5px; |
||||
top: 50%; |
||||
margin-top: -8px; |
||||
height: 16px; |
||||
width: 16px; |
||||
font-size: 16px; |
||||
line-height: 16px; |
||||
color: #333; |
||||
display: block; |
||||
text-align: center; |
||||
z-index: 0; |
||||
background-repeat: no-repeat; |
||||
background-position: center center; |
||||
} |
||||
|
||||
.PopMenu-Item.disabled .PopMenu-Icon { |
||||
color: #aaa; |
||||
} |
||||
|
||||
/* MENU SEPARATOR */ |
||||
|
||||
.PopMenu-Separator { |
||||
display: block; |
||||
height: 1px; |
||||
background: #ccc; |
||||
margin: 0 2px; |
||||
} |
||||
|
||||
.PopMenu-Separator * { |
||||
display: none; |
||||
} |
||||
|
||||
/* SUBMENU */ |
||||
|
||||
.PopMenu-Item.PopMenu-HasMenu > a:after { |
||||
content: '▶'; |
||||
display: block; |
||||
position: absolute; |
||||
right: 5px; |
||||
top: 4px; |
||||
height: 14px; |
||||
width: 14px; |
||||
text-align: center; |
||||
vertical-align: middle; |
||||
font-size: 8px; |
||||
line-height: 14px; |
||||
} |
@ -0,0 +1,138 @@ |
||||
""" Test loading/saving scenarios. """ |
||||
|
||||
import json |
||||
import time |
||||
|
||||
from selenium.webdriver.support.ui import Select |
||||
|
||||
from vasl_templates.webapp.tests.utils import set_template_params, select_tab |
||||
from vasl_templates.webapp.tests.utils import get_stored_msg, set_stored_msg, find_child, find_children |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
def test_scenario_persistence( webapp, webdriver ): |
||||
"""Test loading/saving scenarios.""" |
||||
|
||||
# initialize |
||||
webdriver.get( webapp.url_for( "main", scenario_persistence=1 ) ) |
||||
|
||||
# initialize |
||||
def load_scenario_fields( fields ): |
||||
"""Load the scenario fields.""" |
||||
for tab_id in fields: |
||||
select_tab( tab_id ) |
||||
set_template_params( fields[tab_id] ) |
||||
|
||||
# load the scenario fields |
||||
scenario_params = { |
||||
"scenario": { |
||||
"scenario_name": "my test scenario", "scenario_location": "right here", "scenario_date": "12/31/1945", |
||||
"scenario_width": "101", |
||||
"player_1": "british", "player_1_elr": "1", "player_1_san": "2", |
||||
"player_2": "french", "player_2_elr": "3", "player_2_san": "4", |
||||
"victory_conditions": "just do it!", "victory_conditions_width": "102", |
||||
"ssr": [ "This is an SSR.", "This is another SSR." ], |
||||
"ssr_width": "103", |
||||
}, |
||||
"ob1": { |
||||
"ob_setup_1": "Player 1's OB", "ob_setup_width_1": "201", |
||||
}, |
||||
"ob2": { |
||||
"ob_setup_2": "Player 2's OB", "ob_setup_width_2": "301", |
||||
}, |
||||
} |
||||
load_scenario_fields( scenario_params ) |
||||
|
||||
# save the scenario and check the results |
||||
saved_scenario = _save_scenario() |
||||
expected = { |
||||
k.upper(): v for tab in scenario_params.values() for k,v in tab.items() |
||||
} |
||||
assert saved_scenario == expected |
||||
|
||||
# reset the scenario |
||||
elem = find_child( "#menu" ) |
||||
elem.click() |
||||
elem = find_child( "a.PopMenu-Link[data-name='new']" ) |
||||
elem.click() |
||||
elem = find_child( ".growl-close" ) |
||||
elem.click() |
||||
time.sleep( 0.5 ) |
||||
|
||||
# check the save results |
||||
data = _save_scenario() |
||||
data2 = { k: v for k,v in data.items() if v } |
||||
assert data2 == { |
||||
"PLAYER_1": "german", "PLAYER_1_ELR": "5", "PLAYER_1_SAN": "2", |
||||
"PLAYER_2": "russian", "PLAYER_2_ELR": "5", "PLAYER_2_SAN": "2", |
||||
} |
||||
|
||||
# load a scenario |
||||
_load_scenario( saved_scenario ) |
||||
|
||||
# make sure the scenario was loaded into the UI correctly |
||||
for tab_id in scenario_params: |
||||
select_tab( tab_id ) |
||||
for field,val in scenario_params[tab_id].items(): |
||||
if field == "ssr": |
||||
continue # nb: this requires special handling, we do it below |
||||
elem = next( c for c in ( \ |
||||
find_child( "{}[name='{}']".format(elem_type,field) ) \ |
||||
for elem_type in ["input","textarea","select"] |
||||
) if c ) |
||||
if elem.tag_name == "select": |
||||
assert Select(elem).first_selected_option.get_attribute("value") == val |
||||
else: |
||||
assert elem.get_attribute("value") == val |
||||
ssrs = _get_ssrs() |
||||
assert ssrs == scenario_params["scenario"]["ssr"] |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
def test_loading_ssrs( webapp, webdriver ): |
||||
"""Test loading SSR's.""" |
||||
|
||||
# initialize |
||||
webdriver.get( webapp.url_for( "main", scenario_persistence=1 ) ) |
||||
_ = _save_scenario() # nb: force the "scenario-persistence" element to be created |
||||
|
||||
# initialize |
||||
def do_test( ssrs ): # pylint: disable=missing-docstring |
||||
_load_scenario( { "SSR": ssrs } ) |
||||
assert _get_ssrs() == ssrs |
||||
|
||||
# load a scenario that has SSR's into a UI with no SSR's |
||||
do_test( [ "ssr 1", "ssr 2" ] ) |
||||
|
||||
# load a scenario that has more SSR's than are currently in the UI |
||||
do_test( [ "ssr 5", "ssr 6", "ssr 7", "ssr 8" ] ) |
||||
|
||||
# load a scenario that has fewer SSR's than are currently in the UI |
||||
do_test( [ "ssr 10", "ssr 11" ] ) |
||||
|
||||
# load a scenario that has no SSR's into a UI that has SSR's |
||||
do_test( [] ) |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
def _load_scenario( scenario ): |
||||
"""Load a scenario into the UI.""" |
||||
set_stored_msg( "scenario_persistence", json.dumps(scenario) ) |
||||
elem = find_child( "#menu" ) |
||||
elem.click() |
||||
elem = find_child( "a.PopMenu-Link[data-name='load']" ) |
||||
elem.click() |
||||
|
||||
def _save_scenario(): |
||||
"""Save the scenario.""" |
||||
elem = find_child( "#menu" ) |
||||
elem.click() |
||||
elem = find_child( "a.PopMenu-Link[data-name='save']" ) |
||||
elem.click() |
||||
data = get_stored_msg( "scenario_persistence" ) |
||||
return json.loads( data ) |
||||
|
||||
def _get_ssrs(): |
||||
"""Get the SSR's from the UI.""" |
||||
select_tab( "scenario" ) |
||||
return [ c.text for c in find_children("#ssr-sortable li") ] |
Loading…
Reference in new issue