meta/assets/js/meta.js

228 lines
4.9 KiB
JavaScript
Raw Normal View History

2012-09-12 10:36:43 -04:00
/**
* Extends kis-js to allow centering of absolutely-positioned containers
*/
$_.ext('center', function (sel){
sel = this.el;
if (typeof sel === "undefined") return;
var contHeight,
contWidth,
inH,
inW,
top,
left;
2012-09-13 19:16:51 -04:00
contHeight = sel.offsetHeight;
contWidth = sel.offsetWidth;
2012-09-12 10:36:43 -04:00
2012-09-13 19:16:51 -04:00
inH = window.innerHeight;
inW = window.innerWidth;
2012-09-12 10:36:43 -04:00
sel.style.position = "fixed";
top = (inH - contHeight) / 2;
2012-09-13 19:16:51 -04:00
left = (inW - contWidth) / 2;
2012-09-12 10:36:43 -04:00
2012-09-13 19:16:51 -04:00
sel.style.top = top + "px";
2012-09-12 10:36:43 -04:00
sel.style.left = left + "px";
});
2012-08-31 16:18:23 -04:00
(function() {
2012-08-31 16:18:23 -04:00
"use strict";
2012-09-12 10:36:43 -04:00
var TINY = window.TINY || {};
var $_ = window.$_ || {};
// ! Show/hide forms based on use
2012-08-31 16:18:23 -04:00
$_("fieldset dl").dom.hide();
$_("fieldset legend").event.add('click', function(e){
var form = $_("fieldset dl").dom;
2012-08-31 16:18:23 -04:00
(form.css('display').trim() == 'none')
? form.show()
: form.hide();
});
var meta = {};
2012-09-12 10:36:43 -04:00
window.meta = meta;
/**
* Deletes a genre/category/section/data item
* based on the current page context
*/
meta.delete_item = function(e) {
2012-09-12 10:36:43 -04:00
var item_id, id, type;
// Get the type/id of the item
2012-09-12 10:36:43 -04:00
item_id = this.parentNode.id;
item_id = item_id.split('_');
2012-09-12 10:36:43 -04:00
id = item_id[1];
type = item_id[0];
// Confirm deletion
var confirm_string = "Are you sure you want to delete this "+type+"? Deleting this item will delete all items under it. There is no undo.";
var do_delete = confirm(confirm_string);
2012-09-12 10:36:43 -04:00
var status = false;
2012-09-12 10:36:43 -04:00
if (do_delete)
{
// Call the appropriate deletion method
switch(type)
{
case "genre":
case "category":
case "section":
case "data":
2012-09-13 11:07:31 -04:00
$_.post(APP_URL+'delete', {'id':id, 'type':type}, function(res){
if (res == 1)
{
// Reload the page
window.location = window.location;
}
else
{
$_.get(APP_URL+'message', {
type: 'error',
message: 'There was an error deleting that item'
}, function(h) {
$_('body').dom.prepend(h);
});
}
});
2012-09-12 10:36:43 -04:00
break;
2012-09-12 10:36:43 -04:00
default:
break;
}
}
};
/**
* Gets the edit form and displays the overlay for the item
* being edited
*/
meta.get_edit_form = function(e) {
var item_id, id, type;
// Get the type/id of the item
item_id = this.parentNode.id;
item_id = item_id.split('_');
id = item_id[1];
type = item_id[0];
$_('#overlay_bg, #overlay').dom.show();
//Get the form for the current item
$_.get(APP_URL+'edit', {'id':id, 'type':type}, function(res){
$_('#overlay').dom.html(res);
// Center the form
$_('#overlay').center();
2012-09-12 12:57:41 -04:00
if (type == 'data')
{
// WYSIWYG
new TINY.editor.edit('edit_wysiwyg',
{
id:'val',
width:450,
height:175,
cssclass:'te',
controlclass:'tecontrol',
rowclass:'teheader',
dividerclass:'tedivider',
controls:['bold','italic','underline','strikethrough','|','subscript','superscript','|',
'orderedlist','unorderedlist','|','leftalign',
'centeralign','rightalign','blockjustify','|','unformat','n','undo','redo','|',
'image','hr','link','unlink','|'],
footer:true,
fonts:['Verdana','Arial','Georgia','Trebuchet MS'],
xhtml:true,
cssfile:ASSET_URL+'css.php/g/css',
bodyid:'editor',
footerclass:'tefooter',
toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'},
resize:{cssclass:'resize'}
});
2012-09-13 19:16:51 -04:00
//Do it again, so it's correct this time!
$_('#overlay').center();
2012-09-12 12:57:41 -04:00
}
2012-09-12 10:36:43 -04:00
});
};
/**
* Submit the update form via javascript
*/
meta.update_item = function(e) {
var id, type, name, val, txt, data={};
2012-09-12 12:57:41 -04:00
// Update textarea from WYSIWYG
if (window.edit_wysiwyg)
{
window.edit_wysiwyg.toggle();
}
2012-09-12 10:36:43 -04:00
// Get the form values
data.id = $_.$('#id').value;
data.type = $_.$('#type').value;
data.name = $_.$('#name').value;
txt = document.getElementById('val');
if (txt)
{
data.val = txt.value;
}
// Submit the form
$_.post(APP_URL+'update', data, function(res) {
// Hide the overlay and form
$_('#overlay_bg').dom.css('display', '');
$_('#overlay').dom.html('');
$_('#overlay').dom.hide();
// Show the appropriate status message
if (res == 1)
{
2012-09-12 12:57:41 -04:00
// Reload the page
window.location = window.location;
2012-09-12 10:36:43 -04:00
}
else
{
$_.get(APP_URL+'message', {
type: 'error',
message: 'There was an error updating that item.'
}, function(h) {
$_('body').dom.prepend(h);
});
}
});
};
// -------------------------------------------------
// ! Event binding
// -------------------------------------------------
// Delete Button functionality
$_("button.delete").event.add('click', meta.delete_item);
2012-09-12 10:36:43 -04:00
// Edit Button functionality
$_("button.edit").event.add('click', meta.get_edit_form);
// Overlay close
$_("#overlay_bg").event.add('click', function(e) {
$_('#overlay_bg').dom.css('display', '');
$_('#overlay').dom.html('');
$_('#overlay').dom.hide();
});
2012-09-12 10:36:43 -04:00
// Edit form submission
$_.event.live('#edit_form', 'submit', meta.update_item);
2012-08-31 16:18:23 -04:00
}());