This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
old-scribeengine/scribeengine/public/scripts/ScribeEngine.js
2010-06-06 22:10:03 +02:00

299 lines
9.0 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);
},
liveClick: function (selector, func) {
$(selector).live("click", func);
},
getElement: 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"});
},
tagEditor: function (selector)
{
$(selector).tagEditor({completeOnBlur: true, initialParse: true});
},
elastic: function (selector)
{
$(selector).elastic();
},
fileBrowser: function (field_name, url, type, win)
{
//alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing
var cmsURL = window.location.toString(); // script URL - use an absolute path!
if (cmsURL.indexOf("?") < 0)
{
//add the type as the only query parameter
cmsURL = cmsURL + "?type=" + type;
}
else
{
//add the type as an additional query parameter
// (PHP session ID is now included if there is one at all)
cmsURL = cmsURL + "&type=" + type;
}
tinyMCE.activeEditor.windowManager.open(
{
file: "/media",
title: "Media Library",
width: 600, // Your dimensions may differ - toy around with them!
height: 400,
resizable: "yes",
inline: "yes", // This parameter only has an effect if you use the inlinepopups plugin!
close_previous: "no"
},
{
window: win,
input: field_name
}
);
return false;
},
tinymce: function (selector)
{
$(selector).tinymce({
script_url: "/scripts/tinymce/tiny_mce.js",
theme: "advanced",
dialog_type: "modal",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "bottom",
theme_advanced_resizing: true,
theme_advanced_resize_horizontal: false,
file_browser_callback: "ScribeEngine.Widgets.fileBrowser"
});
},
tree: function (selector, tree_data, onselect)
{
$(selector)
.bind("click.jstree", onselect)
.jstree({
plugins: ["themes", "json_data"],
json_data: {data: tree_data},
themes: {theme: "default"}
});
}
});
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();
},
/**
* Go to a particular month or year on the calendar.
*/
go_to_month: function (e)
{
var link = ScribeEngine.Events.getElement(e);
$("#calendar_wrap").load(link.attr("href"), function() {
ScribeEngine.Events.click("#next a", ScribeEngine.General.go_to_month);
ScribeEngine.Events.click("#prev a", ScribeEngine.General.go_to_month);
});
return false;
},
/**
* 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;
},
/**
* 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 () {
ScribeEngine.Events.click("#next a", ScribeEngine.General.go_to_month);
ScribeEngine.Events.click("#prev a", ScribeEngine.General.go_to_month);
});