-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoldToDelete.min.js
More file actions
1 lines (1 loc) · 9.59 KB
/
HoldToDelete.min.js
File metadata and controls
1 lines (1 loc) · 9.59 KB
1
!function(e,t){HoldToDelete=function(e,t){this.mouseup_check=!1,this.reference_element=!1,this.mouse_click_mapping={left_click:0,middle_click:1,right_click:2},this.position_css_mapping={top:{"border-width":"border-top-width","border-left-radius":"border-top-left-radius","border-right-radius":"border-top-right-radius"},bottom:{"border-width":"border-bottom-width","border-left-radius":"border-bottom-left-radius","border-right-radius":"border-bottom-right-radius"}},this.classname_running="holdtodelete_running",this.dom_elements=this.getDomElements(e),this.options=this.getOptions(t),this.timeout_settings=this.getTimeoutSettings(),this.dom_elements&&(this.registerReferenceEvents(),this.wrapReferenceElements()),this.options.class_name&&this.setClassNames()},HoldToDelete.prototype.getDomElements=function(e){if(e&&"string"==typeof e)return this.selector(e,!1);if(e&&this.isDomElement(e)===!0)return e;throw new Error("No element was specified for HoldToDelete to use. Please specify one.")},HoldToDelete.prototype.getTimeoutSettings=function(){var e={};return e.seconds=1e3*this.options.seconds,e.callback_timeout=1e3*this.options.timeout,e},HoldToDelete.prototype.selector=function(e,o){var r=t.querySelectorAll(e);return o?r[0]:r},HoldToDelete.prototype.selectFeedbackElement=function(e){var t=e.nextSibling;return t&&t.className.indexOf("holdtodelete")>-1&&"I"==t.nodeName?t:!1},HoldToDelete.prototype.selectFeedbackWrapper=function(e){var t=e.parentNode;return t&&t.className.indexOf("holdtodelete_parent")>-1&&"DIV"==t.nodeName?t:!1},HoldToDelete.prototype.getOptions=function(e){var t={seconds:3,timeout:0,remove_feedback:!0,border_radius_percentage:!1,class_name:"",placement:"top"};if(e&&"object"==typeof e)for(var o in e){if(!e.hasOwnProperty(o)||!t.hasOwnProperty(o)||typeof t[o]!=typeof e[o])throw new Error(o+" was not specified correct. It should be the data type "+typeof t[o]);if("placement"===o&&this.validatePlacement(e[o])||"placement"!==o)t[o]=e[o];else if("placement"===o&&!this.validatePlacement(e[o]))throw new Error(o+" was not specified correct. It should be one of the following values: "+Object.keys(this.position_css_mapping))}return t},HoldToDelete.prototype.validatePlacement=function(e){for(var t in this.position_css_mapping)if(t===e)return!0;return!1},HoldToDelete.prototype.isDomElement=function(e){for(var t=0;t<e.length;t++)if("object"==typeof e[t]&&"nodeType"in e[t]&&1===e[t].nodeType&&e[t].cloneNode)return!0},HoldToDelete.prototype.wrapReferenceElements=function(){for(var e=0;e<this.dom_elements.length;e++){var o=t.createElement("div");o.className="holdtodelete_parent holdtodelete_"+this.options.placement;var r=this.dom_elements[e];r.parentNode.insertBefore(o,r),o.appendChild(r)}},HoldToDelete.prototype.browserSupportsTouchEvents=function(){return"ontouchstart"in t.documentElement},HoldToDelete.prototype.getSourceElement=function(e){return e.target||e.srcElement},HoldToDelete.prototype.registerReferenceEvents=function(){for(var e=0;e<this.dom_elements.length;e++){var o=this.dom_elements[e];this.browserSupportsTouchEvents()?(o.addEventListener("touchstart",function(e){this.mouseDownHandler(this.getSourceElement(e))}.bind(this)),o.addEventListener("touchend",function(e){this.mouseUpHandler(this.getSourceElement(e)),this.anim_timer||this.removeReferenceElement()}.bind(this)),o.addEventListener("touchmove",function(e){var o=t.elementFromPoint(e.changedTouches[0].clientX,e.changedTouches[0].clientY);o!=this.selectFeedbackElement(this.getSourceElement(e))&&o!=this.reference_element&&(this.mouseUpHandler(this.getSourceElement(e)),this.anim_timer||this.removeReferenceElement())}.bind(this))):(o.addEventListener("mousedown",function(e){this.isLeftClick(e)?this.mouseDownHandler(this.getSourceElement(e)):(this.mouseUpHandler(this.getSourceElement(e)),this.removeReferenceElement())}.bind(this)),o.addEventListener("mouseup",function(e){this.mouseUpHandler(this.getSourceElement(e)),this.anim_timer||this.removeReferenceElement()}.bind(this)),o.addEventListener("mouseout",function(e){e.toElement!=this.selectFeedbackElement(this.getSourceElement(e))&&(this.mouseUpHandler(this.getSourceElement(e)),this.anim_timer||this.removeReferenceElement())}.bind(this)))}},HoldToDelete.prototype.setClassNames=function(){for(var e=0;e<this.dom_elements.length;e++){var t=this.dom_elements[e];t.className+=" "+this.options.class_name}},HoldToDelete.prototype.isLeftClick=function(e){return"buttons"in e?1===e.buttons:"which"in e?1===e.which||3===e.which?!0:void 0:1===e.button},HoldToDelete.prototype.registerFeedbackEvents=function(e){e.addEventListener("mouseup",function(){this.mouseUpHandler(this.reference_element),this.anim_timer||this.removeReferenceElement()}.bind(this)),e.addEventListener("mouseout",function(e){e.toElement!=this.reference_element&&(this.mouseUpHandler(this.reference_element),this.removeReferenceElement())}.bind(this)),e.addEventListener("mousedown",function(e){this.isLeftClick(e)||(this.mouseUpHandler(this.reference_element),this.removeReferenceElement())}.bind(this))},HoldToDelete.prototype.mouseDownHandler=function(e){this.setMouseupCheck(!0),this.setReferenceElement(e),this.reference_element.className+=" "+this.classname_running;var t=this.getFeedbackObject(this.reference_element),o=this.insertFeedbackElement(t,e);this.registerFeedbackEvents(o),this.callEvent("holdtodelete_started");var r=(new Date).getTime();this.anim_timer=setInterval(function(){var e=Math.min(1,((new Date).getTime()-r)/this.timeout_settings.seconds);o.style.width=0+e*(t.width-0)+"px",this.callEvent("holdtodelete_running"),1===e&&(this.setMouseupCheck(!1),clearInterval(this.anim_timer),this.mouseup_check||(this.callEvent("holdtodelete_before_timeout"),this.callback_timer=setTimeout(function(){this.callEvent("holdtodelete_finished"),this.options.remove_feedback&&(this.setMouseupCheck(!0),this.mouseUpHandler(this.reference_element)),this.removeReferenceElement()}.bind(this),this.timeout_settings.callback_timeout)))}.bind(this),1)},HoldToDelete.prototype.mouseUpHandler=function(e){clearInterval(this.anim_timer),1==this.mouseup_check&&(e.className=e.className.replace(" "+this.classname_running,""),this.removeFeedbackElement(e))},HoldToDelete.prototype.setReferenceElement=function(e){this.reference_element=e},HoldToDelete.prototype.removeReferenceElement=function(){this.reference_element=!1},HoldToDelete.prototype.constructFeedbackElement=function(){var e=t.createElement("i");return e.className="holdtodelete",e},HoldToDelete.prototype.insertFeedbackElement=function(e){var t=this.constructFeedbackElement();return 0!=e.border_radius&&(e.hasOwnProperty("border_left_radius")&&(t.style.cssText+=this.getCSSPropertyForPlacement("border-left-radius")+": "+e.border_left_radius+"px;"),e.hasOwnProperty("border_right_radius")&&(t.style.cssText+=this.getCSSPropertyForPlacement("border-right-radius")+": "+e.border_right_radius+"px;"),e.hasOwnProperty("border_radius")&&0!=e.border_radius&&(t.style.cssText+=this.getCSSPropertyForPlacement("border-width")+": "+e.border_radius+"px;",this.setFeedbackWrapperBorderRadius(e))),e.clicked_element.parentNode.insertBefore(t,e.clicked_element.nextSibling),t},HoldToDelete.prototype.getFeedbackObject=function(e){var t={};return t.clicked_element=e,t.wrapper=this.selectFeedbackWrapper(e),t.width=t.wrapper.offsetWidth,window.getComputedStyle&&(t.border_left_radius=this.formatStringtoNumber(window.getComputedStyle(e,null).getPropertyValue(this.getCSSPropertyForPlacement("border-left-radius"))),t.border_right_radius=this.formatStringtoNumber(window.getComputedStyle(e,null).getPropertyValue(this.getCSSPropertyForPlacement("border-right-radius"))),t.border_radius=t.border_left_radius>=t.border_right_radius?this.getBorderRadiusHeight(t.border_left_radius):t.border_left_radius<t.border_right_radius?this.getBorderRadiusHeight(t.border_right_radius):0),t},HoldToDelete.prototype.getCSSPropertyForPlacement=function(e){return this.position_css_mapping.hasOwnProperty(this.options.placement)&&this.position_css_mapping[this.options.placement].hasOwnProperty(e)?(console.log(this.position_css_mapping[this.options.placement][e]),this.position_css_mapping[this.options.placement][e]):!1},HoldToDelete.prototype.formatStringtoNumber=function(e){return e=e.replace(/ /g,""),"px"===e.slice(-2)?e=e.slice(0,-2):"%"===e.slice(-1)&&(e=e.slice(0,-1)/100*this.reference_element.offsetWidth),Number(e)},HoldToDelete.prototype.getBorderRadiusHeight=function(e){if(this.options.border_radius_percentage){var t=e/this.reference_element.offsetWidth*100;return t/100*this.reference_element.offsetHeight}return e},HoldToDelete.prototype.setFeedbackWrapperBorderRadius=function(e){var t=this.selectFeedbackWrapper(this.reference_element);t.style.cssText+=this.getCSSPropertyForPlacement("border-left-radius")+": "+e.border_left_radius+"px;",t.style.cssText+=this.getCSSPropertyForPlacement("border-right-radius")+": "+e.border_right_radius+"px;"},HoldToDelete.prototype.removeFeedbackElement=function(e){var t=this.selectFeedbackElement(e);t&&t.parentNode.removeChild(t)},HoldToDelete.prototype.setMouseupCheck=function(e){"boolean"==typeof e&&(this.mouseup_check=e)},HoldToDelete.prototype.on=function(e,o){if(t.addEventListener)for(var r=0;r<this.dom_elements.length;r++){var n=this.dom_elements[r];n.addEventListener(e,function(){o(this.reference_element,this.selectFeedbackElement(this.reference_element))}.bind(this),!1)}},HoldToDelete.prototype.callEvent=function(e){var o=["holdtodelete_started","holdtodelete_running","holdtodelete_before_timeout","holdtodelete_finished"];if(t.createEvent&&"string"==typeof e&&o.indexOf(e)>-1){var r=t.createEvent("Event");r.initEvent(e,!0,!0);for(var n=0;n<this.dom_elements.length;n++){var i=this.dom_elements[n];i.dispatchEvent(r)}}},e.HoldToDelete=HoldToDelete}(window,window.document);