/*
* @todo Trap resize of dialog and resize the child areas (tree, grid and edit form)
* @todo Create a message area in the footer for all the organize messages
*/
var url;
var height;
var paused = false;
var task = null;
var transitItems = [];
// **************************************************************************
// JQuery UI Widgets
// Draggable
var draggable = {
cancel: ".gMicroThumbContainer:not(.ui-selected)",
handle: ".gMicroThumbContainer.ui-selected",
revert: true,
zindex: 2000,
helper: function(event, ui) {
console.dir(ui);
$("#gMicroThumbPanel").append("
");
var beginTop = event.pageY;
var beginLeft = event.pageX;
var zindex = $(".gMicroThumbContainer").draggable("option", "zindex");
$("#gDragHelper").css('top', event.pageY - 22.5);
$("#gDragHelper").css('left', event.pageX + 22.5);
var placeHolder = $(this).clone();
$(placeHolder).attr("id", "gPlaceHolder");
$(placeHolder).css("visibility", "hidden");
$(placeHolder).removeClass("ui-selected");
$(placeHolder).removeClass("ui-draggable");
$(this).after(placeHolder);
$("li.ui-selected").each(function(i) {
var clone = $(this).clone();
$(clone).attr("id", "drag_clone_" + $(this).attr("ref"));
$("#gDragHelper ul").append(clone);
$(clone).css("position", "absolute");
$(clone).css("top", beginTop);
$(clone).css("left", beginLeft);
$(clone).css("z-index", zindex--);
$(this).hide();
var children = $(clone).find(".gMicroThumb .gThumbnail");
var width = new String(children.css("width")).replace(/[^0-9]/g,"") * .5;
height = new String(children.css("height")).replace(/[^0-9]/g,"") * .5;
var marginTop = new String(children.css("margin-top")).replace(/[^\.0-9]/g,"") * .5;
children.attr("width", width);
children.attr("height", height);
children.css("margin-top", marginTop);
if (i < 9) {
beginTop -= 5;
beginLeft += 5;
}
});
return $("#gDragHelper");
},
stop: function(event, ui) {
$("#gDragHelper li").each(function(i) {
$("#thumb_" + $(this).attr("ref")).show();
});
$(".gMicroThumbContainer.ui-selected").css("z-index", null);
$("#gDragHelper").remove();
$("#gPlaceHolder").remove();
}
};
// Thumbnail Grid Droppable
var thumbDroppable = {
tolerance: "pointer",
over: function(event, ui) {
$("#gPlaceHolder").show();
},
out: function(event, ui) {
$("#gPlaceHolder").hide();
},
drop: function(event, ui) {
$("#gDragHelper").hide();
$("#gPlaceHolder").hide();
var newOrder = "";
$("#gMicroThumbGrid .gMicroThumbContainer").each(function(i) {
if ($(this).attr("id") == "gPlaceHolder") {
$("#gDragHelper li").each(function(i) {
newOrder += "&item[]=" + $(this).attr("ref");
});
} else if ($(this).css("display") != "none") {
newOrder += "&item[]=" + $(this).attr("ref");
} else {
// If its not displayed then its one of the ones being moved so ignore.
}
});
$("#gDragHelper li").each(function(i) {
$("#gPlaceHolder").before($("#thumb_" + $(this).attr("ref")).show());
});
$.ajax({
data: newOrder,
dataType: "json",
success: startRearrangeCallback,
type: "POST",
url: get_url("organize/rearrangeStart", {item_id: item_id})
});
}
};
// Album Tree Droppable
var treeDroppable = {
tolerance: "pointer",
greedy: true,
hoverClass: "gBranchDroppable",
drop: function(event, ui) {
$("#gDragHelper").hide();
var moveItems = "";
var targetItemId = $(this).attr("ref");
if ($(this).hasClass("gBranchSelected")) {
$("#gOrganizeStatus").empty().append(INVALID_DROP_TARGET);
ui.draggable.trigger("stop", event);
return false;
}
var okToMove = true;
$("#gDragHelper li").each(function(i) {
moveItems += "&item[]=" + $(this).attr("ref");
okToMove &= targetItemId != $(this).attr("ref");
});
if (!okToMove) {
$("#gOrganizeStatus").empty().append(INVALID_DROP_TARGET);
ui.draggable.trigger("stop", event);
return false;
}
$("#gDragHelper li").each(function(i) {
$("#thumb_" + $(this).attr("ref")).remove();
});
$.ajax({
data: moveItems,
dataType: "json",
success: startMoveCallback,
type: "POST",
url: get_url("organize/moveStart", {item_id: targetItemId})
});
}
};
// Selectable
var selectable = {
count: 0,
filter: ".gMicroThumbContainer",
selected: function(event, ui) {
/*
* Count the number of selected items if it is greater than 1,
* then click won't be called so we need to remove the gSelecting
* class in the stop event.
*/
var count = $("#gMicroThumbGrid").selectable("option", "count") + 1;
$("#gMicroThumbGrid").selectable("option", "count", count);
$(ui.selected).addClass("gSelecting");
},
stop: function(event) {
var count = $("#gMicroThumbGrid").selectable("option", "count");
if (count > 1) {
$(".gMicroThumbContainer.gSelecting").removeClass("gSelecting");
}
$("#gMicroThumbGrid").selectable("option", "count", 0);
}
};
// **************************************************************************
// Event Handlers
// MicroThumbContainer click
var onMicroThumbContainerClick = function(event) {
if ($(this).hasClass("gSelecting")) {
$(this).removeClass("gSelecting");
} else {
$(this).removeClass("ui-selected");
}
};
// MicroThumbContainer mousemove
var onMicroThumbContainerMousemove = function(event) {
if ($("#gDragHelper").length > 0 && $(this).attr("id") != "gPlaceHolder") {
if (event.pageX < $(this).offset().left + $(this).width() / 2) {
$(this).before($("#gPlaceHolder"));
} else {
$(this).after($("#gPlaceHolder"));
}
var container = $("#gMicroThumbPanel").get(0);
var scrollHeight = container.scrollHeight;
var scrollTop = container.scrollTop;
var height = $(container).height();
if (event.pageY > height + scrollTop) {
container.scrollTop = this.offsetTop;
} else if (event.pageY < scrollTop) {
container.scrollTop -= height;
}
}
};
// Handle click events on the buttons on the drawer handle
function drawerHandleButtonsClick(event) {
event.preventDefault();
if (!$(this).attr("disabled")) {
var operation = $(this).attr("ref");
switch (operation) {
case "edit":
$("#gOrganizeEditDrawerPanel").slideToggle("normal");
break;
case "select-all":
$(".gMicroThumbContainer").addClass("ui-selected");
$("#gMicroThumbSelectAll").hide();
$("#gMicroThumbUnselectAll").show();
break;
case "unselect-all":
$(".gMicroThumbContainer").removeClass("ui-selected");
$("#gMicroThumbSelectAll").show();
$("#gMicroThumbUnselectAll").hide();
break;
case "close":
$("#gDialog").dialog("close");
break;
default:
console.log(operation);
}
}
};
// **************************************************************************
// AJAX Callbacks
// MicroThumbContainer click
var getMicroThumbsCallback = function(json, textStatus) {
if (json.count > 0) {
$("#gMicroThumbGrid").append(json.data);
retrieveMicroThumbs();
$(".gMicroThumbContainer").click(onMicroThumbContainerClick);
$(".gMicroThumbContainer").mousemove(onMicroThumbContainerMousemove);
$(".gMicroThumbContainer").draggable(draggable);
}
};
// @todo see if we can combine the next two callbacks into an object
// as they are basically the same.
var startMoveCallback = function (data, textStatus) {
if (!paused) {
createProgressDialog(OPERATION_RUNNING);
task = data.task;
task.pauseMsg = MOVE_PAUSED;
task.resumeMSg = MOVE_RESUMED;
}
$(".gMicroThumbContainer").draggable("disable");
var done = false;
paused = false;
while (!done && !paused) {
$.ajax({async: false,
success: function(data, textStatus) {
$(".gProgressBar").progressbar("value", data.task.percent_complete);
done = data.task.done;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
paused = true;
displayAjaxError(XMLHttpRequest.responseText);
},
dataType: "json",
type: "POST",
url: get_url("organize/runTask", {task_id: task.id})
});
}
if (!paused) {
$("#gOrganizeProgressDialog").dialog("destroy").remove();
$.ajax({async: false,
success: function(data, textStatus) {
task = null;
transitItems = [];
$("#gOrganizeStatus").empty().append("