scribeengine/themes/stargazer/public/scripts/ScribeEngine.js

238 lines
6.7 KiB
JavaScript

/*****************************************************************************
* ScribeEngine - Open Source Blog Software *
* ------------------------------------------------------------------------- *
* Copyright (c) 2010 Raoul Snyman *
* ------------------------------------------------------------------------- *
* 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; version 2 of the License. *
* *
* 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. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
*****************************************************************************/
window["ScribeEngine"] = {
Namespace: {
/**
* Create a Javascript namespace.
* Based on: http://code.google.com/p/namespacedotjs/
* Idea behind this is to created nested namespaces that are not ugly.
* Example:
* Namespace(foo.bar);
* foo.bar..myFunction = function () { } ;
*/
create: function (name, attributes) {
var parts = name.split('.'),
ns = window,
i = 0;
// find the deepest part of the namespace
// that is already defined
for(; i < parts.length && parts[i] in ns; i++)
ns = ns[parts[i]];
// initialize any remaining parts of the namespace
for(; i < parts.length; i++)
ns = ns[parts[i]] = {};
// copy the attributes into the namespace
for (var attr in attributes)
ns[attr] = attributes[attr];
},
exists: function (namespace) {
/**
* Determine the namespace of a page
*/
page_namespace = $ScribeEngine.Namespace.get_page_namespace();
return (namespace == page_namespace);
},
get_page_namespace: function () {
return $("#content > h2").attr("id");
}
}
};
Array.prototype.append = function (elem) {
this[this.length] = elem;
}
ScribeEngine.Namespace.create("ScribeEngine.Events", {
// Local variables
onload_functions: Array(),
// Functions
load: function (func) {
this.onload_functions.append(func);
},
click: function (selector, func) {
$(selector).bind("click", func);
},
change: function (selector, func) {
$(selector).bind("change", func);
},
submit: function (selector, func) {
$(selector).bind("submit", func);
},
blur: function (selector, func) {
$(selector).bind("blur", func);
},
paste: function (selector, func) {
$(selector).bind("paste", func);
},
keyup: function (selector, func) {
$(selector).bind("keyup", func);
},
keydown: function (selector, func) {
$(selector).bind("keydown", func);
},
keypress: function (selector, func) {
$(selector).bind("keypress", func);
},
get_element: function(event) {
var targ;
if (!event) {
var event = window.event;
}
if (event.target) {
targ = event.target;
}
else if (event.srcElement) {
targ = event.srcElement;
}
if (targ.nodeType == 3) {
// defeat Safari bug
targ = targ.parentNode;
}
return $(targ);
},
init: function () {
for (idx in this.onload_functions) {
func = this.onload_functions[idx];
func();
}
}
});
ScribeEngine.Namespace.create("ScribeEngine.Widgets", {
/**
* Adds a datepicker to an element.
*/
datepicker: function (selector)
{
$(selector).datepicker({showButtonPanel: true, dateFormat: "dd/mm/yy"});
}
});
ScribeEngine.Namespace.create("ScribeEngine.General", {
/**
* Fades out a message
*/
fade_message: function ()
{
$("#message").hide().fadeIn("slow", function() {
setTimeout("$('#message').fadeOut('slow');", 1500);
});
},
/**
* Checks for a message and fades it in and out.
*/
show_message: function ()
{
if ($("#message"))
{
setTimeout("$ScribeEngine.General.fade_message()", 500);
}
},
/**
* Dynamically hide anything on the page that has a "jshidden" class.
*/
hide_elements: function ()
{
$(".jshidden").hide();
},
/**
* Do all the funny things required to toggle a fieldset
*/
perform_toggle: function (fieldset)
{
content = $('> div', fieldset);
if (fieldset.is('.collapsed'))
{
fieldset.removeClass('collapsed');
content.slideDown('normal');
}
else
{
content.slideUp('normal',
function()
{
fieldset.addClass('collapsed');
}
);
}
},
/**
* Find the fieldset to toggle, and then perform the toggle
*/
toggle_fieldset: function ()
{
fieldset = $(this).parent().parent();
$ScribeEngine.General.perform_toggle(fieldset);
return false;
},
/**
* Sets the active project
*/
set_project: function ()
{
$('#project-selector > div.content').hide();
$('#project-throbber').show();
project_id = $("#CurrentProject").val();
$.get('/dashboard/ajax_project/' + project_id, function () {
window.location.reload();
});
},
/**
* Initialises collapsible fieldsets
*/
init_fieldsets: function ()
{
$("fieldset.collapsible > legend").each(function() {
legend = $(this);
legend_text = legend.text();
legend.text("");
legend.append(
$("<a>").attr("href", "#").attr("title", "Expand/collapse details")
.text(legend_text).click($ScribeEngine.General.toggle_fieldset));
});
$("fieldset.collapsed").each(function() {
$("> div.content", this).slideUp("normal");
});
},
/**
* Initialises elastic textareas
*/
init_textareas: function ()
{
$("textarea").elastic();
}
});
/**
* Global onload
*
* This function below will be executed on all page views.
*/
ScribeEngine.Events.load(function () {
// Hide hidden elements
ScribeEngine.General.hide_elements();
// Initialise collapsible fieldsets
ScribeEngine.General.init_fieldsets();
// Initialise elastic textareas
ScribeEngine.General.init_textareas();
// Show any flash messages
ScribeEngine.General.show_message();
});