Skip to content

Commit

Permalink
Merge pull request #2 from tjrafferty/dev
Browse files Browse the repository at this point in the history
Custom CookieName Option
  • Loading branch information
yasmuru authored Jan 25, 2017
2 parents 02dfd13 + 31a8eb2 commit 8c7cd32
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Include YsExitPopup css and js files.
var options =
{
debug: true,
cookieName: 'subscribe-popup'
cookieValidity: 3,
delay: 5
};
Expand All @@ -38,6 +39,7 @@ $('.test2').ysExit(options);

Option | Description | Default
-------|-------------|--------
`cookieName`|Name of the cookie|ysExit
`cookieValidity`|Number of days to remember if the popup was closed|1
`closeOnOutsideClick`|If true the popup will close when clicked outside|true
`delay`|Delay in ms until the popup is registered|0
Expand Down
5 changes: 1 addition & 4 deletions popup/js/jquery.min.js

Large diffs are not rendered by default.

91 changes: 47 additions & 44 deletions popup/js/ysExit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function($) {
(function ($) {
"use strict";
$.fn.ysExit = function(o) {

$.fn.ysExit = function (o) {
var $self = this;
var defaults = {
width: '40%', //popup width
Expand All @@ -10,169 +10,172 @@
cookieValidity: 1, //days
closeOnOutsideClick: true, //close popup if user clicks outside
delay: 0, //delay in ms until the popup is registered
debug: false //if true, the cookie will not be set
debug: false, //if true, the cookie will not be set
cookieName: 'ysExit'
},

content = insertContent(),
options = fixOptions(o),
element = document.querySelector(options.target),
layer = document.querySelector('.ys-layer'),
layer = document.querySelector('.ys-layer'),
closeBt = document.querySelector(options.target + ' .ys-popup-close'),
inner = document.querySelector(options.target + ' .ys-box'),
exitBt = document.querySelector(options.target + ' .ys-exit'),

//cookies management helper
//cookies management helper
cookies = {
set: function(name, value, days) {
set: function (name, value, days) {
var components = [name + '=' + value];

if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 3600 * 1000));
components.push('expires=' + date.toGMTString());
}

components.push('path=/');

document.cookie = components.join('; ');
},
get: function(name) {
get: function (name) {
var start = name + '=',
arr = document.cookie.split(';'),
i;

for(i = 0; i < arr.length; i++) {
for (i = 0; i < arr.length; i++) {
var item = arr[i].trim();
if (item.indexOf(start) === 0){

if (item.indexOf(start) === 0) {
return item.substring(start.length);
}
}

return null;
}
},
//the popup object
//the popup object
pop = {
active: false,
mouseLeftWindow: function(e) {
mouseLeftWindow: function (e) {
var from, to;

e = e ? e : window.event;
from = e.relatedTarget || e.toElement;
if (!from || from.nodeName === "HTML") {
pop.open();
}
},
setDimension: function(dimension, value) {
setDimension: function (dimension, value) {
if (value.toString().indexOf('%') === -1) {
value = value + 'px';
}

inner.style[dimension] = value;
},
attachEvents: function() {
attachEvents: function () {
function close(e) {
pop.destroy();
e.preventDefault();
}

document.addEventListener('mouseout', pop.mouseLeftWindow, false);
closeBt.addEventListener('click', close);
if(exitBt){
if (exitBt) {
exitBt.addEventListener('click', close);
}

if (options.closeOnOutsideClick) {
element.addEventListener('click', close);
inner.addEventListener('click', function(e) {
inner.addEventListener('click', function (e) {
e.stopPropagation();
});
}

this.active = true;
},
detachEvents: function() {
detachEvents: function () {
document.removeEventListener('mouseout', pop.mouseLeftWindow);
},
open: function() {
open: function () {
var self = this;

element.classList.add('visible');
layer.classList.add('visible');
self.detachEvents();

setTimeout(function() {
setTimeout(function () {
self.setDimension('width', options.width);
self.setDimension('height', 'auto');
}, 20);

setTimeout(function() {
setTimeout(function () {
element.classList.add('finished');
}, 200);
},
destroy: function() {
destroy: function () {
if (this.active) {
pop.detachEvents();

setTimeout(function () {
element.parentNode.removeChild(element);
layer.classList.remove('visible');
}, 200);

if (!options.debug) {
cookies.set('ysExit', 1, options.cookieValidity);
cookies.set(options.cookieName, 1, options.cookieValidity);
}
}
}
};

function insertContent() {

// console.log($self);
var body = $('body').append('<div class="ys-layer"></div> <div class="ys-container" id="ys-container"><div class="ys-box"><a class="ys-popup-close" href="#">x</a><div class="ys-popup-content"></div></div></div>');
$('.ys-popup-content').append($self[0].outerHTML);
$self.hide();
return true;
}

//helper functions
function fixOptions(options) {
var newOptions = {};
Object.keys(defaults).forEach(function(key) {

Object.keys(defaults).forEach(function (key) {
newOptions[key] = options.hasOwnProperty(key) ? options[key] : defaults[key];
});

return newOptions;
}

function delegate(obj, func) {
return function() {
return function () {
func.apply(obj, arguments);
};
}

//start logic
if (!cookies.get('ysExit')) {
if (!cookies.get(options.cookieName)) {
if (typeof options.delay !== 'number') {
throw new Error('options.delay must be a numeric value');
}

if (!element) {
throw new Error('Could not find element with selector: ' + options.target);
}

if (element.parentNode !== document.body) {
throw new Error(options.target + ' element must be placed directly inside of the <body> element');
}

setTimeout(delegate(pop, pop.attachEvents), options.delay);
}

//return object with public interface
return {
open: delegate(pop, pop.open),
destroy: delegate(pop, pop.destroy),
getElement: function() {
getElement: function () {
return element;
}
};
Expand Down
2 changes: 1 addition & 1 deletion popup/js/ysExit.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c7cd32

Please sign in to comment.