-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.sections.js
executable file
·325 lines (253 loc) · 7.34 KB
/
crafity.sections.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*global $, window, document*/
(function (crafity, $) {
"use strict";
crafity.sections = (function () {
/**
* An internal dictionary of sections
*/
var sections = {}
, self = {}
, scrollable$
, paddingTop;
/**
* A type representing a Section
* @param {String} url The url of the section
* @param {Object} data The section's data
*/
function Section(url, data) {
/* Variables */
var self = this;
/**
* Is section visible?
*/
this.visible = false;
/**
* The section element
*/
this.section$ = null;
/**
* The url of this section
* returns {String} The url of the section
*/
this.url = url;
/**
* The data of the section
*/
this.setInnerHtml = function (data) {
if (!data) { return; }
var closeButton = $("<a class='close command' href='#'>Close</a>")
.click(function () {
self.close();
return false;
})
, sectionBody$ = $("<div class='body' />").append(data);
self.section$ = $("<section data-href='" + url + "' />")
.append(sectionBody$)
.append(closeButton);
return self;
};
/**
* Append this section to a parent element
* @param parent A parent element to add this section to
*/
this.appendTo = function (parent) {
if (!parent) { throw new Error("Missing argument 'parent'"); }
if (!self.section$) { throw new Error("There is no data to display"); }
if (!paddingTop) { paddingTop = parseInt($(parent).css("padding-top").replace("px", "")); }
self.section$.appendTo(parent);
return self;
};
this.getPaddingTop = function () {
return (self.section$.get(0).offsetTop - paddingTop);
};
/**
* Show this section
*/
this.show = function () {
if (!self.section$) { throw new Error("There is no data to display"); }
self.section$.removeClass("animate").addClass("visible");
setTimeout(function () {
self.section$.addClass("animate");
}, (Math.random() * 500));
if (!scrollable$) { scrollable$ = $(".scrollable:first"); }
scrollable$.animate(
{
"scrollTop": self.getPaddingTop(),
opacity: 1
},
400,
"swing",
function () {
});
self.visible = true;
return self;
};
/**
* Hide this section
*/
this.hide = function (callback) {
if (!self.section$) { throw new Error("There is no data to display"); }
if (!self.visible) { return; }
self.section$.css({
height: self.section$.height(),
opacity: 1
}).animate({ height: "0px", opacity: 0 }, 400, "swing", function () {
self.section$.removeClass("visible");
if (callback) {
callback();
}
});
self.visible = false;
return self;
};
/**
* Close this section
*/
this.close = function () {
if (!self.section$) { throw new Error("There is no data to display"); }
if (self.onClosing.raise()) { return; }
self.hide(function onHidden() {
self.section$.remove();
});
return self;
};
/**
* On closing event is called before a section is closed
*/
this.onClosing = new crafity.Event("cancel");
// Initialize the new section with data
this.setInnerHtml(data);
return this;
}
/**
* Get all the sections
*/
self.getAll = function () {
return crafity.objects.map(sections, function (value) {
return value;
});
};
/**
* Get a specific section by URL
* @param {String} url The url of the section to get
*/
self.getByUrl = function (url) {
return sections[url];
};
/**
* Create a new section
* @param {String} url (Optional) The url of this section
* @param {Function} callback (Optional) The callback is required when a url is specified
*/
self.create = function create(url, callback) {
if (!url && !callback) {
// When there are no args, Then return a new section synchronous
return new Section(null, null);
} else if (typeof url === 'Function' && !callback) {
// return a new section async without loading from a url
callback = url;
url = null;
callback(new Section(url, null));
} else {
// Download the content, create a section and call the callback
crafity.ajax({
url: url + "?layout=false",
success: function (data) {
callback(null, new Section(url, data));
},
error: function (err) {
callback(err, null);
}
});
}
};
/**
* Add a section to the application
* @param {Section} section The section to add
*/
self.add = function (section) {
sections[section.url] = section;
};
/**
* Remove a section from the list of sections
* @param {Section} section The section to remove
*/
self.remove = function (section) {
if (!section) { return; }
delete sections[section.url];
};
return self;
}());
crafity.navigation.enabled = false;
}(window.crafity = window.crafity || {}, jQuery));
// * * * * * * * * * * * * * * * * * *
// Test code...
// * * * * * * * * * * * * * * * * * *
$(document).ready(function () {
// Variables
var hashInfo = crafity.navigation.hashInfo
, menuSection$ = $("#menu")
, menuSection;
if (menuSection$) {
menuSection = crafity.sections.create();
menuSection.url = "/";
menuSection.setInnerHtml(menuSection$.html());
crafity.sections.add(menuSection);
}
// Register on all the clicks on anchor elements
$("body").delegate("a", "click", function (e) {
// First make sure the A link is clicked (sometimes other click come through)
if (this.nodeName === 'A') {
if ($(this).hasClass("download") ||
$(this).hasClass("mail")) { return; }
// Get the href of the link
var href = this.getAttribute("href");
var base = window.location.href;
if (~base.lastIndexOf("#")) {
base = base.substring(0, base.lastIndexOf("#"));
}
if (~base.lastIndexOf("/")) {
base = base.substring(0, base.lastIndexOf("/") + 1);
}
href = href.replace(base, "");
// If the href is empty or
// is an external link then quit
if (href && (~href.indexOf("http://") || ~href.indexOf("https://"))) {
return;
}
// Change the href in the URL's hash
hashInfo.change({ href: href || "/" });
return false;
}
});
// subscribe on navigation hash changed
hashInfo.onChange.subscribe(function() {
// Get the section belonging to the url
var section = crafity.sections.getByUrl(hashInfo.values.href);
if (section) {
// If the section is found, then show it
section.show();
} else if (hashInfo.values.href && hashInfo.values.href !== "/") {
// If the section was not found and a section is requisted
// then create a new section...
crafity.sections.create(hashInfo.values.href, function(err, section) {
if (err) { throw err; }
// Add the section to the list of sections
crafity.sections.add(section);
// Subscribe to the close event of the section
section.onClosing.subscribe(function () {
var newSection = section.section$.prev();
if (!newSection || !newSection.attr("data-href")) { newSection = section.section$.next(); }
if (newSection && newSection.attr("data-href")) {
hashInfo.change({ href: newSection.attr("data-href") });
}
crafity.sections.remove(section);
return true;
});
// and add the new section to the screen
section.appendTo($("#container")).show();
});
}
});
$(window).trigger("hashchange");
});