Gazelle/static/functions/artists.js

458 lines
11 KiB
JavaScript
Raw Normal View History

2011-03-28 14:21:28 +00:00
var dragObjects = null;
var dragObjectPlaceholder = null;
function editOrdering() {
2013-06-17 08:01:02 +00:00
$('#editlayout').ghide();
$('#releasetypes').ghide();
$('#linkbox').ghide();
$('.sidebar').ghide();
$('.main_column > .box').ghide(); // Artist info
$('.main_column > #requests').ghide();
2013-02-22 08:00:24 +00:00
2013-06-17 08:01:02 +00:00
$('#savelayout').gshow();
$('#emptylinkbox').gshow();
$('#torrents_allopenclose').gshow();
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
dragObjects = new Array();
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var elems = $('#torrents_tables table').objects;
2013-06-17 08:01:02 +00:00
for (i in elems) {
2011-03-28 14:21:28 +00:00
var elemID = elems[i].id;
2013-04-18 08:00:54 +00:00
if (elemID == undefined) {
continue;
}
if (elemID.indexOf('torrents_') == 0) {
2013-06-17 08:01:02 +00:00
$('#' + elemID).gshow();
2011-03-28 14:21:28 +00:00
dragObjects[elemID] = new dragObject(elemID, elemID+'_handle', startDrag, moveDrag, endDrag);
var classes = elems[i].className.split(' ');
2013-06-17 08:01:02 +00:00
for (var j = 0; classes.length; j++) {
2013-04-18 08:00:54 +00:00
if (classes[j].indexOf('releases_') == 0) {
2013-06-17 08:01:02 +00:00
$('.'+classes[j].replace('_table', '')).ghide();
$('.artist_editcol').gshow();
$('.artist_normalcol').ghide();
2011-03-28 14:21:28 +00:00
break;
}
}
}
}
2013-02-22 08:00:24 +00:00
2013-06-17 08:01:02 +00:00
for (i in dragObjects) {
dragObjects[i].StartListening();
}
2011-03-28 14:21:28 +00:00
}
function saveOrdering() {
2013-06-17 08:01:02 +00:00
$('#savelayout').ghide();
$('#savinglayout').gshow();
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var elems = $('#torrents_tables table').objects;
var releaseTypes = "{";
2013-06-17 08:01:02 +00:00
for (i in elems) {
2011-03-28 14:21:28 +00:00
var elemID = elems[i].id;
var releaseType = null;
2013-04-18 08:00:54 +00:00
if (elemID == undefined) {
continue;
}
if (elemID.indexOf('torrents_') == 0) {
2011-03-28 14:21:28 +00:00
var classes = elems[i].className.split(' ');
for(var j=0; classes.length; j++) {
2013-04-18 08:00:54 +00:00
if (classes[j] == null) {
break;
}
if (classes[j].indexOf('releases_') == 0) {
2011-03-28 14:21:28 +00:00
releaseType = classes[j].split('_')[1];
}
}
2013-02-22 08:00:24 +00:00
}
2013-04-18 08:00:54 +00:00
if (releaseType != null) {
releaseTypes += '"' + releaseType + '":' + ($('#releases_' + releaseType + '_defaultopen').raw().checked ? 1 : 0) + ",";
}
2011-03-28 14:21:28 +00:00
}
releaseTypes = releaseTypes.substring(0, releaseTypes.length-1) + '}';
var postData = new Array();
postData['layoutdefaults'] = releaseTypes;
ajax.post("artist.php?action=ajax_edit_artistlayout", postData, saveOrderingCallback);
}
function saveOrderingCallback(response) {
//Let's do what the user asked for, shall we.
//Show/hide
var releaseTypes = json.decode(response);
for(releaseType in releaseTypes) {
2013-04-18 08:00:54 +00:00
if (releaseTypes[releaseType] == 1) {
setShow(releaseType, true);
} else {
setShow(releaseType, false);
}
2013-02-22 08:00:24 +00:00
}
2011-03-28 14:21:28 +00:00
//Ordering in linkbox
var prevOrderedLink = null;
2013-06-17 08:01:02 +00:00
for (releaseType in releaseTypes) {
2011-03-28 14:21:28 +00:00
var elem = $('#torrents_' + releaseType + '_anchorlink').raw();
2013-04-18 08:00:54 +00:00
if (elem == undefined) {
continue;
}
if (prevOrderedLink == null) {
prevOrderedLink = elem;
} else {
2013-02-22 08:00:24 +00:00
prevOrderedLink.parentNode.insertBefore(elem, prevOrderedLink.nextSibling);
2011-03-28 14:21:28 +00:00
prevOrderedLink = elem;
}
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
//Now let's return to the non editing layout.
var elems = $('#torrents_tables table').objects;
2013-06-17 08:01:02 +00:00
for (i in elems) {
2011-03-28 14:21:28 +00:00
var elemID = elems[i].id;
2013-04-18 08:00:54 +00:00
if (elemID == undefined) {
continue;
}
if (elemID.indexOf('torrents_') == 0) {
2011-03-28 14:21:28 +00:00
var classes = elems[i].className.split(' ');
var empty = false;
2013-06-17 08:01:02 +00:00
for (var j = 0; classes.length; j++) {
2013-04-18 08:00:54 +00:00
if (classes[j] == null) {
break;
}
if (classes[j].indexOf('releases_') == 0) {
2013-06-17 08:01:02 +00:00
$('.artist_editcol').ghide();
$('.artist_normalcol').gshow();
2011-03-28 14:21:28 +00:00
}
2013-04-18 08:00:54 +00:00
if (classes[j].indexOf('empty') == 0) {
empty = true;
}
}
if (empty) {
2013-06-17 08:01:02 +00:00
$('#' + elemID).ghide();
2011-03-28 14:21:28 +00:00
}
}
2013-02-22 08:00:24 +00:00
}
2013-06-17 08:01:02 +00:00
for (i in dragObjects) {
2013-04-18 08:00:54 +00:00
dragObjects[i].StopListening();
}
2011-03-28 14:21:28 +00:00
dragObjects = null;
2013-02-22 08:00:24 +00:00
2013-06-17 08:01:02 +00:00
$('#savinglayout').ghide();
$('#emptylinkbox').ghide();
$('#torrents_allopenclose').ghide();
2013-02-22 08:00:24 +00:00
2013-06-17 08:01:02 +00:00
$('#editlayout').gshow();
$('#releasetypes').gshow();
$('#linkbox').gshow();
$('.sidebar').gshow();
$('.main_column > .box').gshow(); // Artist info
$('.main_column > #requests').gshow();
2011-03-28 14:21:28 +00:00
}
function setDefaultShow(id, show) {
2013-04-18 08:00:54 +00:00
if (id == 'all') {
2011-03-28 14:21:28 +00:00
var elems = $('#torrents_tables table').objects;
2013-06-17 08:01:02 +00:00
for (i in elems) {
2011-03-28 14:21:28 +00:00
var elemID = elems[i].id;
var releaseType = null;
2013-04-18 08:00:54 +00:00
if (elemID == undefined) {
continue;
}
if (elemID.indexOf('torrents_') == 0) {
2011-03-28 14:21:28 +00:00
var classes = elems[i].className.split(' ');
2013-06-17 08:01:02 +00:00
for (var j = 0; classes.length; j++) {
2013-04-18 08:00:54 +00:00
if (classes[j] == null) {
break;
}
if (classes[j].indexOf('releases_') == 0) {
2011-03-28 14:21:28 +00:00
releaseType = classes[j].split('_')[1];
}
}
}
setDefaultShow(releaseType, show);
}
2013-04-18 08:00:54 +00:00
} else if (show) {
2013-06-17 08:01:02 +00:00
$('#releases_'+id+'_openlink').ghide();
$('#releases_'+id+'_closedlink').gshow();
2011-03-28 14:21:28 +00:00
$('#releases_'+id+'_defaultopen').raw().checked = 'checked';
2013-04-18 08:00:54 +00:00
} else {
2013-06-17 08:01:02 +00:00
$('#releases_'+id+'_openlink').gshow();
$('#releases_'+id+'_closedlink').ghide();
2013-02-22 08:00:24 +00:00
$('#releases_'+id+'_defaultopen').raw().checked = '';
2011-03-28 14:21:28 +00:00
}
}
function setShow(id, show) {
2013-04-18 08:00:54 +00:00
if (show) {
2013-06-17 08:01:02 +00:00
$('#releases_'+id+'_viewlink').ghide();
$('#releases_'+id+'_hidelink').gshow();
$('.releases_'+id).gshow();
2013-04-18 08:00:54 +00:00
} else {
2013-06-17 08:01:02 +00:00
$('#releases_'+id+'_viewlink').gshow();
$('#releases_'+id+'_hidelink').ghide();
$('.releases_'+id).ghide();
2011-03-28 14:21:28 +00:00
}
}
function startDrag(element) {
element.style.top = element.offsetTop + 'px';
element.style.left = element.offsetLeft + 'px';
element.style.height = element.offsetHeight + 'px';
element.style.width = element.offsetWidth + 'px';
element.style.position = 'absolute';
element.style.zIndex = '100';
$('body').objects[0].style.cursor = 'move';
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
dragObjectPlaceholder = document.createElement('TABLE');
dragObjectPlaceholder.style.backgroundColor = '#DDDDDD';
dragObjectPlaceholder.style.height = element.style.height;
dragObjectPlaceholder.style.width = element.style.width;
element.parentNode.insertBefore(dragObjectPlaceholder, element);
}
function moveDrag(element) {
2013-04-18 08:00:54 +00:00
if (
2013-02-22 08:00:24 +00:00
(element.offsetTop > (dragObjectPlaceholder.offsetTop + parseInt(dragObjectPlaceholder.style.height))) ||
2011-03-28 14:21:28 +00:00
((element.offsetTop + parseInt(dragObjectPlaceholder.style.height)) < dragObjectPlaceholder.offsetTop)
) {
var bestItem = 'END';
elems = element.parentNode.childNodes;
2013-02-22 08:00:24 +00:00
2013-06-17 08:01:02 +00:00
for (var i = 0; i < elems.length; i++) {
2011-03-28 14:21:28 +00:00
elem = elems[i];
2013-04-18 08:00:54 +00:00
if (elem == element || elem.nodeName != 'TABLE') {
continue;
}
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if ((element.offsetTop > dragObjectPlaceholder.offsetTop) && (elem.offsetTop - element.offsetTop) > parseInt(element.style.height)) {
2011-03-28 14:21:28 +00:00
bestItem = elem;
break;
2013-04-18 08:00:54 +00:00
} else if ((element.offsetTop < dragObjectPlaceholder.offsetTop) && (elem.offsetTop + parseInt(element.style.height)) > element.offsetTop) {
2011-03-28 14:21:28 +00:00
bestItem = elem;
break;
}
}
2013-04-18 08:00:54 +00:00
if (bestItem == dragObjectPlaceholder) {
return;
}
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (bestItem != 'END') {
element.parentNode.insertBefore(dragObjectPlaceholder, element.parentNode.childNodes[i]);
} else {
element.parentNode.appendChild(dragObjectPlaceholder);
}
2011-03-28 14:21:28 +00:00
}
}
function endDrag(element) {
$('body').objects[0].style.cursor = '';
element.style.top = '';
element.style.left = '';
element.style.zIndex = '';
element.style.position = '';
element.parentNode.replaceChild(element, dragObjectPlaceholder);
dragObjectPlaceholder = null;
}
//Slightly modified from: http://www.switchonthecode.com/tutorials/javascript-draggable-elements
function addEvent(element, eventName, callback) {
2013-04-18 08:00:54 +00:00
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
2011-03-28 14:21:28 +00:00
}
function removeEvent(element, eventName, callback) {
2013-04-18 08:00:54 +00:00
if (element.removeEventListener) {
element.removeEventListener(eventName, callback, false);
} else if (element.detachEvent) {
element.detachEvent("on" + eventName, callback);
}
2011-03-28 14:21:28 +00:00
}
function cancelEvent(e) {
e = e ? e : window.event;
2013-04-18 08:00:54 +00:00
if (e.stopPropagation) {
e.stopPropagation();
}
if (e.preventDefault) {
e.preventDefault();
}
2011-03-28 14:21:28 +00:00
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
return false;
}
function Position(x, y) {
this.X = x;
this.Y = y;
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.Add = function(val) {
var newPos = new Position(this.X, this.Y);
2013-04-18 08:00:54 +00:00
if (val != null) {
if (!isNaN(val.X)) {
newPos.X += val.X;
}
if (!isNaN(val.Y)) {
newPos.Y += val.Y;
}
2011-03-28 14:21:28 +00:00
}
return newPos;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.Subtract = function(val) {
var newPos = new Position(this.X, this.Y);
2013-04-18 08:00:54 +00:00
if (val != null) {
if (!isNaN(val.X)) {
newPos.X -= val.X;
}
if (!isNaN(val.Y)) {
newPos.Y -= val.Y;
}
2011-03-28 14:21:28 +00:00
}
return newPos;
2013-02-22 08:00:24 +00:00
}
2011-03-28 14:21:28 +00:00
this.Check = function() {
var newPos = new Position(this.X, this.Y);
2013-04-18 08:00:54 +00:00
if (isNaN(newPos.X)) {
newPos.X = 0;
}
if (isNaN(newPos.Y)) {
newPos.Y = 0;
}
2011-03-28 14:21:28 +00:00
return newPos;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.Apply = function(element, horizontal, vertical) {
2013-04-18 08:00:54 +00:00
if (!isNaN(this.X) && horizontal) {
element.style.left = this.X + 'px';
}
if (!isNaN(this.Y) && vertical) {
element.style.top = this.Y + 'px';
}
2011-03-28 14:21:28 +00:00
}
}
function absoluteCursorPostion(eventObj) {
eventObj = eventObj ? eventObj : window.event;
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (isNaN(window.scrollX)) {
2011-03-28 14:21:28 +00:00
return new Position(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
2013-04-18 08:00:54 +00:00
} else {
return new Position(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
2011-03-28 14:21:28 +00:00
}
}
function dragObject(element, handlerElement, startCallback, moveCallback, endCallback) {
2013-04-18 08:00:54 +00:00
if (typeof(element) == "string") {
element = $('#' + element).raw();
}
if (element == null) {
return;
}
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (typeof(handlerElement) == "string") {
handlerElement = $('#' + handlerElement).raw();
}
if (handlerElement == null) {
handlerElement = element;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var cursorStartPos = null;
var elementStartPos = null;
var dragging = false;
var listening = false;
var disposed = false;
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
function dragStart(eventObj) {
2013-04-18 08:00:54 +00:00
if (dragging || !listening || disposed) {
return;
}
2011-03-28 14:21:28 +00:00
dragging = true;
cursorStartPos = absoluteCursorPostion(eventObj);
elementStartPos = new Position(parseInt(element.offsetLeft), parseInt(element.offsetTop));
elementStartPos = elementStartPos.Check();
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (startCallback != null) {
startCallback(element);
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
addEvent(document, "mousemove", dragGo);
addEvent(document, "mouseup", dragStopHook);
return cancelEvent(eventObj);
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
function dragGo(eventObj) {
2013-04-18 08:00:54 +00:00
if (!dragging || disposed) {
return;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
var newPos = absoluteCursorPostion(eventObj);
newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
newPos.Apply(element, false, true);
2013-04-18 08:00:54 +00:00
if (moveCallback != null) {
moveCallback(element);
}
2011-03-28 14:21:28 +00:00
2013-02-22 08:00:24 +00:00
return cancelEvent(eventObj);
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
function dragStop() {
2013-04-18 08:00:54 +00:00
if (!dragging || disposed) {
return;
}
2011-03-28 14:21:28 +00:00
removeEvent(document, "mousemove", dragGo);
removeEvent(document, "mouseup", dragStopHook);
cursorStartPos = null;
elementStartPos = null;
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (endCallback != null) {
endCallback(element);
}
2011-03-28 14:21:28 +00:00
dragging = false;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
function dragStopHook(eventObj) {
dragStop();
return cancelEvent(eventObj);
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.Dispose = function() {
2013-04-18 08:00:54 +00:00
if (disposed) {
return;
}
2011-03-28 14:21:28 +00:00
this.StopListening(true);
element = null;
handlerElement = null
startCallback = null;
moveCallback = null
endCallback = null;
disposed = true;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.StartListening = function() {
2013-04-18 08:00:54 +00:00
if (listening || disposed) {
return;
}
2011-03-28 14:21:28 +00:00
listening = true;
addEvent(handlerElement, "mousedown", dragStart);
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
this.StopListening = function(stopCurrentDragging) {
2013-04-18 08:00:54 +00:00
if (!listening || disposed) {
return;
}
2011-03-28 14:21:28 +00:00
removeEvent(handlerElement, "mousedown", dragStart);
listening = false;
2013-02-22 08:00:24 +00:00
2013-04-18 08:00:54 +00:00
if (stopCurrentDragging && dragging) {
dragStop();
}
2011-03-28 14:21:28 +00:00
}
}