Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use "local" cookie store if cookies disabled #56

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 82 additions & 78 deletions src/controller/projekktor.plugininterface.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* this file is part of:
* this file is part of:
* projekktor zwei
* http://www.projekktor.com
*
Expand All @@ -10,19 +10,20 @@
var projekktorPluginInterface = function(){};
jQuery(function($) {
projekktorPluginInterface.prototype = {

pluginReady: false,
reqVer: null,
name: '',
pp: {},
config: {},
playerDom: null,

_appliedDOMObj: [],
_pageDOMContainer: {},
_childDOMContainer: {},

_init: function(pluginConfig) {
_localStore: {},

_init: function(pluginConfig) {
this.config = $.extend(true, this.config, pluginConfig);
if (this.reqVer!=null) {
if (!$p.utils.versionCompare(this.pp.getPlayerVer(), this.reqVer)) {
Expand All @@ -33,55 +34,55 @@ projekktorPluginInterface.prototype = {
}
this.initialize();
},
getConfig: function(idx, defaultValue) {

getConfig: function(idx, defaultValue) {
var result = null,
def = defaultValue || null;

if (this.pp.getConfig('plugin_'+this.name)!=null) {
result = this.pp.getConfig('plugin_'+this.name)[idx];
}

if (result==null) {
result = this.pp.getConfig(idx);
}

if (result==null) {
result = this.config[idx];
}

if (typeof result == 'object' && result.length === null)
result = $.extend(true, {}, result, this.config[idx]);
else if (typeof result == 'object') {
result = $.extend(true, [], this.config[idx] || [], result || [] );
}

return (result==null) ? def : result;
},

getDA: function(name) {
return 'data-' + this.pp.getNS() + '-' + this.name + '-' + name;
return 'data-' + this.pp.getNS() + '-' + this.name + '-' + name;
},

getCN: function(name) {
return this.pp.getNS() + name;
},
return this.pp.getNS() + name;
},

sendEvent: function(eventName, data) {
this.pp._promote({_plugin:this.name, _event:eventName}, data);
},

deconstruct: function() {
this.pluginReady = false;
$.each(this._appliedDOMObj, function() {
$(this).unbind();
$(this).unbind();
});
},

/**
* applies a new dom element to the player in case it is not yet present
* also transparently applies the cssclass prefix as configured
*
*
* @private
* @element (Object) the element
* @fu (String) function, default 'container'
Expand All @@ -94,24 +95,24 @@ projekktorPluginInterface.prototype = {
var func = fu || 'container',
tmpClass = '',
ref = this;

try {tmpClass = element.attr("class") || this.name} catch(e){tmpClass = this.name;}

this._pageDOMContainer[func] = $( "["+this.getDA('host')+"='" + this.pp.getId() + "']["+this.getDA('func')+"='"+func+"']" );
this._childDOMContainer[func] = this.playerDom.find("[" + this.getDA('func') + "='" + func + "'],." + this.getCN(tmpClass) + ":not([" +this.getDA('func') +"=''])");

// check if this element aleady exists somewhere on page
// check if this element aleady exists somewhere on page
if ( this._pageDOMContainer[func].length > 0 ) {
this._pageDOMContainer[func].removeClass('active').addClass('inactive');

$.each(this._pageDOMContainer[func], function() {
ref._appliedDOMObj.push($(this));
});

return this._pageDOMContainer[func];
}
// add new DOM container to the player

// add new DOM container to the player
if (this._childDOMContainer[func].length==0) {
element
.removeClass(tmpClass)
Expand All @@ -120,25 +121,25 @@ projekktorPluginInterface.prototype = {
.addClass('inactive')
.attr(this.getDA('func'), func)
.appendTo(this.playerDom);

this._childDOMContainer[func] = element;
this._appliedDOMObj.push(element);
if (visible===true) {
element.addClass('active').removeClass('inactive');
}

return element;
} else {
} else {
$.each(this._childDOMContainer[func], function() {
$(this).attr(ref.getDA('func'), func)
ref._appliedDOMObj.push($(this));
});
}

if (visible===true) {
this._childDOMContainer[func].addClass('active').removeClass('inactive');
}

return $(this._childDOMContainer[func][0]);
},

Expand All @@ -151,40 +152,40 @@ projekktorPluginInterface.prototype = {
$(this._childDOMContainer['container']).removeClass('active').addClass('inactive');
this.sendEvent('inactive', $.extend(true, {}, this._pageDOMContainer['container'], this._childDOMContainer['container']));
},

setActive: function(elm, on) {
var dest = (typeof elm =='object') ? elm : this.getElement(elm);

if (elm==null) {
this._pageDOMContainer['container'].removeClass('inactive').addClass('active');
this._childDOMContainer['container'].removeClass('inactive').addClass('active');
this.sendEvent('active', $.extend(true, {}, this._pageDOMContainer['container'], this._childDOMContainer['container']));
return dest;
}

if (on!=false) {
dest.addClass('active').removeClass('inactive');
}
else {
dest.addClass('inactive').removeClass('active');
}

dest.css('display', '');

return dest;
},
getActive: function(elm) {

getActive: function(elm) {
return $(elm).hasClass('active');
},
// triggered on plugin-instanciation
},

// triggered on plugin-instanciation
initialize: function() {},

isReady: function() {
return this.pluginReady;
},

clickHandler: function(what) {
try {
this.pp[this.getConfig(what+'Click').callback](this.getConfig(what+'Click').value);
Expand All @@ -193,64 +194,67 @@ projekktorPluginInterface.prototype = {
this.getConfig(what+'Click')(this.getConfig(what+'Click').value);
} catch(e){}
}
return false;
return false;
},

cookie: function (key, value, ttl) {
if (document.cookie===undefined || document.cookie===false) return null;
var useLocal = (document.cookie===undefined || document.cookie===false || this.pp.getConfig('cookieExpiry')==0);
if (key==null && value!=null) return null;
if (this.pp.getConfig('cookieExpiry')==0) return null;

var t = new Date(),
result = null,
cookieString = '',
tmp = storedData = jQuery.parseJSON(eval(result = new RegExp('(?:^|; )' + encodeURIComponent(this.getConfig('cookieName')+"_"+this.name) + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null);
tmp = storedData = useLocal ? this._localStore[key] : jQuery.parseJSON(eval(result = new RegExp('(?:^|; )' + encodeURIComponent(this.getConfig('cookieName')+"_"+this.name) + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null);

if (typeof storedData!='object' || storedData==null) {
storedData = {};
if (key!=null)
storedData[key] = tmp;
}

// read cookie
if (key==null) {
return storedData;
}

if (arguments.length==1) {
return storedData[key];
}

if (value!=null) {
storedData[key] = value;
}
else {
delete storedData[key];
}

if ($.isEmptyObject(storedData)) {
ttl=0;
storedData = '';
}
else {
storedData = $p.utils.stringify(storedData)
}

// set cookie:
t.setDate(t.getDate() + (ttl || this.getConfig('cookieExpiry', 0)));

cookieString = encodeURIComponent(this.getConfig('cookieName', 'projekktor')+"_"+this.name)+'='
+encodeURIComponent(storedData)
+'; expires=' + ((ttl==false) ? "Thu, 01 Jan 1970 00:00:01 GMT" : t.toUTCString())

if (this.getConfig('cookieDomain', false)) {
cookieString += '; domain=' + options.domain;
}

document.cookie = cookieString;
if (useLocal) {
this._localStore[key] = storedData;
} else {
if (value!=null) {
storedData[key] = value;
}
else {
delete storedData[key];
}

if ($.isEmptyObject(storedData)) {
ttl=0;
storedData = '';
}
else {
storedData = $p.utils.stringify(storedData)
}

// set cookie:
t.setDate(t.getDate() + (ttl || this.getConfig('cookieExpiry', 0)));

cookieString = encodeURIComponent(this.getConfig('cookieName', 'projekktor')+"_"+this.name)+'='
+encodeURIComponent(storedData)
+'; expires=' + ((ttl==false) ? "Thu, 01 Jan 1970 00:00:01 GMT" : t.toUTCString())

if (this.getConfig('cookieDomain', false)) {
cookieString += '; domain=' + options.domain;
}

document.cookie = cookieString;
}
return value;
},

// important
eventHandler: function() {}
}
Expand Down