From 7a64053cb818931e38af8c9138a259a711ea4da7 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Mon, 7 Jul 2014 19:37:19 -0300 Subject: Improve javascript code and remove CSP errors --- assets/js/app.js | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++++ assets/js/board.js | 203 ------------------------------------------ assets/js/task.js | 33 ------- 3 files changed, 255 insertions(+), 236 deletions(-) create mode 100644 assets/js/app.js delete mode 100644 assets/js/board.js delete mode 100644 assets/js/task.js (limited to 'assets/js') diff --git a/assets/js/app.js b/assets/js/app.js new file mode 100644 index 00000000..2b65da99 --- /dev/null +++ b/assets/js/app.js @@ -0,0 +1,255 @@ + +// Common functions +var Kanboard = (function() { + + return { + + // Display a popup + Popover: function(e, callback) { + e.preventDefault(); + e.stopPropagation(); + + $.get(e.target.getAttribute("href"), function(content) { + + $("body").append('
' + content + '
'); + + $("#popover-container").click(function() { + $(this).remove(); + }); + + $("#popover-content").click(function(e) { + e.stopPropagation(); + }); + + if (callback) { + callback(); + } + }); + }, + + // Return true if the page is visible + IsVisible: function() + { + var property = ""; + + if (typeof document.hidden !== "undefined") { + property = "visibilityState"; + } else if (typeof document.mozHidden !== "undefined") { + property = "mozVisibilityState"; + } else if (typeof document.msHidden !== "undefined") { + property = "msVisibilityState"; + } else if (typeof document.webkitHidden !== "undefined") { + property = "webkitVisibilityState"; + } + + if (property != "") { + return document[property] == "visible"; + } + + return true; + } + }; + +})(); + + +// Board related functions +Kanboard.Board = (function() { + + var checkInterval = null; + + // Setup the board + function board_load_events() + { + // Drag and drop + $(".column").sortable({ + connectWith: ".column", + placeholder: "draggable-placeholder", + stop: function(event, ui) { + board_save(); + } + }); + + // Assignee change + $(".assignee-popover").click(Kanboard.Popover); + + // Task edit popover + $(".task-edit-popover").click(function(e) { + Kanboard.Popover(e, Kanboard.Task.Init); + }); + + // Redirect to the task details page + $("[data-task-id]").each(function() { + $(this).click(function() { + window.location = "?controller=task&action=show&task_id=" + $(this).attr("data-task-id"); + }); + }); + + // Automatic refresh + var interval = parseInt($("#board").attr("data-check-interval")); + + if (interval > 0) { + checkInterval = window.setInterval(board_check, interval * 1000); + } + } + + // Stop events + function board_unload_events() + { + $("[data-task-id]").off(); + clearInterval(checkInterval); + } + + // Save and refresh the board + function board_save() + { + var data = []; + var boardSelector = $("#board"); + var projectId = boardSelector.attr("data-project-id"); + + board_unload_events(); + + $(".column").each(function() { + var columnId = $(this).attr("data-column-id"); + + $("#column-" + columnId + " .task-board").each(function(index) { + data.push({ + "task_id": parseInt($(this).attr("data-task-id")), + "position": index + 1, + "column_id": parseInt(columnId) + }); + }); + }); + + $.ajax({ + cache: false, + url: "?controller=board&action=save&project_id=" + projectId, + data: {"positions": data, "csrf_token": boardSelector.attr("data-csrf-token")}, + type: "POST", + success: function(data) { + $("#board").remove(); + $("#main").append(data); + board_load_events(); + filter_apply(); + } + }); + } + + // Check if a board have been changed by someone else + function board_check() + { + var boardSelector = $("#board"); + var projectId = boardSelector.attr("data-project-id"); + var timestamp = boardSelector.attr("data-time"); + + if (Kanboard.IsVisible() && projectId != undefined && timestamp != undefined) { + $.ajax({ + cache: false, + url: "?controller=board&action=check&project_id=" + projectId + "×tamp=" + timestamp, + statusCode: { + 200: function(data) { + boardSelector.remove(); + $("#main").append(data); + board_unload_events(); + board_load_events(); + filter_apply(); + } + } + }); + } + } + + // Apply user or date filter (change tasks opacity) + function filter_apply() + { + var selectedUserId = $("#form-user_id").val(); + var selectedCategoryId = $("#form-category_id").val(); + var filterDueDate = $("#filter-due-date").hasClass("filter-on"); + + $("[data-task-id]").each(function(index, item) { + + var ownerId = item.getAttribute("data-owner-id"); + var dueDate = item.getAttribute("data-due-date"); + var categoryId = item.getAttribute("data-category-id"); + + if (ownerId != selectedUserId && selectedUserId != -1) { + item.style.opacity = "0.2"; + } + else { + item.style.opacity = "1.0"; + } + + if (filterDueDate && (dueDate == "" || dueDate == "0")) { + item.style.opacity = "0.2"; + } + + if (categoryId != selectedCategoryId && selectedCategoryId != -1) { + item.style.opacity = "0.2"; + } + }); + } + + // Load filter events + function filter_load_events() + { + $("#form-user_id").change(filter_apply); + + $("#form-category_id").change(filter_apply); + + $("#filter-due-date").click(function(e) { + $(this).toggleClass("filter-on"); + filter_apply(); + e.preventDefault(); + }); + } + + return { + Init: function() { + board_load_events(); + filter_load_events(); + + // Project select box + $("#board-selector").chosen({ + width: 180 + }); + + $("#board-selector").change(function() { + window.location = "?controller=board&action=show&project_id=" + $(this).val(); + }); + } + }; + +})(); + + +// Task related functions +Kanboard.Task = (function() { + + return { + Init: function() { + + // Datepicker for the due date + $("#form-date_due").datepicker({ + showOtherMonths: true, + selectOtherMonths: true, + dateFormat: 'yy-mm-dd' + }); + + // Image preview for attachments + $(".file-popover").click(Kanboard.Popover); + } + }; + +})(); + + +// Initialization +$(function() { + + if ($("#board").length) { + Kanboard.Board.Init(); + } + else { + Kanboard.Task.Init(); + } +}); diff --git a/assets/js/board.js b/assets/js/board.js deleted file mode 100644 index 170cbbe2..00000000 --- a/assets/js/board.js +++ /dev/null @@ -1,203 +0,0 @@ -(function () { - - var checkInterval = null; - - // Setup the board - function board_load_events() - { - // Drag and drop - $(".column").sortable({ - connectWith: ".column", - placeholder: "draggable-placeholder", - stop: function(event, ui) { - board_save(); - } - }); - - // Open assignee popover - $(".task-board-popover").click(function(e) { - - e.preventDefault(); - e.stopPropagation(); - - var href = $(this).attr('href'); - - $.get(href, function(data) { - popover_show(data); - }); - }); - - // Redirect to the task details page - $("[data-task-id]").each(function() { - $(this).click(function() { - window.location = "?controller=task&action=show&task_id=" + $(this).attr("data-task-id"); - }); - }); - - // Automatic refresh - var interval = parseInt($("#board").attr("data-check-interval")); - - if (interval > 0) { - checkInterval = window.setInterval(board_check, interval * 1000); - } - } - - // Stop events - function board_unload_events() - { - $("[data-task-id]").off(); - clearInterval(checkInterval); - } - - // Save and refresh the board - function board_save() - { - var data = []; - var $boardSelector = $("#board"); - var projectId = $boardSelector.attr("data-project-id"); - - board_unload_events(); - - $(".column").each(function() { - var columnId = $(this).attr("data-column-id"); - - $("#column-" + columnId + " .task-board").each(function(index) { - data.push({ - "task_id": parseInt($(this).attr("data-task-id")), - "position": index + 1, - "column_id": parseInt(columnId) - }); - }); - }); - - $.ajax({ - cache: false, - url: "?controller=board&action=save&project_id=" + projectId, - data: {"positions": data, "csrf_token": $boardSelector.attr("data-csrf-token")}, - type: "POST", - success: function(data) { - $("#board").remove(); - $("#main").append(data); - board_load_events(); - filter_apply(); - } - }); - } - - // Check if a board have been changed by someone else - function board_check() - { - var $boardSelector = $("#board"); - var projectId = $boardSelector.attr("data-project-id"); - var timestamp = $boardSelector.attr("data-time"); - - if (is_visible() && projectId != undefined && timestamp != undefined) { - $.ajax({ - cache: false, - url: "?controller=board&action=check&project_id=" + projectId + "×tamp=" + timestamp, - statusCode: { - 200: function(data) { - $boardSelector.remove(); - $("#main").append(data); - board_unload_events(); - board_load_events(); - filter_apply(); - } - } - }); - } - } - - // Apply user or date filter (change tasks opacity) - function filter_apply() - { - var selectedUserId = $("#form-user_id").val(); - var selectedCategoryId = $("#form-category_id").val(); - var filterDueDate = $("#filter-due-date").hasClass("filter-on"); - - $("[data-task-id]").each(function(index, item) { - - var ownerId = item.getAttribute("data-owner-id"); - var dueDate = item.getAttribute("data-due-date"); - var categoryId = item.getAttribute("data-category-id"); - - if (ownerId != selectedUserId && selectedUserId != -1) { - item.style.opacity = "0.2"; - } - else { - item.style.opacity = "1.0"; - } - - if (filterDueDate && (dueDate == "" || dueDate == "0")) { - item.style.opacity = "0.2"; - } - - if (categoryId != selectedCategoryId && selectedCategoryId != -1) { - item.style.opacity = "0.2"; - } - }); - } - - // Load filter events - function filter_load_events() - { - $("#form-user_id").change(filter_apply); - - $("#form-category_id").change(filter_apply); - - $("#filter-due-date").click(function(e) { - $(this).toggleClass("filter-on"); - filter_apply(); - e.preventDefault(); - }); - } - - // Show popup - function popover_show(content) - { - $("body").append('
' + content + '
'); - - $("#popover-container").click(function() { - $(this).remove(); - }); - - $("#popover-content").click(function(e) { - e.stopPropagation(); - }); - } - - // Return true if the page is visible - function is_visible() - { - var property = ""; - - if (typeof document.hidden !== "undefined") { - property = "visibilityState"; - } else if (typeof document.mozHidden !== "undefined") { - property = "mozVisibilityState"; - } else if (typeof document.msHidden !== "undefined") { - property = "msVisibilityState"; - } else if (typeof document.webkitHidden !== "undefined") { - property = "webkitVisibilityState"; - } - - if (property != "") { - return document[property] == "visible"; - } - - return true; - } - - // Initialization - $(function() { - board_load_events(); - filter_load_events(); - - $("#board-selector").chosen(); - - $("#board-selector").change(function() { - window.location = "?controller=board&action=show&project_id=" + $(this).val(); - }); - }); - -}()); diff --git a/assets/js/task.js b/assets/js/task.js deleted file mode 100644 index 13f48348..00000000 --- a/assets/js/task.js +++ /dev/null @@ -1,33 +0,0 @@ -(function () { - - // Show popup - function popover_show(content) - { - $("body").append('
' + content + '
'); - - $("#popover-container").click(function() { - $(this).remove(); - }); - - $("#popover-content").click(function(e) { - e.stopPropagation(); - }); - } - - $(".popover").click(function(e) { - - e.preventDefault(); - e.stopPropagation(); - - $.get($(this).attr("href"), function(data) { - popover_show(data); - }); - }); - - $("#form-date_due").datepicker({ - showOtherMonths: true, - selectOtherMonths: true, - dateFormat: 'yy-mm-dd' - }); - -}()); -- cgit v1.2.3