+
+var SethManager = {
+
+ view_id: null, // ID of current view
+ group_id: null, // ID of group shown
+
+ next_tab_no: 0,
+
+ disableEditLinks: false, // manual disable all links when dragging
+ cancelMove: true, // cancels moves (used when moving into tabs)
+ currentTabNo: -1, // tab_no of current tab
+
+ init: function(view_id, group_id, list_of_tabs) {
+ this.view_id = view_id;
+ this.group_id = group_id;
+
+ // create addList dialog
+ $("#addlist").dialog({
+ modal: true,
+ autoOpen: false,
+ height: 600,
+ width: 800,
+ buttons: {
+ "Fortryd": function() {
+ SethManager.closeAddListDialog();
+ }
+ },
+ close: function() {
+ SethManager.emptyListsAndUpdateCurrent();
+ $('#addlistiframe').attr('src', 'about:blank'); // make F5 work
+ }
+ });
+
+ // create edit element dialog
+ $("#editelement").dialog({
+ modal: true,
+ autoOpen: false,
+ height: 600,
+ width: 800,
+ buttons: {
+ "Fortryd": function() {
+ SethManager.closeEditDialog();
+ },
+ "Gem": function() {
+ SethManager.saveAndCloseEditDialog();
+ },
+ "Slet": function() {
+ SethManager.deleteAndCloseEditDialog();
+ }
+ },
+ close: function() {
+ $("#editiframe").attr('src', 'about:blank'); // make F5 work
+ }
+ });
+
+ // turn add/edit list links into open dialog
+ $("#addlist-link").click(function() {
+ SethManager.openAddListDialog();
+ return false;
+ });
+ $("#editlist-link").click(function() {
+ SethManager.openEditListDialog();
+ return false;
+ });
+
+ // remove list link
+ $("#removelist-link").click(function() {
+ SethManager.removeCurrentList();
+ return false;
+ });
+
+ // create filter tabs
+ $("#tabs").tabs({
+ show: function(event, ui) {
+ SethManager.showTab(event, ui);
+ SethManager.currentTabNo = $(ui.panel).data('tab_no');
+ }
+ });
+ var tabsul = $("#tabs-list");
+
+ // Allow to sort tabs
+ tabsul.sortable();
+
+ tabsul.bind('sortupdate', function(event, ui) {
+ SethManager.storeFilterOrder();
+ });
+
+ // create tabs
+ $.each(list_of_tabs, function(index, value) {
+ SethManager.addList(value.id, value.title);
+ });
+
+ },
+
+ // Callback and get a fresh list
+ updateList: function(tabno) {
+ var listDom = $("#list-" + tabno);
+ var list_id = listDom.data('list_id');
+ listDom.data('list_status', 'loading');
+
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'get',
+ listid: list_id
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data.list) {
+ SethManager.fillInList(tabno, data.list);
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+ },
+
+ // Fill in a list using return data
+ fillInList: function(tabno, listdata) {
+ // check for error (list returned as nested result)
+ if (listdata.err) {
+ this.commError(null, listdata);
+ return;
+ }
+
+ var listDom = $("#list-" + tabno);
+ var first = true;
+
+ listDom.empty();
+
+ listDom.data('list_status', 'loaded');
+
+ // We need to create a ul for each group
+ $.each(listdata.groups, function(index, value) {
+ var group = $('<div class="group-seperator">'+value.title+'</div>');
+ group.data('group_defined_as', value.defined_as);
+ group.data('group_when_placing_here', value.when_placing_here);
+ listDom.append(group);
+
+ var table = $('<ul class="ul-helper-reset table"/>');
+ // and for each element in the group create it
+ $.each(value.elements, function(index, value) {
+ var elmLi = $('<li class="ui-state-default element" />');
+ var elmDiv = $('<div class="draggable" />');
+ var elmA = $('<a class="element-'+value.id+'" href="#"/>');
+ // Add mouseover
+ elmDiv.attr('title', value.description);
+ // store meta data
+ elmLi.data('element_id', value.id);
+ // set onclick event
+ elmA.click(function() {
+ if (!SethManager.disableEditLinks) {
+ SethManager.editElement(value.id);
+ }
+ return false;
+ });
+ // set link title
+ elmA.text(value.title);
+ // append element
+ elmDiv.append(elmA);
+ elmLi.append(elmDiv);
+ table.append(elmLi);
+ });
+
+ listDom.append(group).append(table);
+
+ // create a new element button for each group
+ if (value.defined_as) {
+ var button = $('<input type="submit" value="Tilføj ny"/>').button();
+ var input = $('<input id="list-new-'+index+'" type="text"/>');
+ var form = $('<form class="ui-helper-reset"/>').submit(function() {
+ SethManager.createNew(this);
+ return false;
+ });
+
+ var new_li = $('<div class="ui-state-default new"></div>');
+ form.append(new_li.append(input).append(button));
+ listDom.append(form);
+ }
+ });
+
+ var tables = listDom.find("ul.table");
+ // Make drag'n'drop able
+ tables.sortable({
+ start: function() {
+ // disable links while dragging
+ SethManager.disableEditLinks = true;
+ SethManager.cancelMove = false;
+ },
+ stop: function() {
+ SethManager.cancelMove = true;
+ // Hack, but cannot seem to find any event that triggers at a proper time
+ setTimeout(function() {
+ SethManager.disableEditLinks = false;
+ }, 10);
+ },
+ connectWith: '#list-' + tabno + ' ul',
+ update: function(event, ui) {
+ SethManager.sortElement(event, ui);
+ }
+ });
+
+ },
+
+ // Create new element
+ createNew: function(form) {
+ form = $(form);
+ var text = form.find('input').get(0);
+ var input_id = text.id; // going back to this one afterwards
+ var list_id = form.parent().data('list_id');
+ var tab_no = form.parent().data('tab_no');
+ var list = form.prev();
+ var group_defined_as = list.prev().data('group_defined_as');
+ var title = text.value;
+
+ if ($.trim(title) == '') return; // have to enter something
+
+ text.disabled = true;
+
+ // create new element and add it to the list (before server update)
+ this.addNewElement(list, title);
+
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'new',
+ obj: {
+ title: title,
+ filter_id: list_id,
+ group_defined_as: group_defined_as
+ }
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data['new']) {
+ // success
+ // update list with result
+ SethManager.fillInList(tab_no, data.list);
+ // flush other tabs
+ SethManager.emptyAllListsButCurrent();
+ // Set focus back
+ $("#"+input_id).focus();
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+
+ },
+
+ commError: function(obj, data) {
+ if (data && data.err) {
+ alert(data.err);
+ } else {
+ /* silent ignore */
+// alert('Fejl ved kommunikation med server - genindlæs siden!');
+ }
+ },
+
+ // Add one element to the list
+ addNewElement: function(list, text) {
+ var li = $('<li class="ui-state-default">test</li>');
+ li.text(text);
+
+ list.append(li);
+ },
+
+ // Add a list to the tabs
+ addList: function(id, title, addedByUser) {
+ var tab_no = (this.next_tab_no++);
+ var tabs = $("#tabs");
+ var content = $('<div id="list-'+tab_no+'" class="filtered_list"></div>');
+
+ // store list id as meta data
+ content.data('tab_no', tab_no);
+ content.data('list_id', id);
+ content.data('list_status', 'not-loaded');
+
+ tabs.append(content);
+
+ // new tab
+ tabs.tabs("add", "#list-"+tab_no, title);
+
+ // allow to drop element on the tab
+ var tab = tabs.find("> ul:first > li > a[href='#list-"+tab_no+"']").parent();
+ var list_id = id;
+ tab.droppable({
+ accept: "ul.table li",
+ hoverClass: "ui-state-hover",
+ tolerance: 'pointer',
+ drop: function(ev, ui) {
+ var item = $(ui.draggable);
+ var element_id = item.data('element_id');
+ // Lets check if its the current list so we can ignore this
+ var cur_list_id = item.parents('div.filtered_list').data('list_id');
+ if (cur_list_id == list_id) {
+ alert('Allerede i den liste.');
+ } else {
+ // we have to mark the element dirty to avoid moving it
+ // back again, since the jquery sortable api will also generate an
+ // event
+ SethManager.cancelMove = true;
+ item.remove();
+ SethManager.moveElementToOtherList(element_id, list_id);
+ }
+ }
+ });
+
+ if (addedByUser) {
+ // select the tab and load it
+ tabs.tabs("select", "list-"+tab_no);
+ // store order
+ this.storeFilterOrder();
+ }
+
+ },
+
+ // Remove a list from tabs
+ removeCurrentList: function() {
+ var tabs = $("#tabs");
+ var selected = tabs.tabs('option', 'selected');
+ if (selected != -1) {
+ tabs.tabs('remove', selected);
+ SethManager.storeFilterOrder();
+ }
+ },
+
+ // Store new filter order in view
+ storeFilterOrder: function() {
+ var tabs = $("#tabs ul:first > li > a");
+ var order = [];
+ $.each(tabs, function() {
+ order[order.length] = $($(this).attr("href")).data('list_id');
+ });
+
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'setfilterorder',
+ view_id: this.view_id,
+ order: order
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data.save) {
+ // success, be quietly happy :-)
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+ },
+
+ // Drag element from one list to another
+ moveElementToOtherList: function(id, filter_id) {
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'move_to_list',
+ element_id: id,
+ filter_id: filter_id
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data.save) {
+ // success
+ SethManager.emptyListsAndUpdateCurrent();
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+ },
+
+ // Sort element between groups or in same list
+ sortElement: function(event, ui) {
+ // ignore events send from other list (group by)
+ // (the other group will handle it)
+ if (ui.sender) return;
+
+ // ignore if moving into a tab
+ if (this.cancelMove) return;
+
+ // we need to find where we were placed
+ // element before, after, and which group
+ var item = $(ui.item)
+ var elmId = item.data('element_id');
+ var above = item.prev('li');
+ var aboveId = above ? $(above).data('element_id') : null;
+ var below = item.next('li');
+ var belowId = below ? $(below).data('element_id') : null;
+
+ // find our filter id
+ var filterList = item.parents('div.filtered_list');
+ var filterId = filterList.data('list_id');
+ var tabno = filterList.data('tab_no');
+
+ // expect to have a group header just above the <ul />
+ var group_defined_as = item.parent().prev().data('group_defined_as');
+
+ if (!group_defined_as) {
+ alert('Fix me, cannot find my group');
+ }
+
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'move_element',
+ obj: {
+ element_id: elmId,
+ above_id: aboveId,
+ below_id: belowId,
+ group_defined_as: group_defined_as,
+ filter_id: filterId
+ }
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data.moved && data.list) {
+ SethManager.fillInList(tabno, data.list);
+ // flush other tabs
+ SethManager.emptyAllListsButCurrent();
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+ },
+
+ // Flushes all lists after update and updates the current one
+ // excludeCurrent - true to not update current (done otherwise)
+ emptyListsAndUpdateCurrent: function(excludeCurrent) {
+ var tabs = $("#tabs");
+ var selected = tabs.tabs('option', 'selected');
+ var curTabNo = null;
+ var curListId = null;
+ if (selected != -1) {
+ curTabNo = this.currentTabNo;
+ curListId = "#list-" + curTabNo;
+ }
+ // wipe all lists except current
+ $.each(tabs.find("> div"), function() {
+ if (("#"+this.id) != curListId) {
+ $(this).empty();
+ $(this).data('list_status', 'not-loaded');
+ }
+ });
+
+ // update current list
+ if (curListId && !excludeCurrent) {
+ this.updateList(curTabNo);
+ }
+ },
+
+ // Flush all lists except current
+ emptyAllListsButCurrent: function() {
+ this.emptyListsAndUpdateCurrent(true);
+ },
+
+ // Display tab, load content is not already done
+ showTab: function(event, ui) {
+ var panel = $(ui.panel);
+ if (panel.data('list_status') == 'not-loaded') {
+ this.updateList(panel.data('tab_no'));
+ }
+ },
+
+ // Delete specific element
+ deleteElement: function(id) {
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'delete_element',
+ element_id: id
+ },
+ dataType: 'json',
+ error: function() {
+ SethManager.commError(this);
+ },
+ success: function(data) {
+ if (data && data['delete']) {
+ SethManager.emptyListsAndUpdateCurrent();
+ } else {
+ SethManager.commError(this, data);
+ }
+ }
+ });
+ },
+
+// ---------------------- Edit Element dialog ----------------------------------
+//
+ // Open dialog to edit element
+ editElement: function(id) {
+ $("#editelement").data('element_id', id);
+ $("#editiframe").attr('src', 'edit.php?id=' + id);
+ $("#editelement").dialog("open");
+ },
+
+ closeEditDialog: function() {
+ $("#editelement").dialog("close");
+ },
+
+ updateThenCloseEditDialog: function(id, title) {
+ $(".element-"+id).text(title);
+ this.emptyListsAndUpdateCurrent();
+ this.closeEditDialog();
+ },
+
+ saveAndCloseEditDialog: function () {
+ $("#editiframe").contents().find("form").submit();
+ },
+
+ deleteAndCloseEditDialog: function () {
+ if (confirm('Er du sikker på at slette dette element?')) {
+ var elementId = $("#editelement").data('element_id');
+ $.each($(".element-"+elementId), function() {
+ $(this).parent().parent().remove();
+ });
+ this.deleteElement(elementId);
+ this.closeEditDialog();
+ }
+ },
+
+// ---------------------- Add Filter dialog -------------------------------------
+
+ // Open dialog to add list
+ openAddListDialog: function(id) {
+ $("#addlistiframe").attr('src', 'addfilter.php?group_id=' + this.group_id);
+ $("#addlist").dialog("open");
+ },
+
+ // Open dialog to edit list
+ openEditListDialog: function(id) {
+ var tabs = $("#tabs");
+ var selected = tabs.tabs('option', 'selected');
+ if (selected != -1) {
+ var id = $('#list-' + this.currentTabNo).data('list_id');
+
+ $("#addlistiframe").attr('src', 'editfilter.php?id=' + id);
+ $("#addlist").dialog("open");
+ }
+ },
+
+ closeAddListDialog: function() {
+ $("#addlist").dialog("close");
+ },
+
+ addListAndCloseDialog: function(id, title) {
+ // Notice that the dialog uses will escape the title parameter
+ // as an URL-encoded string so we must decode:
+ title = unescape(title);
+ this.closeAddListDialog();
+ this.addList(id, title, true);
+ },
+
+// ------------------------ addFilter.php ---------------------------------------
+ initAddFilter: function() {
+ // auto submit form when changing group
+ $("#group_id").bind('change', function() {
+ $(this).parents('form').get(0).submit();
+ });
+ },
+
+ removeFilter: function(id, title) {
+ if (confirm('Er du sikker på at slettet "'+unescape(title)+'" ?')) {
+ // do callback
+ $.ajax({
+ type: 'POST',
+ data: {
+ action: 'remove',
+ id: id,
+ },
+ dataType: 'json',
+ error: function() {
+ alert('Server fejl');
+ },
+ success: function(data) {
+ if (data && data.ok) {
+ window.location.reload();
+ }
+ }
+ });
+ }
+ }
+
+}