2013-11-02 22:42:57 +00:00
|
|
|
/**
|
|
|
|
* (C) Copyright Collin Doering 2013
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-12-16 06:34:04 +00:00
|
|
|
*
|
2013-11-02 22:42:57 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-12-16 06:34:04 +00:00
|
|
|
*
|
2013-11-02 22:42:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-05 23:20:03 +00:00
|
|
|
*/
|
2013-11-02 22:42:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File:. default.js
|
|
|
|
* Author: Collin J. Doering
|
|
|
|
* Date: Sep 6, 2013
|
2015-01-07 06:59:29 +00:00
|
|
|
* Description: Client-side logic for blog-rekahsoft-ca
|
2013-11-02 22:42:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
//------------------------
|
2015-01-20 04:35:43 +00:00
|
|
|
/*jslint browser: true*/
|
|
|
|
/*global jQuery, MathJax*/
|
|
|
|
//------------------------
|
2013-11-02 22:42:57 +00:00
|
|
|
|
2015-01-20 04:35:43 +00:00
|
|
|
(function ($, mj) {
|
|
|
|
"use strict";
|
2015-01-06 09:08:40 +00:00
|
|
|
|
2015-01-20 04:35:43 +00:00
|
|
|
var page = (function () {
|
|
|
|
// var pageId = '#page-content', navId = '#nav';
|
2014-03-05 23:20:03 +00:00
|
|
|
|
|
|
|
function newContentCallback() {
|
2015-01-22 05:24:52 +00:00
|
|
|
$('#page-content a').each(function (i) {
|
2015-01-20 04:35:43 +00:00
|
|
|
var page_href = $(this).attr('href'),
|
|
|
|
external_url_regexp = /https?:\/\/.*/,
|
|
|
|
mailto_regexp = /mailto:.*/,
|
|
|
|
files_regexp = /files\/.*/,
|
|
|
|
images_regexp = /images\/.*/;
|
|
|
|
|
2015-01-22 05:24:52 +00:00
|
|
|
if (!(external_url_regexp.test(page_href) || mailto_regexp.test(page_href) || files_regexp.test(page_href) || images_regexp.test(page_href))) {
|
|
|
|
$(this).attr('href', "/#" + page_href);
|
2014-03-05 23:20:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-12-11 04:31:48 +00:00
|
|
|
|
2014-03-05 23:20:03 +00:00
|
|
|
function loadPageContent(page_href) {
|
2015-01-20 04:35:43 +00:00
|
|
|
var post_regexp = /posts\/.*\.html/,
|
2015-01-22 18:35:58 +00:00
|
|
|
page_regexp = /pages\/(.*\.html)/,
|
2015-01-20 04:35:43 +00:00
|
|
|
tag_regexp = /tags\/.*(\d*)\.html/,
|
2015-01-22 18:35:58 +00:00
|
|
|
tag_not_regexp = /(tags\/.*[^\d]+)(\.html)/,
|
|
|
|
blog_page_regexp = /(pages\/)?(blog\d*\.html)/;
|
2014-12-16 06:34:04 +00:00
|
|
|
|
2014-03-13 00:59:58 +00:00
|
|
|
// Check whether the requested url is a post
|
2014-03-05 23:20:03 +00:00
|
|
|
if (post_regexp.test(page_href)) {
|
|
|
|
// Handle post urls (no change required to page_href)
|
|
|
|
$('#nav-menu li.active').removeClass('active');
|
2015-01-22 18:35:58 +00:00
|
|
|
$('#nav-menu li a[href="/#/pages/blog.html"]').parent('li').addClass('active');
|
2014-03-05 23:20:03 +00:00
|
|
|
} else if (tag_regexp.test(page_href)) {
|
|
|
|
// Handle tag pages
|
|
|
|
$('#nav-menu li.active').removeClass('active');
|
2015-01-22 18:35:58 +00:00
|
|
|
$('#nav-menu li a[href="/#/pages/blog.html"]').parent('li').addClass('active');
|
2014-12-19 13:39:38 +00:00
|
|
|
|
2015-01-20 04:35:43 +00:00
|
|
|
if (tag_not_regexp.test(page_href)) {
|
|
|
|
page_href = page_href.replace(tag_not_regexp, "$11$2");
|
|
|
|
}
|
2014-03-13 00:59:58 +00:00
|
|
|
} else { // otherwise assume its a page
|
2015-01-20 04:35:43 +00:00
|
|
|
// Check if the page_href is / and if so goto home
|
2014-12-16 06:34:04 +00:00
|
|
|
if (page_href === '/') {
|
2014-03-05 23:20:03 +00:00
|
|
|
page_href = '/home.html';
|
2014-09-13 06:19:44 +00:00
|
|
|
} else if (blog_page_regexp.test(page_href)) {
|
2015-01-22 18:35:58 +00:00
|
|
|
if (page_href === "/blog.html") {
|
2014-12-16 06:34:04 +00:00
|
|
|
page_href = "/blog1.html";
|
2015-01-22 18:35:58 +00:00
|
|
|
} else if (/pages\/.*/.test(page_href)) {
|
|
|
|
$.address.value(page_href.replace(blog_page_regexp, "$2"));
|
|
|
|
return;
|
2015-01-20 04:35:43 +00:00
|
|
|
}
|
2014-12-16 06:34:04 +00:00
|
|
|
|
|
|
|
// If page_href refers to a blog page set Blog to be the active menu item
|
2014-09-13 06:19:44 +00:00
|
|
|
$('a.menuitem[rel="address:/blog.html"]').closest('ul').find('li.active').removeClass('active');
|
|
|
|
$('a.menuitem[rel="address:/blog.html"]').closest('li').addClass('active');
|
2014-03-05 23:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initially set the active menuitem in the nav
|
|
|
|
$('a.menuitem[rel="address:' + page_href + '"]').closest('ul').find('li.active').removeClass('active');
|
|
|
|
$('a.menuitem[rel="address:' + page_href + '"]').closest('li').addClass('active');
|
|
|
|
|
2015-01-22 18:35:58 +00:00
|
|
|
// Rewrite page url if it specifies pages/ prefix; otherwise set page_href to
|
|
|
|
// full url (including pages/ prefix) for ajax call
|
|
|
|
if (page_regexp.test(page_href)) {
|
|
|
|
$.address.value(page_href.replace(page_regexp, "$1"));
|
|
|
|
return;
|
|
|
|
} else {
|
2015-01-22 06:07:48 +00:00
|
|
|
page_href = "pages" + page_href;
|
|
|
|
}
|
2014-03-05 23:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the ajax request for the new page-content (whether it be a page or a post) *could change*
|
|
|
|
$.ajax({
|
|
|
|
url: page_href,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'html',
|
|
|
|
beforeSend: function (xhr, settings) {
|
2015-01-06 09:08:40 +00:00
|
|
|
// Remove loading error from page-content and any status message errors
|
|
|
|
$('#page-content').removeClass('loading-error');
|
|
|
|
$('#status').slideUp('normal', function () {
|
|
|
|
$('#status').removeClass('error').removeClass('success');
|
|
|
|
});
|
|
|
|
|
2014-03-05 23:20:03 +00:00
|
|
|
// Add .loading to #page-content and #nav to facilitate a loading animation
|
2015-01-06 09:08:40 +00:00
|
|
|
$('#page-content, #nav').removeClass('loading-done').addClass('loading');
|
2014-03-05 23:20:03 +00:00
|
|
|
|
|
|
|
console.log('beforeSend a.menuitem');
|
|
|
|
},
|
|
|
|
success: function (dta) {
|
|
|
|
// Remove the initial loading gif (if its there)
|
|
|
|
$('#page-content').removeClass('init');
|
|
|
|
|
|
|
|
// Stop animations in the nav and page-content and scroll to the top of the page in a set amount of time
|
|
|
|
setTimeout(function () {
|
|
|
|
// Replace old page-content with new page-content
|
|
|
|
$('#page-content').html(dta);
|
|
|
|
|
2014-03-13 00:59:58 +00:00
|
|
|
// Stop page loading
|
2014-03-05 23:20:03 +00:00
|
|
|
$('#page-content, #nav').removeClass('loading');
|
|
|
|
|
2014-03-13 00:59:58 +00:00
|
|
|
// Reload any new maths using MathJax
|
|
|
|
$('#page-content .math').each(function (math_elem) {
|
2015-01-20 04:35:43 +00:00
|
|
|
mj.Hub.Queue(["Typeset", mj.Hub, math_elem[0]]);
|
2014-03-13 00:59:58 +00:00
|
|
|
});
|
2014-12-16 06:34:04 +00:00
|
|
|
|
2015-01-07 06:59:29 +00:00
|
|
|
// Scroll to top of the page
|
2014-03-05 23:20:03 +00:00
|
|
|
if ($('body').scrollTop() > $('#nav').offset().top - 15) {
|
|
|
|
$('html, body').animate({
|
|
|
|
scrollTop: $('#nav').offset().top - 15
|
|
|
|
}, 'fast');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new content callbacks
|
|
|
|
newContentCallback();
|
|
|
|
}, 250);
|
|
|
|
},
|
|
|
|
error: function (xhr, status) {
|
2015-01-06 09:08:40 +00:00
|
|
|
/* Remove .loading from #page-content and #nav to stop the loading
|
|
|
|
* animation. Then add .loading-error to #page-content if its the sites
|
|
|
|
* first load (#page-content has class .init). Finally, display an error
|
|
|
|
* message in #status.
|
2014-03-05 23:20:03 +00:00
|
|
|
*/
|
2015-01-06 09:08:40 +00:00
|
|
|
$('#page-content, #nav').removeClass('loading');
|
|
|
|
if ($('#page-content.init')[0]) {
|
|
|
|
$('#page-content').addClass('loading-error').html('<p class="container border-box">Error initially loading blog.rekahsoft.ca. Check the url! Given "' + page_href + '"</p>');
|
|
|
|
} else {
|
2015-01-20 04:35:43 +00:00
|
|
|
$('#status > p.message').text('Error retrieving page "' + page_href + '": ' + status);
|
2015-01-06 09:08:40 +00:00
|
|
|
$('#status').addClass('error').slideDown();
|
|
|
|
}
|
2014-03-05 23:20:03 +00:00
|
|
|
}
|
|
|
|
});
|
2013-12-11 04:31:48 +00:00
|
|
|
}
|
2014-03-05 23:20:03 +00:00
|
|
|
|
2015-01-20 04:35:43 +00:00
|
|
|
function init() {
|
|
|
|
$(document).ready(function () {
|
|
|
|
$.address.init(function (event) {
|
|
|
|
console.log("init:" + event.value);
|
|
|
|
$(window).load(function () {
|
|
|
|
loadPageContent(event.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#nav-menu a.menuitem').click(function () {
|
|
|
|
$(this).closest('ul').find('li.active').removeClass('active');
|
|
|
|
$(this).closest('li').addClass('active');
|
|
|
|
//$('.navbar-collapse').collapse('hide');
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#status a.close-button').click(function () {
|
|
|
|
$(this).parent().slideUp();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Callback for when the inital page has completely loaded (including images, etc..)
|
|
|
|
$(window).load(function () {
|
|
|
|
$.address.change(function (event) {
|
|
|
|
console.log("change " + event.value);
|
|
|
|
loadPageContent(event.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-05 23:20:03 +00:00
|
|
|
return {
|
2015-01-20 04:35:43 +00:00
|
|
|
init: init
|
2014-03-05 23:20:03 +00:00
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
page.init();
|
2015-01-20 04:35:43 +00:00
|
|
|
}(jQuery, MathJax));
|