rpgui.min.js 9.2 KB

1
  1. RPGUI=function(){function e(e){var n=e.nextSibling;if(!n||"LABEL"!==n.tagName)throw"After a '"+l+"' there must be a label!";c.copy_event_listeners(e,n),function(e,n){n.addEventListener("click",function(){e.disabled||c.set_value(e,!e.checked)})}(e,n)}function n(e){u=null,window.removeEventListener("mousemove",s)}function t(e){var n=e.target||e.srcElement;if(c.has_class(n,p)){u=n;var t=u.getBoundingClientRect();_={x:t.left-e.clientX,y:t.top-e.clientY},n.style.zIndex=f++,window.addEventListener("mousemove",s,!0)}}function s(e){u&&(u.style.position="absolute",u.style.left=e.clientX+_.x+"px",u.style.top=e.clientY+_.y+"px")}function a(e){progress_container=e,c.insert_after(progress_container,e);var n=c.create_element("div");c.add_class(n,"rpgui-progress-track"),progress_container.appendChild(n);var t=c.create_element("div");c.add_class(t,"rpgui-progress-left-edge"),progress_container.appendChild(t);var s=c.create_element("div");c.add_class(s,"rpgui-progress-right-edge"),progress_container.appendChild(s);var a=c.create_element("div");c.add_class(a,"rpgui-progress-fill"),n.appendChild(a),c.has_class(e,"blue")&&(a.className+=" blue"),c.has_class(e,"red")&&(a.className+=" red"),c.has_class(e,"green")&&(a.className+=" green");var i=void 0!==e.dataset.value?parseFloat(e.dataset.value):1;c.set_value(e,i)}function i(e){var n=e.nextSibling;if(!n||"LABEL"!==n.tagName)throw"After a '"+v+"' there must be a label!";c.copy_event_listeners(e,n),function(e,n){n.addEventListener("click",function(){e.disabled||c.set_value(e,!0)})}(e,n)}function r(e){var n="<label>&#9660;</label> ",t=c.create_element("p");e.id&&(t.id=e.id+"-rpgui-dropdown-head"),c.add_class(t,"rpgui-dropdown-imp rpgui-dropdown-imp-header"),c.insert_after(t,e);var s=c.create_element("ul");e.id&&(s.id=e.id+"-rpgui-dropdown"),c.add_class(s,"rpgui-dropdown-imp"),c.insert_after(s,t);var a=t.getBoundingClientRect();s.style.position="absolute",s.style.width=a.right-a.left-14+"px",s.style.display="none",e.style.display="none";for(var i=0;i<e.children.length;++i){var r=e.children[i];if("OPTION"==r.tagName){var o=c.create_element("li");o.innerHTML=r.innerHTML,s.appendChild(o),c.copy_event_listeners(r,o),function(e,t,s,a,i){s.addEventListener("click",function(){a.innerHTML=n+t.innerHTML,i.style.display="none",t.selected=!0,c.fire_event(e,"change")})}(e,r,o,t,s)}}!function(e,n,t){t.onclick=function(){if(!e.disabled){var t=n.style.display;n.style.display="none"==t?"block":"none"}},n.onmouseleave=function(){n.style.display="none"}}(e,s,t),function(e,t,s){_on_change=function(){-1!=e.selectedIndex?t.innerHTML=n+e.options[e.selectedIndex].text:t.innerHTML=n,s.style.display="none"},e.addEventListener("change",_on_change),_on_change()}(e,t,s)}function o(e){e.size||(e.size=3);var n=c.create_element("ul");e.id&&(n.id=e.id+"-rpgui-list"),c.add_class(n,"rpgui-list-imp"),e.parentNode.insertBefore(n,e.nextSibling),e.style.display="none";for(var t=[],s=0;s<e.children.length;++s){var a=e.children[s];if("OPTION"==a.tagName){var i=c.create_element("li");i.innerHTML=a.innerHTML,n.appendChild(i),i.dataset.rpguivalue=a.value,t.push(i),c.copy_event_listeners(a,i),function(e,n,t,s,a){t.addEventListener("click",function(){e.disabled||(n.selected=!0,c.fire_event(e,"change"))})}(e,a,i,n,t)}}if(t.length&&e.size){var r=t[0].offsetHeight;n.style.height=r*e.size+"px"}!function(e,n){function t(e){for(var t=0;t<n.length;++t){var s=n[t];s.dataset.rpguivalue==e.value?c.add_class(s,"rpgui-selected"):c.remove_class(s,"rpgui-selected")}}e.addEventListener("change",function(){t(this)}),t(e)}(e,t)}function d(e){var n=c.has_class(e,"golden")?" golden":"",t=c.create_element("div");e.id&&(t.id=e.id+"-rpgui-slider"),c.copy_css(e,t),c.add_class(t,"rpgui-slider-container"+n),c.insert_after(t,e),t.style.width=e.offsetWidth+"px";var s=c.create_element("div");c.add_class(s,"rpgui-slider-track"+n),t.appendChild(s);var a=c.create_element("div");c.add_class(a,"rpgui-slider-left-edge"+n),t.appendChild(a);var i=c.create_element("div");c.add_class(i,"rpgui-slider-right-edge"+n),t.appendChild(i);var r=c.create_element("div");c.add_class(r,"rpgui-slider-thumb"+n),t.appendChild(r),e.style.display="none",c.copy_event_listeners(e,t);var o={mouse_down:!1};!function(e,n,t,s,a,i,r){function o(e){var n=u+Math.round(e/p*(_-u))-1;d(n)}function d(n){e.disabled||e.value==n||c.set_value(e,n)}function l(){var n=p/(_-u),s=Math.round(parseFloat(e.value)-u);t.style.left=Math.floor(.25*f)+s*n+"px"}var u=parseFloat(e.min),_=parseFloat(e.max),f=i.offsetWidth+r.offsetWidth,p=s.offsetWidth-f;n.addEventListener("mouseup",function(e){a.mouse_down=!1}),window.addEventListener("mouseup",function(e){a.mouse_down=!1}),s.addEventListener("mousedown",function(e){a.mouse_down=!0,o(e.offsetX||e.layerX)}),n.addEventListener("mousedown",function(e){a.mouse_down=!0}),r.addEventListener("mousedown",function(e){d(u)}),i.addEventListener("mousedown",function(e){d(_)}),r.addEventListener("mousemove",function(e){a.mouse_down&&d(u)}),i.addEventListener("mousemove",function(e){a.mouse_down&&d(_)}),s.addEventListener("mousemove",function(n){a.mouse_down&&!e.disabled&&o(n.offsetX||n.layerX)}),e.addEventListener("change",function(e){l()}),l()}(e,t,r,s,o,i,a)}var c=c||{};c.version=1.03,c.author="Ronen Ness",c.init_on_load=!0,window.addEventListener("load",function(){c.init_on_load&&c.init()}),c.init=function(){if(c._was_init)throw"RPGUI was already init!";for(var e=0;e<c.__init_list.length;++e)c.__init_list[e]();c._was_init=!0},c.__init_list=[],c.on_load=function(e){c._was_init&&e(),c.__init_list.push(e)},c.__update_funcs={},c.__create_funcs={},c.__get_funcs={},c.__set_funcs={},c.create=function(e,n){if(!c.__create_funcs[n])throw"Not a valid rpgui type! options: "+Object.keys(c.__create_funcs);e.dataset.rpguitype=n,c.__create_funcs[n](e)},c.update=function(e){var n=e.dataset.rpguitype;c.__update_funcs[n]?c.__update_funcs[n](e):c.fire_event(e,"change")},c.set_value=function(e,n){var t=e.dataset.rpguitype;c.__set_funcs[t]?c.__set_funcs[t](e,n):e.value=n,c.update(e)},c.get_value=function(e){var n=e.dataset.rpguitype;return c.__get_funcs[n]?c.__get_funcs[n](e):e.value};var l="rpgui-checkbox";c.__create_funcs.checkbox=function(n){c.add_class(n,l),e(n)},c.__set_funcs.checkbox=function(e,n){e.checked=n},c.__get_funcs.checkbox=function(e){return e.checked},c.on_load(function(){for(var e=document.getElementsByClassName(l),n=0;n<e.length;++n)c.create(e[n],"checkbox")}),c.on_load(function(){for(var e=document.getElementsByClassName("rpgui-content"),n=0;n<e.length;++n){var t=e[n];c.prevent_drag(t),c.set_cursor(t,"default")}});var u=null,_=null,f=1e3,p="rpgui-draggable";c.__create_funcs.draggable=function(e){e.draggable=!1,e.ondragstart=function(){return!1},c.add_class(e,p),e.addEventListener("mousedown",t)},c.on_load(function(){for(var e=document.getElementsByClassName(p),t=0;t<e.length;++t)c.create(e[t],"draggable");window.addEventListener("mouseup",n)});var g="rpgui-progress";c.__create_funcs.progress=function(e){c.add_class(e,g),a(e)},c.__set_funcs.progress=function(e,n){var t=c.get_child_with_class(e,"rpgui-progress-track"),s=c.get_child_with_class(t,"rpgui-progress-fill");c.get_child_with_class(e,"rpgui-progress-left-edge"),c.get_child_with_class(e,"rpgui-progress-right-edge");s.style.left="0px",s.style.width=100*n+"%"},c.on_load(function(){for(var e=document.getElementsByClassName(g),n=0;n<e.length;++n)c.create(e[n],"progress")});var v="rpgui-radio";c.__create_funcs.radio=function(e){c.add_class(e,v),i(e)},c.__set_funcs.radio=function(e,n){e.checked=n},c.__get_funcs.radio=function(e){return e.checked},c.on_load(function(){for(var e=document.getElementsByClassName(v),n=0;n<e.length;++n)c.create(e[n],"radio")});var h="rpgui-dropdown";c.__create_funcs.dropdown=function(e){c.add_class(e,h),r(e)},c.on_load(function(){for(var e=document.getElementsByClassName(h),n=0;n<e.length;++n)c.create(e[n],"dropdown")});var m="rpgui-list";c.__create_funcs.list=function(e){c.add_class(e,m),o(e)},c.on_load(function(){for(var e=document.getElementsByClassName(m),n=0;n<e.length;++n)c.create(e[n],"list")});var y="rpgui-slider";return c.__create_funcs.slider=function(e){c.add_class(e,y),d(e)},c.on_load(function(){for(var e=document.getElementsByClassName(y),n=0;n<e.length;++n)c.create(e[n],"slider")}),c.create_element=function(e){return e=document.createElement(e)},c.set_cursor=function(e,n){c.add_class(e,"rpgui-cursor-"+n)},c.prevent_drag=function(e){},c.copy_css=function(e,n){n.style.cssText=e.style.cssText},c.has_class=function(e,n){return(" "+e.className+" ").indexOf(" "+n+" ")>-1},c.add_class=function(e,n){c.has_class(e,n)||(e.className+=" "+n)},c.get_child_with_class=function(e,n){for(var t=0;t<e.childNodes.length;t++)if(c.has_class(e.childNodes[t],n))return e.childNodes[t]},c.remove_class=function(e,n){e.className=(" "+e.className+" ").replace(n,""),e.className=e.className.substring(1,e.className.length-1)},c.fire_event=function(e,n){if("createEvent"in document){var t=document.createEvent("HTMLEvents");t.initEvent(n,!1,!0),e.dispatchEvent(t)}else e.fireEvent("on"+n)},c.copy_event_listeners=function(e,n){if("function"==typeof getEventListeners){var t=getEventListeners(e);for(var s in t)t[s].forEach(function(e){n.addEventListener(s,e.listener,e.useCapture)})}for(attr in e)0===attr.indexOf("on")&&(n[attr]=e[attr])},c.insert_after=function(e,n){n.parentNode.insertBefore(e,n.nextSibling)},c}();