diff --git a/dist/basicContext.min.js b/dist/basicContext.min.js index fedf67a..e6b21f2 100644 --- a/dist/basicContext.min.js +++ b/dist/basicContext.min.js @@ -1 +1 @@ -"use strict";!function(n,t){"undefined"!=typeof module&&module.exports?module.exports=t():"function"==typeof define&&define.amd?define(t):window[n]=t()}("basicContext",function(){var n=null,t="item",e="separator",i=function(){var n=arguments.length<=0||void 0===arguments[0]?"":arguments[0];return document.querySelector(".basicContext "+n)},l=function(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],i=0===Object.keys(n).length?!0:!1;return i===!0&&(n.type=e),null==n.type&&(n.type=t),null==n["class"]&&(n["class"]=""),n.visible!==!1&&(n.visible=!0),null==n.icon&&(n.icon=null),null==n.title&&(n.title="Undefined"),n.disabled!==!0&&(n.disabled=!1),n.disabled===!0&&(n["class"]+=" basicContext__item--disabled"),null==n.fn&&n.type!==e&&n.disabled===!1?(console.warn("Missing fn for item '"+n.title+"'"),!1):!0},o=function(n,i){var o="",r="";return l(n)===!1?"":n.visible===!1?"":(n.num=i,null!==n.icon&&(r=""),n.type===t?o="\n \n "+r+n.title+"\n \n ":n.type===e&&(o="\n \n "),o)},r=function(n){var t="";return t+="\n
\n
\n \n \n ",n.forEach(function(n,e){return t+=o(n,e)}),t+="\n \n
\n
\n
\n "},a=function(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],t={x:n.clientX,y:n.clientY};if("touchend"===n.type&&(null==t.x||null==t.y)){var e=n.changedTouches;null!=e&&e.length>0&&(t.x=e[0].clientX,t.y=e[0].clientY)}return(null==t.x||t.x<0)&&(t.x=0),(null==t.y||t.y<0)&&(t.y=0),t},s=function(n,t){var e=a(n),i=e.x,l=e.y,o={width:window.innerWidth,height:window.innerHeight},r={width:t.offsetWidth,height:t.offsetHeight};i+r.width>o.width&&(i-=i+r.width-o.width),l+r.height>o.height&&(l-=l+r.height-o.height),r.height>o.height&&(l=0,t.classList.add("basicContext--scrollable"));var s=e.x-i,u=e.y-l;return{x:i,y:l,rx:s,ry:u}},u=function(){var n=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return null==n.fn?!1:n.visible===!1?!1:n.disabled===!0?!1:(i("td[data-num='"+n.num+"']").onclick=n.fn,i("td[data-num='"+n.num+"']").oncontextmenu=n.fn,!0)},c=function(t,e,l,o){var a=r(t);document.body.insertAdjacentHTML("beforeend",a),null==n&&(n=document.body.style.overflow,document.body.style.overflow="hidden");var c=i(),d=s(e,c);return c.style.left=d.x+"px",c.style.top=d.y+"px",c.style.transformOrigin=d.rx+"px "+d.ry+"px",c.style.opacity=1,null==l&&(l=f),c.parentElement.onclick=l,c.parentElement.oncontextmenu=l,t.forEach(u),"function"==typeof e.preventDefault&&e.preventDefault(),"function"==typeof e.stopPropagation&&e.stopPropagation(),"function"==typeof o&&o(),!0},d=function(){var n=i();return null==n||0===n.length?!1:!0},f=function(){if(d()===!1)return!1;var t=document.querySelector(".basicContextContainer");return t.parentElement.removeChild(t),null!=n&&(document.body.style.overflow=n,n=null),!0};return{ITEM:t,SEPARATOR:e,show:c,visible:d,close:f}}); \ No newline at end of file +"use strict";!function(t,n){"undefined"!=typeof module&&module.exports?module.exports=n():"function"==typeof define&&define.amd?define(n):window[t]=n()}("basicContext",function(){var t="item",n="separator",e=function(){var t=arguments.length<=0||void 0===arguments[0]?"":arguments[0];return document.querySelector(".basicContext "+t)},i=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],i=0===Object.keys(e).length;return i===!0&&(e.type=n),null==e.type&&(e.type=t),null==e["class"]&&(e["class"]=""),e.visible!==!1&&(e.visible=!0),null==e.icon&&(e.icon=null),null==e.title&&(e.title="Undefined"),e.disabled!==!0&&(e.disabled=!1),e.disabled===!0&&(e["class"]+=" basicContext__item--disabled"),null!=e.fn||e.type===n||e.disabled!==!1||(console.warn("Missing fn for item '"+e.title+"'"),!1)},l=function(e,l){var o="",r="";return i(e)===!1?"":e.visible===!1?"":(e.num=l,null!==e.icon&&(r=""),e.type===t?o="\n\t\t \n\t\t "+r+e.title+"\n\t\t \n\t\t ":e.type===n&&(o="\n\t\t \n\t\t "),o)},o=function(t){var n="";return n+="\n\t\t\t
\n\t \n\t \n\t ",t.forEach(function(t,e){return n+=l(t,e)}),n+="\n\t\t\t\t\t\n
\n
\n\t "},r=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],n={x:t.clientX,y:t.clientY};if("touchend"===t.type&&(null==n.x||null==n.y)){var e=t.changedTouches;null!=e&&e.length>0&&(n.x=e[0].clientX,n.y=e[0].clientY)}return(null==n.x||n.x<0)&&(n.x=0),(null==n.y||n.y<0)&&(n.y=0),n},a=function(t,n){var e=r(t),i=e.x,l=e.y,o={width:window.innerWidth,height:window.innerHeight},a={width:n.offsetWidth,height:n.offsetHeight};i+a.width>o.width&&(i-=i+a.width-o.width),l+a.height>o.height&&(l-=l+a.height-o.height),a.height>o.height&&(l=0,n.classList.add("basicContext--scrollable"));var c=e.x-i,s=e.y-l;return{x:i,y:l,rx:c,ry:s}},c=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return null!=t.fn&&(t.visible!==!1&&(t.disabled!==!0&&(e("td[data-num='"+t.num+"']").onclick=t.fn,e("td[data-num='"+t.num+"']").oncontextmenu=t.fn,!0)))},s=function(t,n,i,l){d();var r=o(t);document.body.insertAdjacentHTML("beforeend",r);var s=e(),u=a(n,s);return s.style.left=u.x+"px",s.style.top=u.y+"px",s.style.transformOrigin=u.rx+"px "+u.ry+"px",s.style.opacity=1,null==i&&(i=d),s.parentElement.onclick=i,s.parentElement.oncontextmenu=i,t.forEach(c),"function"==typeof n.preventDefault&&n.preventDefault(),"function"==typeof n.stopPropagation&&n.stopPropagation(),"function"==typeof l&&l(),!0},u=function(){var t=e();return null!=t&&0!==t.length},d=function(){var t=document.querySelector(".basicContext");return null!==t&&t.parentElement.removeChild(t),!0};return{ITEM:t,SEPARATOR:n,show:s,visible:u,close:d}}); \ No newline at end of file diff --git a/src/scripts/basicContext.js b/src/scripts/basicContext.js index 9ff2b7b..9bf0eb2 100644 --- a/src/scripts/basicContext.js +++ b/src/scripts/basicContext.js @@ -1,5 +1,3 @@ -let overflow = null - const ITEM = 'item', SEPARATOR = 'separator' @@ -80,19 +78,17 @@ const build = function(items) { let html = '' html += ` -
-
- - +
+
+ ` items.forEach((item, i) => html += buildItem(item, i)) html += ` - -
-
-
+ + + ` return html @@ -183,18 +179,15 @@ const bind = function(item = {}) { const show = function(items, e, fnClose, fnCallback) { + // Close any other open context menus + close() + // Build context let html = build(items) // Add context to the body document.body.insertAdjacentHTML('beforeend', html) - // Save current overflow and block scrolling of site - if (overflow==null) { - overflow = document.body.style.overflow - document.body.style.overflow = 'hidden' - } - // Cache the context let context = dom() @@ -210,7 +203,7 @@ const show = function(items, e, fnClose, fnCallback) { // Close fn fallback if (fnClose==null) fnClose = close - // Bind click on background + // Bind click on parent element context.parentElement.onclick = fnClose context.parentElement.oncontextmenu = fnClose @@ -239,17 +232,9 @@ const visible = function() { const close = function() { - if (visible()===false) return false + let container = document.querySelector('.basicContext') - let container = document.querySelector('.basicContextContainer') - - container.parentElement.removeChild(container) - - // Reset overflow to its original value - if (overflow!=null) { - document.body.style.overflow = overflow - overflow = null - } + if (container!==null) container.parentElement.removeChild(container) return true @@ -261,4 +246,4 @@ return { show, visible, close -} \ No newline at end of file +}