/src/main/resources/js/status-updates.js
https://bitbucket.org/aragot/status-updates-for-confluence · JavaScript · 182 lines · 143 code · 29 blank · 10 comment · 15 complexity · d89cfce6349822eeb0a435733fd1823a MD5 · raw file
- AJS.$(function($) {
- AJS.log("status-updates initialising");
- // Workaround for the top bar icon
- $("#status-updates-anchor").text(" ");
- function createSidebar() {// return;
- // We don't modify the display:block of main. We wrap it in a div with display:table-cell.
- var $main = $("#main");
- $main.wrap("<div id='main-wrapper'/>").wrap("<div/>").wrap("<div/>");
- $main.parent().parent().append("<div id='status-updates-sidebar'/>");
- var $sidebar = $("#status-updates-sidebar");
- var $template = $(StatusUpdates.Templates.statusUpdatesSidebar());
- $sidebar.append($template);
- }
- createSidebar();
- $.get(AJS.contextPath() + '/rest/status/latest/by-status', function(data) {
- var $statusContainer = $('.user-statuses');
- $statusContainer.empty().append($(StatusUpdates.Templates.renderStatusUpdates({statusUpdates: data})));
- });
-
- // ========================== The Update Status popup =========================
- var popup,
- maxChars = 140;
- function createPopUp() {
- /** HACK: Passing in a non-falsy value results in an invalid attribute value for the dialog's height.
- * This actually results in the default browser value of 'auto' being given to the dialog.
- * This hack enables the dialog's contents to dictate its size, thus enabling better i18n support.
- */
- var notAHeight = 'idontthinksohal';
- var popup = new AJS.Dialog(650, notAHeight, "update-user-status-dialog");
- var $dialog = popup.popup.element;
- var template = $(StatusUpdates.Templates.dialogContent({maxChars: maxChars}));
- popup.addHeader(AJS.I18n.getText("status.dialog.heading"));
- popup.addPanel(AJS.I18n.getText("status.dialog.panel.title"), template);
- popup.addButton(AJS.I18n.getText("status.dialog.button.update"), updateStatus, "status-update-button");
-
- popup.addCancel(AJS.I18n.getText("cancel.name"), function (dialog) {dialog.hide(); return false;});
- popup.setError = function(html) {
- $(".error-message", $dialog).html(html)
- };
-
- // add shortcut tip
- if (Confluence.KeyboardShortcuts && Confluence.KeyboardShortcuts.enabled) {
- popup.addHelpText(AJS.I18n.getText("keyboard.shortcuts.dialog.tip", "s"));
- }
-
- return popup;
- }
-
- function validate(html) {
- var error,
- text = $(html).text();
-
- if (!text) {
- error = AJS.I18n.getText("status.message.error.blank");
- } else if (!$.trim(text)) {
- // The message was just whitespace
- error = AJS.I18n.getText("status.message.error.onlywhitespace");
- } else if (text.length > maxChars) {
- error = AJS.I18n.getText("status.message.error.too.long", maxChars);
- }
-
- if (error) {
- popup.setError(error);
- }
-
- return !error;
- }
-
- function setCurrentStatus(status) {
- $(".current-user-latest-status .status-text").html($("<div/>").html($("<div/>").html(status.text).text()).text());
-
- $(".current-user-latest-status a[id^=view]").each(function() {
- var $this = $(this),
- href = $this.attr("href");
- $this.attr("href", href.replace(/\d+$/, status.id))
- .text(status.friendlyDate)
- .attr("title", new Date(status.date).toLocaleString());
- });
- }
-
- function getLatestStatus() {
- $.getJSON(Confluence.getContextPath() + "/status/current.action", function(data) {
- // TODO switch to MessageHandler
- if (data.errorMessage) {
- popup.setError(data.errorMessage);
- }
- else {
- setCurrentStatus(data);
- }
- });
- }
-
- var updateStatus = function() {
- var $dialog = popup.popup.element,
- ed = AJS.Rte.getEditor(),
- $updateButton = $(".status-update-button", $dialog),
- html = AJS.Rte.getEditor().getContent({format: 'raw'}),
- reEnableForm,
- updateTask;
-
- if (!validate(html)) {
- return false;
- }
-
- updateTask = AJS.safe.ajax({
- url: Confluence.getContextPath() + "/status/update.action",
- type: "POST",
- dataType: "json",
- data: {
- "text": html //text
- }
- });
-
- updateTask.done(function(data) {
- if (data.errorMessage) {
- popup.setError(data.errorMessage);
- }
- else {
- setCurrentStatus(data);
- ed.setContent("");
- $dialog.fadeOut(200, function() {
- popup.hide();
- });
- }
- });
-
- updateTask.fail(function(xhr, text, error) {
- AJS.log("Error updating status: " + text);
- AJS.log(error);
- popup.setError("There was an error - " + error);
- });
-
- return updateTask.promise();
- };
-
- var bindBehaviour = function(popup) {
- var $dialog = popup.popup.element,
- ed = AJS.Rte.getEditor(),
- $charsLeft = $(".chars-left", $dialog),
- $updateButton = $(".status-update-button", $dialog);
-
- ed.onKeyUp.add(function(ed, keyboardEvent){
- var contents = ed.getContent(),
- userLength = $(contents).text().trim().length,
- remainder = maxChars - userLength;
-
- console.log("Count", remainder, userLength, contents);
- $updateButton[remainder > 0 ? 'removeAttr' : 'attr']("disabled", "disabled");
- $charsLeft.text(Math.abs(remainder))
- .toggleClass("close-to-limit", remainder < 20)
- .toggleClass("over-limit", remainder < 0);
- });
- $("form", $dialog).submit(function(e) {
- e.preventDefault();
- updateStatus();
- });
- };
- $(".actions .update-status").click(function() {
- if (typeof popup == "undefined") {
- popup = createPopUp();
- makeMeRTE();
- bindBehaviour(popup);
- }
- getLatestStatus();
- popup.setError("");
- popup.show();
- $("#update-user-status-dialog #status-text").removeAttr("readonly").removeAttr("disabled").focus();
- });
-
- function makeMeRTE() {
- AJS.Confluence.EditorLoader.activate(AJS.$("form.quick-status-form"), function(){console.log("success", arguments);}, function(){console.log("failure", arguments);});
- }
- AJS.log("status-updates done");
- });