-
Notifications
You must be signed in to change notification settings - Fork 0
/
curio.js
188 lines (155 loc) · 5.29 KB
/
curio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*!
* Curio 1.0
* https://github.com/sandeepjain/curio
*
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
(function () {
'use strict';
var emptyFunction = function () {},
eventMatches = (function () {
var div = document.createElement("div");
return ["moz", "webkit", "ms", "o"].filter(function (prefix) {
return prefix + "MatchesSelector" in div;
})[0] + "MatchesSelector";
})(),
curioFactory;
function ensureEl(el) {
if (typeof el === 'string') {
el = document.querySelector(el);
}
return el;
}
function removeClass(el, className) {
if (el.classList) {
el.classList.remove(className);
} else {
el.className = el.className.replace(new RegExp('(^| )' + className.split(' ').join('|') + '( |$)', 'gi'), ' ');
}
}
function addClass(el, className) {
if (el.classList) {
el.classList.add(className);
} else {
el.className += ' ' + className;
}
}
curioFactory = function (el, options) {
options = options || {}
el = ensureEl(el);
var elContent = ensureEl(options.elContent),
elLoader = options.elLoader ? ensureEl(options.elLoader) : false,
onRender = options.onRender || emptyFunction,
beforeFetch = options.beforeFetch || emptyFunction,
doCache = typeof options.doCache != 'undefined' ? options.doCache : true,
keepHistory = options.history || false,
cache = [],
xhr = false,
elActive = false,
linkSelector = '[href]';
function changeContent(e) {
var actualHref,
href;
if (!e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
e.preventDefault();
actualHref = this.href;
href = this.getAttribute('data-ajaxhref') || actualHref;
elActive && removeClass(elActive, 'active');
elActive = this;
if (keepHistory) {
window.history.pushState({ elurl: href }, document.title, actualHref || href);
}
beforeFetch();
getContent(href);
return false;
}
}
function getContent(url) {
// close previous request
xhr && xhr.abort();
elActive && addClass(elActive, 'active');
hideResults();
showLoader();
if (doCache && cache[url]) {
renderResults(cache[url]);
return;
}
addClass(elActive, 'working');
// create new request
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
// cache response
doCache && (cache[url] = xhr.responseText);
renderResults(xhr.responseText);
} else {
throw new Error('Target server returned an error');
}
removeClass(elActive, 'working');
};
xhr.onerror = function() {
removeClass(elActive, 'working');
throw new Error('Target server returned an error');
};
xhr.send();
}
function showLoader() {
if (elLoader) {
addClass(elLoader, 'show');
} else {
addClass(elContent, 'loading');
}
}
function hideLoader() {
if (elLoader) {
removeClass(elLoader, 'show');
} else {
removeClass(elContent, 'loading');
}
}
function renderResults (html) {
elContent.innerHTML = html;
hideLoader();
showResults();
onRender(el);
}
function showResults() {
elLoader && addClass(elContent, 'show');
}
function hideResults() {
elLoader && removeClass(elContent, 'show');
}
if (keepHistory && !(window.history && window.history.pushState)) {
keepHistory = false;
}
if (keepHistory) {
window.addEventListener('popstate', function (event) {
if (event.state) {
renderResults(cache[event.state.elurl]);
}
}, false);
}
el.addEventListener('click', function (event) {
var target = event.target;
// find node matching selector
while( target && target !== this && !target[eventMatches](linkSelector) ) {
target = target.parentNode;
}
// if required element clicked
if( target && target !== this ) {
return changeContent.call(target, event);
}
}, false);
};
// export functions
if (typeof define === 'function' && define.amd) {
define(curioFactory);
} else if (typeof exports === 'object') {
module.exports = curioFactory;
} else {
window.curio = curioFactory;
}
}).call(this);