diff --git a/dist/basicContext.min.js b/dist/basicContext.min.js index fedf67a..94bb87b 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,e){"undefined"!=typeof module&&module.exports?module.exports=e():"function"==typeof define&&define.amd?define(e):window[t]=e()}("basicContext",function(){var t=null,e=null,n="item",i="separator",o=function(){var t=arguments.length<=0||void 0===arguments[0]?"":arguments[0];return document.querySelector(".basicContext "+t)},l=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=0===Object.keys(t).length;return e===!0&&(t.type=i),null==t.type&&(t.type=n),null==t["class"]&&(t["class"]=""),t.visible!==!1&&(t.visible=!0),null==t.icon&&(t.icon=null),null==t.title&&(t.title="Undefined"),t.disabled!==!0&&(t.disabled=!1),t.disabled===!0&&(t["class"]+=" basicContext__item--disabled"),!0},r=function(t,e){var o="",r="";return l(t)===!1?"":t.visible===!1?"":(t.num=e,null!==t.icon&&(r=""),t.type===n?o=""+r+t.title+"":t.type===i&&(o=""),o)},c=function(t){var e="";return e+="
",t.forEach(function(t,n){return e+=r(t,n)}),e+="
"},u=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e={x:t.clientX,y:t.clientY};if("touchend"===t.type&&(null==e.x||null==e.y)){var n=t.changedTouches;null!=n&&n.length>0&&(e.x=n[0].clientX,e.y=n[0].clientY)}return(null==e.x||e.x<0)&&(e.x=0),(null==e.y||e.y<0)&&(e.y=0),e},a=function(t,e){var n=u(t),i=n.x,o=n.y,l={width:window.innerWidth,height:window.innerHeight},r={width:e.offsetWidth,height:e.offsetHeight};i+r.width>l.width&&(i-=i+r.width-l.width),o+r.height>l.height&&(o-=o+r.height-l.height),r.height>l.height&&(o=0,e.classList.add("basicContext--scrollable"));var c=n.x-i,a=n.y-o;return{x:i,y:o,rx:c,ry:a}},s=function(t,e){if(void 0===t&&(t={}),t.visible===!1)return!1;if(t.disabled===!0)return!1;var n=function(n){"function"==typeof t.fn&&t.fn(n),v("basicContext:click",{clickevent:n,item:t,i:e})};return o("td[data-num='"+t.num+"']").onclick=n,o("td[data-num='"+t.num+"']").oncontextmenu=n,!0},d=function(n,i,l,r){e=i.target;var u=c(n);document.body.insertAdjacentHTML("beforeend",u),null==t&&(t=document.body.style.overflow,document.body.style.overflow="hidden");var d=o(),f=a(i,d);return d.style.left=f.x+"px",d.style.top=f.y+"px",d.style.transformOrigin=f.rx+"px "+f.ry+"px",d.style.opacity=1,null==l&&(l=h),d.parentElement.onclick=l,d.parentElement.oncontextmenu=l,n.forEach(s),"function"==typeof i.preventDefault&&i.preventDefault(),"function"==typeof i.stopPropagation&&i.stopPropagation(),"function"==typeof r&&r(),v("basicContext:show"),!0},f=function(){var t=o();return null!=t&&0!==t.length},h=function(){if(f()===!1)return!1;var e=document.querySelector(".basicContextContainer");return e.parentElement.removeChild(e),null!=t&&(document.body.style.overflow=t,t=null),v("basicContext:close"),!0},v=function(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],i=void 0;return"function"==typeof document.CustomEvent?i=new CustomEvent(t,{detail:n,bubbles:!0}):(i=document.createEvent("CustomEvent"),i.initCustomEvent(t,!0,!0,n)),e.dispatchEvent(i)};return{ITEM:n,SEPARATOR:i,show:d,visible:f,close:h}}); \ No newline at end of file diff --git a/dist/basiccontext.js b/dist/basiccontext.js new file mode 100644 index 0000000..6329f8e --- /dev/null +++ b/dist/basiccontext.js @@ -0,0 +1 @@ +"use strict";!function(t,e){"undefined"!=typeof module&&module.exports?module.exports=e():"function"==typeof define&&define.amd?define(e):window[t]=e()}("basicContext",function(){var t=null,e=null,n="item",i="separator",o=function(){var t=arguments.length<=0||void 0===arguments[0]?"":arguments[0];return document.querySelector(".basicContext "+t)},l=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e=0===Object.keys(t).length;return e===!0&&(t.type=i),null==t.type&&(t.type=n),null==t["class"]&&(t["class"]=""),t.visible!==!1&&(t.visible=!0),null==t.icon&&(t.icon=null),null==t.title&&(t.title="Undefined"),t.disabled!==!0&&(t.disabled=!1),t.disabled===!0&&(t["class"]+=" basicContext__item--disabled"),!0},c=function(t,e){var o="",c="";return l(t)===!1?"":t.visible===!1?"":(t.num=e,null!==t.icon&&(c=""),t.type===n?o=""+c+t.title+"":t.type===i&&(o=""),o)},r=function(t){var e="";return e+="
",t.forEach(function(t,n){return e+=c(t,n)}),e+="
"},a=function(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],e={x:t.clientX,y:t.clientY};if("touchend"===t.type&&(null==e.x||null==e.y)){var n=t.changedTouches;null!=n&&n.length>0&&(e.x=n[0].clientX,e.y=n[0].clientY)}return(null==e.x||e.x<0)&&(e.x=0),(null==e.y||e.y<0)&&(e.y=0),e},u=function(t,e){var n=a(t),i=n.x,o=n.y,l={width:window.innerWidth,height:window.innerHeight},c={width:e.offsetWidth,height:e.offsetHeight};i+c.width>l.width&&(i-=i+c.width-l.width),o+c.height>l.height&&(o-=o+c.height-l.height),c.height>l.height&&(o=0,e.classList.add("basicContext--scrollable"));var r=n.x-i,u=n.y-o;return{x:i,y:o,rx:r,ry:u}},s=function(t,e){if(void 0===t&&(t={}),t.visible===!1)return!1;if(t.disabled===!0)return!1;var n=function(n){"function"==typeof t.fn&&t.fn(n),v("basicContext:click",{clickevent:n,item:t,i:e})};return o("td[data-num='"+t.num+"']").onclick=n,o("td[data-num='"+t.num+"']").oncontextmenu=n,!0},d=function(n,i,l,c){e=i.target;var a=r(n);document.body.insertAdjacentHTML("beforeend",a),null==t&&(t=document.body.style.overflow,document.body.style.overflow="hidden");var d=o(),f=u(i,d);return d.style.left=f.x+"px",d.style.top=f.y+"px",d.style.transformOrigin=f.rx+"px "+f.ry+"px",d.style.opacity=1,null==l&&(l=h),d.parentElement.onclick=l,d.parentElement.oncontextmenu=l,n.forEach(s),"function"==typeof i.preventDefault&&i.preventDefault(),"function"==typeof i.stopPropagation&&i.stopPropagation(),"function"==typeof c&&c(),v("basicContext:show"),!0},f=function(){var t=o();return null!=t&&0!==t.length},h=function(){if(f()===!1)return!1;var e=document.querySelector(".basicContextContainer");return e.parentElement.removeChild(e),null!=t&&(document.body.style.overflow=t,t=null),v("basicContext:close"),!0},v=function(t){var n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],i=void 0;"function"==typeof document.CustomEvent?i=new CustomEvent(t,{detail:n,bubbles:!0}):(i=document.createEvent("CustomEvent"),i.initEvent(t,!0,!0,n)),e.dispatchEvent(i)};return{ITEM:n,SEPARATOR:i,show:d,visible:f,close:h}}); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 790721d..88cc554 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,6 +2,7 @@ var name = require('./package.json').moduleName, fs = require('fs'), gulp = require('gulp'), plugins = require('gulp-load-plugins')() + replace = require('gulp-replace') var head = fs.readFileSync('./node_modules/@electerious/modulizer/head.js', { encoding: 'utf8' }), foot = fs.readFileSync('./node_modules/@electerious/modulizer/foot.js', { encoding: 'utf8' }) @@ -49,6 +50,7 @@ gulp.task('scripts', function() { .pipe(plugins.babel()) .on('error', catchError) .pipe(plugins.concat(name + '.min.js', { newLine: "\n" })) + .pipe(replace(/\\n\\t\\*t* +/g,'')) .pipe(plugins.uglify()) .on('error', catchError) .pipe(gulp.dest('./dist')) diff --git a/package.json b/package.json index 8166f81..ab021de 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "gulp-load-plugins": "^1.2.0", "gulp-minify-css": "^1.2.3", "gulp-rename": "^1.2.2", + "gulp-replace": "^0.5.4", "gulp-sass": "^2.1.1", "gulp-uglify": "^1.5.1", "gulp-util": "^3.0.7" diff --git a/src/scripts/basicContext.js b/src/scripts/basicContext.js index 9ff2b7b..f1cb2d0 100644 --- a/src/scripts/basicContext.js +++ b/src/scripts/basicContext.js @@ -1,4 +1,5 @@ -let overflow = null +let overflow = null, + target = null const ITEM = 'item', SEPARATOR = 'separator' @@ -24,15 +25,6 @@ const valid = function(item = {}) { if (item.disabled!==true) item.disabled = false if (item.disabled===true) item.class += ' basicContext__item--disabled' - // Item requires a function when - // it's not a separator and not disabled - if (item.fn==null && item.type!==SEPARATOR && item.disabled===false) { - - console.warn(`Missing fn for item '${ item.title }'`) - return false - - } - return true } @@ -168,20 +160,26 @@ const getPosition = function(e, context) { } -const bind = function(item = {}) { +const bind = function(item = {}, index) { - if (item.fn==null) return false if (item.visible===false) return false if (item.disabled===true) return false - dom(`td[data-num='${ item.num }']`).onclick = item.fn - dom(`td[data-num='${ item.num }']`).oncontextmenu = item.fn + let fn = function(e) { + if (typeof item.fn === 'function') item.fn(e) + triggerEvent('basicContext:click',{clickevent: e, item: item, i: index}) + } + + dom(`td[data-num='${ item.num }']`).onclick = fn + dom(`td[data-num='${ item.num }']`).oncontextmenu = fn return true } const show = function(items, e, fnClose, fnCallback) { + // Save target + target = e.target // Build context let html = build(items) @@ -224,6 +222,8 @@ const show = function(items, e, fnClose, fnCallback) { // Call callback when a function if (typeof fnCallback === 'function') fnCallback() + triggerEvent('basicContext:show') + return true } @@ -251,10 +251,23 @@ const close = function() { overflow = null } + triggerEvent('basicContext:close') + return true } +const triggerEvent = function(name, data = {}) { + let e; + if (typeof document.CustomEvent === "function") { + e = new CustomEvent(name, {detail: data, bubbles: true}); + } else { + e = document.createEvent("CustomEvent"); + e.initCustomEvent(name, true, true, data); + } + return target.dispatchEvent(e); +} + return { ITEM, SEPARATOR,