PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Capt/Scripts/CaptionInput.js

https://bitbucket.org/seanmccleary/capt
JavaScript | 199 lines | 104 code | 41 blank | 54 comment | 9 complexity | fb00376c250fd18810edd889f8932f1d MD5 | raw file
  1. /*
  2. * Copyright 2011 Sean McCleary
  3. *
  4. * This file is part of capt.
  5. *
  6. * capt is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * capt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with capt. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. var Capt = {};
  20. Capt.Hints = [];
  21. /*
  22. * This is called when a field, with a default, pre-filled hint, is focused.
  23. * It'll get that hint out of there so's it don't get in the user's way.
  24. */
  25. Capt.FocusFieldWithDefaultHint = function (el, hintText) {
  26. if (el.value == hintText) {
  27. $(el).removeClass('prefilled_hint');
  28. el.value = '';
  29. }
  30. };
  31. /*
  32. * This is called when a user has put focus on a field with a hint,
  33. * but then left without typing anything there. It'll put the hint back.
  34. */
  35. Capt.BlurFieldWithDefaulthint = function (el, hintText) {
  36. if (jQuery.trim(el.value) == '') {
  37. $(el).addClass('prefilled_hint');
  38. el.value = hintText;
  39. }
  40. };
  41. /*
  42. * This is called when the user clicks into the "Your Caption" textarea,
  43. * and the rest of the caption-related controls should be displayed.
  44. */
  45. Capt.ExpandCaptionControls = function () {
  46. $("#CaptionText").animate({ height: '150px' });
  47. $("#secondary_caption_controls_container").slideDown();
  48. };
  49. /*
  50. * This updates the 140-character-limit counter.
  51. */
  52. Capt.UpdateCounter = function () {
  53. var $textarea = $(this);
  54. if ($textarea.val().length > 140) {
  55. $textarea.val($textarea.val().substr(0, 140));
  56. }
  57. $("#" + $textarea.attr('id') + "_counter").html(140 - this.value.length);
  58. };
  59. /*
  60. * This toggles the arrows when a user votes something up or down.
  61. */
  62. Capt.ToggleArrowHighlights = function (clicked_arrow, other_arrow) {
  63. if (!$(other_arrow).hasClass('highlighted')) {
  64. $(clicked_arrow).addClass('highlighted');
  65. }
  66. $(other_arrow).removeClass('highlighted');
  67. };
  68. /*
  69. * This is called when the user has submitted his AJAX comment.
  70. */
  71. Capt.CommentPosted = function (containerName) {
  72. $('#' + containerName).find('.comment_save_button').removeAttr('disabled');
  73. $('#' + containerName).find('textarea').removeAttr('disabled').val('');
  74. };
  75. /*
  76. * This is called when there was an error saving the comment
  77. */
  78. Capt.CommentNotPosted = function (containerName) {
  79. $('#' + containerName).find('.comment_save_button').removeAttr('disabled');
  80. $('#' + containerName).find('textarea').removeAttr('disabled');
  81. alert("Oh, uh, there was a problem saving your comment. Sorry about that.");
  82. };
  83. Capt.CommentPostBegin = function (containerName) {
  84. $('#' + containerName).find('.comment_save_button').attr('disabled', 'disabled');
  85. $('#' + containerName).find('textarea').attr('disabled', 'disabled');
  86. }
  87. /*
  88. * Stuff to do when the DOM is ready.
  89. */
  90. $(document).ready(function () {
  91. // Re-size the caption table cell to better fit the image
  92. $("#main_image").load(function () {
  93. var imgWidth = $(this).width();
  94. var imgHeight = $(this).height() + 40;
  95. $("#picture_captions_image_cell")
  96. .css("width", imgWidth + "px")
  97. .css("height", imgHeight + "px");
  98. });
  99. // Get the pre-filled hints & set the events on fields with pre-filled hints
  100. $(".prefilled_hint")
  101. .each(function () {
  102. Capt.Hints[this.id] = this.value;
  103. })
  104. .focus(function () {
  105. Capt.FocusFieldWithDefaultHint(this, Capt.Hints[this.id]);
  106. })
  107. .blur(function () {
  108. Capt.BlurFieldWithDefaulthint(this, Capt.Hints[this.id]);
  109. });
  110. // Set up the event to send them to the login page if they try to
  111. // enter a caption or vote on a caption without being logged in.
  112. $(".requires_login").click(function () {
  113. location.href = '/Account/LogOn?ReturnUrl=' + location.href;
  114. return false;
  115. });
  116. // Set it up so the other controls are visible when clicking in the caption
  117. $("#CaptionText:not(.requires_login)")
  118. .focus(Capt.ExpandCaptionControls)
  119. .keyup(Capt.UpdateCounter)
  120. .click(Capt.UpdateCounter)
  121. .change(Capt.UpdateCounter);
  122. $(".caption_comment_text")
  123. .keyup(Capt.UpdateCounter)
  124. .click(Capt.UpdateCounter)
  125. .change(Capt.UpdateCounter);
  126. // Disable the "name" box when the user clicks the "Anonymous" checkbox
  127. $("#IsAnonymous").click(function () {
  128. var user_name = $("#UserName");
  129. if (this.checked) {
  130. Capt.oldNameValue = user_name.val();
  131. user_name.val('');
  132. user_name.attr('disabled', 'disabled');
  133. }
  134. else {
  135. if (Capt.oldNameValue) {
  136. user_name.val(Capt.oldNameValue);
  137. }
  138. user_name.removeAttr('disabled');
  139. }
  140. });
  141. $(".below_threshold").click(function () {
  142. alert("You can't vote until you get your score back up above zero!\nWrite some captions! (And make 'em good!)");
  143. return false;
  144. });
  145. // Wire up the voting arrows. Set it up so that if:
  146. // A: One is highlighted and the other is clicked, they both go grey, or
  147. // B: If neither is highlited and one is clicked, it becomes highlighted
  148. $(".vote_control_container").each(function () {
  149. var up_arrow = $(this).find(".up_arrow").not(".below_threshold")[0];
  150. var down_arrow = $(this).find(".down_arrow").not(".below_threshold")[0];
  151. $(up_arrow).click(function () {
  152. Capt.ToggleArrowHighlights(up_arrow, down_arrow);
  153. });
  154. $(down_arrow).click(function () {
  155. Capt.ToggleArrowHighlights(down_arrow, up_arrow);
  156. });
  157. });
  158. });