|
;(function ($, window, document, undefined) { |
|
|
|
var plugin_name = 'xd_session_cookie', |
|
defaults = { |
|
session_endpoint: undefined, // e.g. "//auth.example.com" |
|
session_action: undefined, // e.g. "iframe_session" |
|
form_request_timeout: 2000 |
|
}; |
|
|
|
function Plugin(element, options) { |
|
this.$element = $(element); |
|
this.options = $.extend({}, defaults, options); |
|
|
|
this._defaults = defaults; |
|
this._name = plugin_name; |
|
|
|
this.init(); |
|
} |
|
|
|
Plugin.prototype.init = function () { |
|
this.$session_iframe = undefined; // <iframe> |
|
this.$session_form = undefined; // <form> |
|
}; |
|
|
|
// GET /iframe_session |
|
Plugin.prototype.createSessionIframe = function () { |
|
var _class = [this.plugin_name, 'iframe'].join('-'), |
|
_id = [_class, this.options.session_endpoint.replace(/[\W]/g, '_')].join('_'), |
|
attrs = { |
|
'class': _class, |
|
'id': _id, |
|
'name': _id, |
|
'src': [this.options.session_endpoint, this.options.session_action].join('/'), |
|
'data-session-endpoint': this.options.session_endpoint, |
|
'style': 'display:none;' |
|
}; |
|
|
|
this.$iframe_session_iframe = $('<iframe />', attrs); |
|
}; |
|
|
|
// POST /iframe_session |
|
Plugin.prototype.createSessionForm = function () { |
|
var _class = [this.plugin_name, 'form'].join('-'), |
|
_id = [_class, this.options.session_endpoint.replace(/[\W]/g, '_')].join('_'), |
|
attrs = { |
|
'class': _class, |
|
'id': _id, |
|
'name': _id, |
|
'enctype': 'application/x-www-form-urlencoded', |
|
'action': [this.options.session_endpoint, this.options.session_action].join('/'), |
|
'data-domain': this.options.session_endpoint, |
|
'data-session-created': false, |
|
'style': 'display:none;' |
|
}; |
|
|
|
this.$iframe_session_form = $('<form />', attrs); |
|
}; |
|
|
|
Plugin.prototype.loadSessionIframe = function () { |
|
|
|
}; |
|
|
|
Plugin.prototype.submitSessionForm = function () { |
|
var session_already_created = /(true|1)/.test($session_form.attr('data-session-created')); |
|
|
|
if (session_already_created) { |
|
// skip |
|
} else { |
|
this.$session_form |
|
.attr('data-session-created', true) |
|
.submit(function(argument) { |
|
// NOTE: Function.prototype.bind is ECMAScript 5 - won't work in older browsers. |
|
setTimeout(this.onSessionFormSubmitted.bind(this.options.session_endpoint), this.options.form_request_timeout); |
|
return true; |
|
}); |
|
} |
|
}; |
|
|
|
Plugin.prototype.onSessionFormSubmitted = function () { |
|
|
|
}; |
|
|
|
// A really lightweight plugin wrapper around the constructor, |
|
// preventing against multiple instantiations |
|
$.fn[plugin_name] = function (options) { |
|
return this.each(function () { |
|
if (!$.data(this, 'plugin_' + plugin_name)) { |
|
$.data(this, 'plugin_' + plugin_name, new Plugin(this, options)); |
|
} |
|
}); |
|
}; |
|
|
|
})(jQuery, window, document); |
can you show a simple example of usage with iframe, please