@zayesh/stay
Advanced tools
Comparing version 0.0.5 to 0.0.6
/** | ||
* stay build 07.07.2015 | ||
* stay build 08.07.2015 | ||
* | ||
@@ -158,3 +158,3 @@ * Copyright 2015 Raoul van Rueschen | ||
this.responseFields = ["content", "navigation"]; | ||
this.responseFields = ["main", "complementary", "contentinfo"]; | ||
this.infix = "/json"; | ||
@@ -181,3 +181,3 @@ this.timeoutPost = 60000; | ||
this.eventNavigate = {type: "navigate"}; | ||
this.eventContentReceived = {type: "contentreceived", response: null}; | ||
this.eventReceive = {type: "receive", response: null}; | ||
this.eventLoad = {type: "load"}; | ||
@@ -266,2 +266,32 @@ | ||
/** | ||
* Adds a response field. | ||
* | ||
* @param {string} field - The field to add. | ||
*/ | ||
Stay.prototype.addResponseField = function(field) | ||
{ | ||
if(this.responseFields.indexOf(field) === -1) | ||
{ | ||
this.responseFields.push(field); | ||
} | ||
}; | ||
/** | ||
* Removes a response field. | ||
* | ||
* @param {string} field - The field to remove. | ||
*/ | ||
Stay.prototype.removeResponseField = function(field) | ||
{ | ||
var i = this.responseFields.indexOf(field); | ||
if(i !== -1) | ||
{ | ||
this.responseFields.splice(i, 1); | ||
} | ||
}; | ||
/** | ||
* Navigates to the next target uri. | ||
@@ -376,4 +406,4 @@ * | ||
* Binds event listeners to all links and forms. | ||
* This method is combined with the cleanup and | ||
* basically refreshes the navigation listeners. | ||
* This method is combined with the cleanup and basically refreshes | ||
* the navigation listeners. | ||
*/ | ||
@@ -412,4 +442,4 @@ | ||
* behaviour can be disabled. It is then the responsibility of the | ||
* programmer to call update with the response data provided by the | ||
* "contentreceived" event. | ||
* programmer to call stay.update() with the response data provided | ||
* by the "receive" event. | ||
* | ||
@@ -438,3 +468,3 @@ * @param {object} response - The response to display. | ||
this.eventContentReceived.response = null; | ||
this.eventReceive.response = null; | ||
this.dispatchEvent(this.eventLoad); | ||
@@ -476,4 +506,4 @@ this.locked = false; | ||
response.url = xhr.responseURL; | ||
this.eventContentReceived.response = response; | ||
this.dispatchEvent(this.eventContentReceived); | ||
this.eventReceive.response = response; | ||
this.dispatchEvent(this.eventReceive); | ||
@@ -480,0 +510,0 @@ if(this.autoUpdate) |
/** | ||
* stay build 07.07.2015 | ||
* stay build 08.07.2015 | ||
* | ||
@@ -18,2 +18,2 @@ * Copyright 2015 Raoul van Rueschen | ||
*/ | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Stay=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(){this._listeners={}}d.prototype.addEventListener=function(a,b){void 0===this._listeners[a]&&(this._listeners[a]=[]),-1===this._listeners[a].indexOf(b)&&this._listeners[a].push(b)},d.prototype.hasEventListener=function(a,b){return void 0!==this._listeners[a]&&-1!==this._listeners[a].indexOf(b)},d.prototype.removeEventListener=function(a,b){var c,d=this._listeners,e=d[a];void 0!==e&&(c=e.indexOf(b),-1!==c&&e.splice(c,1))},d.prototype.dispatchEvent=function(a){var b,c,d=this._listeners,e=d[a.type];if(void 0!==e)for(a.target=this,b=0,c=e.length;c>b;++b)e[b].call(this,a)},b.exports=d},{}],2:[function(a,b,c){"use strict";function d(a){var b=document.createElement("a");return b.href=a,b}function Stay(a){var b=this;e.call(this),this.responseFields=["content","navigation"],this.infix="/json",this.timeoutPost=6e4,this.timeoutGet=5e3,this.autoUpdate=!0,void 0!==a&&(void 0!==a.responseFields&&(this.responseFields=a.responseFields),void 0!==a.infix&&(this.infix=a.infix),void 0!==a.timeoutPost&&(this.timeoutPost=a.timeoutPost),void 0!==a.timeoutGet&&(this.timeoutGet=a.timeoutGet),void 0!==a.autoUpdate&&(this.autoUpdate=a.autoUpdate)),this.locked=!1,this.backForward=!1,this.absolutePath=null,this.containers=[],this.intermediateContainer=null,this.navigationListeners=[],this.eventNavigate={type:"navigate"},this.eventContentReceived={type:"contentreceived",response:null},this.eventLoad={type:"load"},this.xhr=new XMLHttpRequest,this.xhr.addEventListener("readystatechange",function(a){b._handleResponse(this,a)}),this.xhr.addEventListener("timeout",function(){var a={};b.responseFields.length&&(a.title="Timeout Error",a[b.responseFields[0]]=Stay.Error.TIMEOUT,b.locked=!0,b.update(a))}),window.addEventListener("popstate",function(a){b.locked||null===a.state||(b.backForward=!0,b._navigate({href:a.state.url}))}),this._switchPage=function(a){var c=void 0!==a.preventDefault,d=!1;return"submit"===a.type?d=!0:a.metaKey||a.shiftKey||a.altKey||a.ctrlKey||(void 0!==a.which?d=1===a.which:void 0!==a.button&&(d=0===a.button)),d&&(c&&a.preventDefault(),b.locked||b._navigate(this)),!(d&&!c)},this._updateListeners()}var e=a("@zayesh/eventdispatcher"),f="/";Stay.prototype=Object.create(e.prototype),Stay.prototype.constructor=Stay,Stay.prototype._navigate=function(a){var b,c,e,g=!1;a.action?(this.absolutePath=a.action,b=new FormData(a),g=!0):this.absolutePath=a.href,c=d(this.absolutePath),this.locked=!0,e=c.pathname===f?this.absolutePath.slice(0,this.absolutePath.length-1)+this.infix+c.pathname:this.absolutePath.replace(c.pathname,this.infix+c.pathname),this.eventNavigate.method=g?"POST":"GET",this.dispatchEvent(this.eventNavigate),g?(this.xhr.open("POST",e,!0),this.xhr.timeout=this.timeoutPost,this.xhr.send(b)):(this.xhr.open("GET",e,!0),this.xhr.timeout=this.timeoutGet,this.xhr.send())},Stay.prototype._updateView=function(a){var b,c,d,e,f=!1;if(null===this.intermediateContainer)this.intermediateContainer=document.createElement("div");else for(;this.intermediateContainer.children.length>0;)this.intermediateContainer.removeChild(this.intermediateContainer.children[0]);for(b=0,c=this.responseFields.length;c>b;++b)if(d=this.containers[this.responseFields[b]],d||(d=this.containers[this.responseFields[b]]=document.getElementById(this.responseFields[b])),e=a[this.responseFields[b]]){for(;d.children.length>0;)d.removeChild(d.children[0]);for(this.intermediateContainer.innerHTML=e;this.intermediateContainer.children.length>0;)d.appendChild(this.intermediateContainer.children[0]);f=!0}f&&this._updateListeners()},Stay.prototype._updateListeners=function(){var a,b,c=this,d=document.getElementsByTagName("a"),e=document.getElementsByTagName("form");for(a=0,b=this.navigationListeners.length;b>a;++a)this.navigationListeners[a][0].removeEventListener(this.navigationListeners[a][1],c._switchPage);for(a=0,b=d.length;b>a;++a)d[a].addEventListener("click",c._switchPage),this.navigationListeners.push([d[a],"click"]);for(a=0,b=e.length;b>a;++a)e[a].addEventListener("submit",c._switchPage),this.navigationListeners.push([e[a],"submit"])},Stay.prototype.update=function(a){this._updateView(a),document.title=a.title,a.url&&(this.absolutePath=a.url.replace(this.infix,"")),this.backForward?this.backForward=!1:history.pushState({url:this.absolutePath},a.title,this.absolutePath),this.eventContentReceived.response=null,this.dispatchEvent(this.eventLoad),this.locked=!1},Stay.prototype._handleResponse=function(a){var b={};if(0===this.responseFields.length)b.title="Setup Error",b[this.responseFields[0]]=Stay.Error.NO_RESPONSE_FIELDS;else if(4===a.readyState){try{b=JSON.parse(a.responseText)}catch(c){b.title="Parse Error",b[this.responseFields[0]]=Stay.Error.UNPARSABLE,console.log(c)}b.url=a.responseURL,this.eventContentReceived.response=b,this.dispatchEvent(this.eventContentReceived),this.autoUpdate&&this.update(b)}},Stay.Error=Object.freeze({TIMEOUT:"<p>The server didn't respond in time. Please try again later!</p>",UNPARSABLE:"<p>The received content could not be parsed.</p>",NO_RESPONSE_FIELDS:"<p>No response fields have been specified.</p>"}),b.exports=Stay},{"@zayesh/eventdispatcher":1}]},{},[2])(2)}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Stay=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){"use strict";function d(){this._listeners={}}d.prototype.addEventListener=function(a,b){void 0===this._listeners[a]&&(this._listeners[a]=[]),-1===this._listeners[a].indexOf(b)&&this._listeners[a].push(b)},d.prototype.hasEventListener=function(a,b){return void 0!==this._listeners[a]&&-1!==this._listeners[a].indexOf(b)},d.prototype.removeEventListener=function(a,b){var c,d=this._listeners,e=d[a];void 0!==e&&(c=e.indexOf(b),-1!==c&&e.splice(c,1))},d.prototype.dispatchEvent=function(a){var b,c,d=this._listeners,e=d[a.type];if(void 0!==e)for(a.target=this,b=0,c=e.length;c>b;++b)e[b].call(this,a)},b.exports=d},{}],2:[function(a,b,c){"use strict";function d(a){var b=document.createElement("a");return b.href=a,b}function Stay(a){var b=this;e.call(this),this.responseFields=["main","complementary","contentinfo"],this.infix="/json",this.timeoutPost=6e4,this.timeoutGet=5e3,this.autoUpdate=!0,void 0!==a&&(void 0!==a.responseFields&&(this.responseFields=a.responseFields),void 0!==a.infix&&(this.infix=a.infix),void 0!==a.timeoutPost&&(this.timeoutPost=a.timeoutPost),void 0!==a.timeoutGet&&(this.timeoutGet=a.timeoutGet),void 0!==a.autoUpdate&&(this.autoUpdate=a.autoUpdate)),this.locked=!1,this.backForward=!1,this.absolutePath=null,this.containers=[],this.intermediateContainer=null,this.navigationListeners=[],this.eventNavigate={type:"navigate"},this.eventReceive={type:"receive",response:null},this.eventLoad={type:"load"},this.xhr=new XMLHttpRequest,this.xhr.addEventListener("readystatechange",function(a){b._handleResponse(this,a)}),this.xhr.addEventListener("timeout",function(){var a={};b.responseFields.length&&(a.title="Timeout Error",a[b.responseFields[0]]=Stay.Error.TIMEOUT,b.locked=!0,b.update(a))}),window.addEventListener("popstate",function(a){b.locked||null===a.state||(b.backForward=!0,b._navigate({href:a.state.url}))}),this._switchPage=function(a){var c=void 0!==a.preventDefault,d=!1;return"submit"===a.type?d=!0:a.metaKey||a.shiftKey||a.altKey||a.ctrlKey||(void 0!==a.which?d=1===a.which:void 0!==a.button&&(d=0===a.button)),d&&(c&&a.preventDefault(),b.locked||b._navigate(this)),!(d&&!c)},this._updateListeners()}var e=a("@zayesh/eventdispatcher"),f="/";Stay.prototype=Object.create(e.prototype),Stay.prototype.constructor=Stay,Stay.prototype.addResponseField=function(a){-1===this.responseFields.indexOf(a)&&this.responseFields.push(a)},Stay.prototype.removeResponseField=function(a){var b=this.responseFields.indexOf(a);-1!==b&&this.responseFields.splice(b,1)},Stay.prototype._navigate=function(a){var b,c,e,g=!1;a.action?(this.absolutePath=a.action,b=new FormData(a),g=!0):this.absolutePath=a.href,c=d(this.absolutePath),this.locked=!0,e=c.pathname===f?this.absolutePath.slice(0,this.absolutePath.length-1)+this.infix+c.pathname:this.absolutePath.replace(c.pathname,this.infix+c.pathname),this.eventNavigate.method=g?"POST":"GET",this.dispatchEvent(this.eventNavigate),g?(this.xhr.open("POST",e,!0),this.xhr.timeout=this.timeoutPost,this.xhr.send(b)):(this.xhr.open("GET",e,!0),this.xhr.timeout=this.timeoutGet,this.xhr.send())},Stay.prototype._updateView=function(a){var b,c,d,e,f=!1;if(null===this.intermediateContainer)this.intermediateContainer=document.createElement("div");else for(;this.intermediateContainer.children.length>0;)this.intermediateContainer.removeChild(this.intermediateContainer.children[0]);for(b=0,c=this.responseFields.length;c>b;++b)if(d=this.containers[this.responseFields[b]],d||(d=this.containers[this.responseFields[b]]=document.getElementById(this.responseFields[b])),e=a[this.responseFields[b]]){for(;d.children.length>0;)d.removeChild(d.children[0]);for(this.intermediateContainer.innerHTML=e;this.intermediateContainer.children.length>0;)d.appendChild(this.intermediateContainer.children[0]);f=!0}f&&this._updateListeners()},Stay.prototype._updateListeners=function(){var a,b,c=this,d=document.getElementsByTagName("a"),e=document.getElementsByTagName("form");for(a=0,b=this.navigationListeners.length;b>a;++a)this.navigationListeners[a][0].removeEventListener(this.navigationListeners[a][1],c._switchPage);for(a=0,b=d.length;b>a;++a)d[a].addEventListener("click",c._switchPage),this.navigationListeners.push([d[a],"click"]);for(a=0,b=e.length;b>a;++a)e[a].addEventListener("submit",c._switchPage),this.navigationListeners.push([e[a],"submit"])},Stay.prototype.update=function(a){this._updateView(a),document.title=a.title,a.url&&(this.absolutePath=a.url.replace(this.infix,"")),this.backForward?this.backForward=!1:history.pushState({url:this.absolutePath},a.title,this.absolutePath),this.eventReceive.response=null,this.dispatchEvent(this.eventLoad),this.locked=!1},Stay.prototype._handleResponse=function(a){var b={};if(0===this.responseFields.length)b.title="Setup Error",b[this.responseFields[0]]=Stay.Error.NO_RESPONSE_FIELDS;else if(4===a.readyState){try{b=JSON.parse(a.responseText)}catch(c){b.title="Parse Error",b[this.responseFields[0]]=Stay.Error.UNPARSABLE,console.log(c)}b.url=a.responseURL,this.eventReceive.response=b,this.dispatchEvent(this.eventReceive),this.autoUpdate&&this.update(b)}},Stay.Error=Object.freeze({TIMEOUT:"<p>The server didn't respond in time. Please try again later!</p>",UNPARSABLE:"<p>The received content could not be parsed.</p>",NO_RESPONSE_FIELDS:"<p>No response fields have been specified.</p>"}),b.exports=Stay},{"@zayesh/eventdispatcher":1}]},{},[2])(2)}); |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["xhr", "xmlhttprequest", "ajax", "web", "application"], | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"homepage": "https://github.com/vanruesc/stay", | ||
@@ -8,0 +8,0 @@ "author": { |
@@ -7,3 +7,5 @@ # Stay | ||
Stay is a small but effective JavaScript library for the creation of dynamic xhr-driven web applications. | ||
Stay is a small but effective module for the creation of dynamic xhr-driven web applications. | ||
It expects the server to be able to send the page content as a JSON string in which the key names | ||
correspond with the IDs of the target DOM containers. | ||
@@ -31,4 +33,4 @@ ## Installation | ||
var stay = new Stay({ | ||
/* Default is ["content", "navigation"] */ | ||
responseFields: ["myContent", "myNavigation", "myFooter"], | ||
/* Default is ["main", "complementary", "contentinfo"] */ | ||
responseFields: ["myContent", "myNavigation", "myFooter", "myWidget"], | ||
/* Default is "/json" */ | ||
@@ -39,5 +41,11 @@ infix: "/urlPatternForAsyncRequests", | ||
/* Default is 5000ms */ | ||
timeoutGet: 0 | ||
timeoutGet: 0, | ||
/* Default is true */ | ||
autoUpdate: false | ||
}); | ||
// You can also add and remove response fields. | ||
stay.addResponseField("myContainer"); | ||
stay.removeResponseField("contentinfo"); | ||
stay.addEventListener("navigate", function() | ||
@@ -48,2 +56,10 @@ { | ||
stay.addEventListener("receive", function(event) | ||
{ | ||
// If autoUpdate is set to false, the programmer can | ||
// decide when to update the page content. | ||
// The response is the parsed JSON string from the server. | ||
stay.update(event.response); | ||
}); | ||
stay.addEventListener("load", function() | ||
@@ -50,0 +66,0 @@ { |
@@ -45,3 +45,3 @@ "use strict"; | ||
this.responseFields = ["content", "navigation"]; | ||
this.responseFields = ["main", "complementary", "contentinfo"]; | ||
this.infix = "/json"; | ||
@@ -68,3 +68,3 @@ this.timeoutPost = 60000; | ||
this.eventNavigate = {type: "navigate"}; | ||
this.eventContentReceived = {type: "contentreceived", response: null}; | ||
this.eventReceive = {type: "receive", response: null}; | ||
this.eventLoad = {type: "load"}; | ||
@@ -153,2 +153,32 @@ | ||
/** | ||
* Adds a response field. | ||
* | ||
* @param {string} field - The field to add. | ||
*/ | ||
Stay.prototype.addResponseField = function(field) | ||
{ | ||
if(this.responseFields.indexOf(field) === -1) | ||
{ | ||
this.responseFields.push(field); | ||
} | ||
}; | ||
/** | ||
* Removes a response field. | ||
* | ||
* @param {string} field - The field to remove. | ||
*/ | ||
Stay.prototype.removeResponseField = function(field) | ||
{ | ||
var i = this.responseFields.indexOf(field); | ||
if(i !== -1) | ||
{ | ||
this.responseFields.splice(i, 1); | ||
} | ||
}; | ||
/** | ||
* Navigates to the next target uri. | ||
@@ -263,4 +293,4 @@ * | ||
* Binds event listeners to all links and forms. | ||
* This method is combined with the cleanup and | ||
* basically refreshes the navigation listeners. | ||
* This method is combined with the cleanup and basically refreshes | ||
* the navigation listeners. | ||
*/ | ||
@@ -299,4 +329,4 @@ | ||
* behaviour can be disabled. It is then the responsibility of the | ||
* programmer to call update with the response data provided by the | ||
* "contentreceived" event. | ||
* programmer to call stay.update() with the response data provided | ||
* by the "receive" event. | ||
* | ||
@@ -325,3 +355,3 @@ * @param {object} response - The response to display. | ||
this.eventContentReceived.response = null; | ||
this.eventReceive.response = null; | ||
this.dispatchEvent(this.eventLoad); | ||
@@ -363,4 +393,4 @@ this.locked = false; | ||
response.url = xhr.responseURL; | ||
this.eventContentReceived.response = response; | ||
this.dispatchEvent(this.eventContentReceived); | ||
this.eventReceive.response = response; | ||
this.dispatchEvent(this.eventReceive); | ||
@@ -367,0 +397,0 @@ if(this.autoUpdate) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39730
916
77