Skip to content

Instantly share code, notes, and snippets.

@thaniaclair
Created May 11, 2013 20:27
Show Gist options
  • Save thaniaclair/5561320 to your computer and use it in GitHub Desktop.
Save thaniaclair/5561320 to your computer and use it in GitHub Desktop.
Gerenciador de árvore JS.
/**
* Gerencia os eventos para criação e atualização de tópicos e temas.
* @author Thania
*/
var gerenciadorArvore = {
/**
* Instala os eventos necessários após o carregamento total da página.
*/
init: function() {
gerenciadorArvore.appendPriorityList();
gerenciadorArvore.installThemeSorting();
gerenciadorArvore.installTopicSorting();
gerenciadorArvore.installNavigationEvent();
},
/**
* Adiciona a lista de prioridades para todos os tópicos para posterior ordenação.
*/
appendPriorityList: function() {
$(".prioridades").each(function(idx, element) {
$(this).removeClass("invisible");
var $header = $(this).next("table").find("th.principal-table");
$header.find(".prioridades").remove();
$header.prepend($(this));
});
},
/**
* Instala o evento de ordenação de temas através de movimentos drag-and-drop das linhas da
* tabela.
*/
installThemeSorting: function() {
$("table.sorting tbody").sortable(gerenciadorArvore.getConfigSorting());
},
/**
* Instala o evento de ordenação de tópicos através do combobox com as prioridades.
*/
installTopicSorting: function() {
$(".principal-table .prioridades select").bind("change", function() {
gerenciadorArvore.sortTopic($(this));
});
},
/**
* Atualiza o efeito de zebrado nas linhas da tabela.
*/
updateZebraEffects: function($tbody) {
var $lines = $tbody.find("tr");
$lines.removeClass("zebra");
$lines.each(function(index, element) {
var $line = $(element);
if (index % 2 == 1) $line.addClass("zebra");
});
},
/**
* Recupera a configuração padrão para o drag-and-drop de temas.
*/
getConfigSorting: function() {
return {
items: "tr",
forcePlaceholderSize: true,
forceHelperSize: true,
tolerance: "pointer",
containment: "parent",
update: function(event, ui) {
var $line = ui["item"];
gerenciadorArvore.updateZebraEffects($line.parent());
gerenciadorArvore.sortThemes($line);
}
};
},
/**
* Recupera os identificadores dos temas do tópico sendo alterado.
* @param $tbody é o corpo da tabela com os temas da árvore.
*/
getThemesID: function($tbody) {
var $lines = $tbody.find("tr");
var temas = new Array();
$lines.each(function(index, element) {
temas[index] = $(element).attr("id").split("-")[1];
});
return temas;
},
/**
* Solicita a ordenação server-side dos temas via AJAX.
* @param $line linha com o tema que foi reordenado.
*/
sortThemes: function($line) {
var temasID = gerenciadorArvore.getThemesID($line.parent());
var params = { "temasID" : temasID };
var url = SIS.basics.getPath() + "ajax/ordenarTemas";
$.ajax({ type: "post", url: url, data: params, dataType: "json",
success: function (json) {
var view = json.temaView;
var $table = $line.closest("table");
gerenciadorArvore.showFeedbackMsg(view, $table);
}
});
},
/**
* Mostra uma mensagem de retorno ao usuário após a ordenação.
*/
showFeedbackMsg: function(view, $table) {
var cssClass = "actionMessages";
if (view.error) cssClass = "actionErrors";
if ($table.prev("." + cssClass).size() > 0) return;
var msgBlock = "<div class='" + cssClass + "'>" + view.message + "</div>";
var $msgBlock = $table.before(msgBlock).prev();
$msgBlock.delay(1500).fadeOut('slow', function() {
$msgBlock.remove();
});
},
/**
* Solicita a ordenação server-side do tópico via AJAX.
* @param $select o combobox selecionado pelo usuário para ordenação.
*/
sortTopic: function($select) {
var posicao = $select.val();
if (posicao == '-') return;
var idTopico = $select.attr("id").split("-")[2];
var params = { "idTopico" : idTopico, "posicao" : posicao };
var url = SIS.basics.getPath() + "ajax/ordenarTopico";
$.ajax({ type: "post", url: url, data: params, dataType: "json",
success: function (json) {
var url = $(location).attr("href");
$(location).attr("href", url);
}
});
},
/**
* Instala os eventos de navegação e persistência do período de avaliação.
*/
installNavigationEvent: function() {
$(".navigation .actions a.salvar").bind("click", function(e) {
e.preventDefault();
var action = $(this).attr("href");
var $form = $(this).closest("form");
$form.attr("action", action);
$form.submit();
});
$(".navigation .actions a.navegar").bind("click", function(e) {
e.preventDefault();
$(this).closest("form").submit();
});
}
};
/**
* Ações executadas após o carregamento da página.
*/
$(function() {
gerenciadorArvore.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment