/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

  1. AJS.$(function($) {
  2. AJS.log("status-updates initialising");
  3. // Workaround for the top bar icon
  4. $("#status-updates-anchor").text(" ");
  5. function createSidebar() {// return;
  6. // We don't modify the display:block of main. We wrap it in a div with display:table-cell.
  7. var $main = $("#main");
  8. $main.wrap("<div id='main-wrapper'/>").wrap("<div/>").wrap("<div/>");
  9. $main.parent().parent().append("<div id='status-updates-sidebar'/>");
  10. var $sidebar = $("#status-updates-sidebar");
  11. var $template = $(StatusUpdates.Templates.statusUpdatesSidebar());
  12. $sidebar.append($template);
  13. }
  14. createSidebar();
  15. $.get(AJS.contextPath() + '/rest/status/latest/by-status', function(data) {
  16. var $statusContainer = $('.user-statuses');
  17. $statusContainer.empty().append($(StatusUpdates.Templates.renderStatusUpdates({statusUpdates: data})));
  18. });
  19. // ========================== The Update Status popup =========================
  20. var popup,
  21. maxChars = 140;
  22. function createPopUp() {
  23. /** HACK: Passing in a non-falsy value results in an invalid attribute value for the dialog's height.
  24. * This actually results in the default browser value of 'auto' being given to the dialog.
  25. * This hack enables the dialog's contents to dictate its size, thus enabling better i18n support.
  26. */
  27. var notAHeight = 'idontthinksohal';
  28. var popup = new AJS.Dialog(650, notAHeight, "update-user-status-dialog");
  29. var $dialog = popup.popup.element;
  30. var template = $(StatusUpdates.Templates.dialogContent({maxChars: maxChars}));
  31. popup.addHeader(AJS.I18n.getText("status.dialog.heading"));
  32. popup.addPanel(AJS.I18n.getText("status.dialog.panel.title"), template);
  33. popup.addButton(AJS.I18n.getText("status.dialog.button.update"), updateStatus, "status-update-button");
  34. popup.addCancel(AJS.I18n.getText("cancel.name"), function (dialog) {dialog.hide(); return false;});
  35. popup.setError = function(html) {
  36. $(".error-message", $dialog).html(html)
  37. };
  38. // add shortcut tip
  39. if (Confluence.KeyboardShortcuts && Confluence.KeyboardShortcuts.enabled) {
  40. popup.addHelpText(AJS.I18n.getText("keyboard.shortcuts.dialog.tip", "s"));
  41. }
  42. return popup;
  43. }
  44. function validate(html) {
  45. var error,
  46. text = $(html).text();
  47. if (!text) {
  48. error = AJS.I18n.getText("status.message.error.blank");
  49. } else if (!$.trim(text)) {
  50. // The message was just whitespace
  51. error = AJS.I18n.getText("status.message.error.onlywhitespace");
  52. } else if (text.length > maxChars) {
  53. error = AJS.I18n.getText("status.message.error.too.long", maxChars);
  54. }
  55. if (error) {
  56. popup.setError(error);
  57. }
  58. return !error;
  59. }
  60. function setCurrentStatus(status) {
  61. $(".current-user-latest-status .status-text").html($("<div/>").html($("<div/>").html(status.text).text()).text());
  62. $(".current-user-latest-status a[id^=view]").each(function() {
  63. var $this = $(this),
  64. href = $this.attr("href");
  65. $this.attr("href", href.replace(/\d+$/, status.id))
  66. .text(status.friendlyDate)
  67. .attr("title", new Date(status.date).toLocaleString());
  68. });
  69. }
  70. function getLatestStatus() {
  71. $.getJSON(Confluence.getContextPath() + "/status/current.action", function(data) {
  72. // TODO switch to MessageHandler
  73. if (data.errorMessage) {
  74. popup.setError(data.errorMessage);
  75. }
  76. else {
  77. setCurrentStatus(data);
  78. }
  79. });
  80. }
  81. var updateStatus = function() {
  82. var $dialog = popup.popup.element,
  83. ed = AJS.Rte.getEditor(),
  84. $updateButton = $(".status-update-button", $dialog),
  85. html = AJS.Rte.getEditor().getContent({format: 'raw'}),
  86. reEnableForm,
  87. updateTask;
  88. if (!validate(html)) {
  89. return false;
  90. }
  91. updateTask = AJS.safe.ajax({
  92. url: Confluence.getContextPath() + "/status/update.action",
  93. type: "POST",
  94. dataType: "json",
  95. data: {
  96. "text": html //text
  97. }
  98. });
  99. updateTask.done(function(data) {
  100. if (data.errorMessage) {
  101. popup.setError(data.errorMessage);
  102. }
  103. else {
  104. setCurrentStatus(data);
  105. ed.setContent("");
  106. $dialog.fadeOut(200, function() {
  107. popup.hide();
  108. });
  109. }
  110. });
  111. updateTask.fail(function(xhr, text, error) {
  112. AJS.log("Error updating status: " + text);
  113. AJS.log(error);
  114. popup.setError("There was an error - " + error);
  115. });
  116. return updateTask.promise();
  117. };
  118. var bindBehaviour = function(popup) {
  119. var $dialog = popup.popup.element,
  120. ed = AJS.Rte.getEditor(),
  121. $charsLeft = $(".chars-left", $dialog),
  122. $updateButton = $(".status-update-button", $dialog);
  123. ed.onKeyUp.add(function(ed, keyboardEvent){
  124. var contents = ed.getContent(),
  125. userLength = $(contents).text().trim().length,
  126. remainder = maxChars - userLength;
  127. console.log("Count", remainder, userLength, contents);
  128. $updateButton[remainder > 0 ? 'removeAttr' : 'attr']("disabled", "disabled");
  129. $charsLeft.text(Math.abs(remainder))
  130. .toggleClass("close-to-limit", remainder < 20)
  131. .toggleClass("over-limit", remainder < 0);
  132. });
  133. $("form", $dialog).submit(function(e) {
  134. e.preventDefault();
  135. updateStatus();
  136. });
  137. };
  138. $(".actions .update-status").click(function() {
  139. if (typeof popup == "undefined") {
  140. popup = createPopUp();
  141. makeMeRTE();
  142. bindBehaviour(popup);
  143. }
  144. getLatestStatus();
  145. popup.setError("");
  146. popup.show();
  147. $("#update-user-status-dialog #status-text").removeAttr("readonly").removeAttr("disabled").focus();
  148. });
  149. function makeMeRTE() {
  150. AJS.Confluence.EditorLoader.activate(AJS.$("form.quick-status-form"), function(){console.log("success", arguments);}, function(){console.log("failure", arguments);});
  151. }
  152. AJS.log("status-updates done");
  153. });