/Foswiki-1.1.4/pub/System/JavascriptFiles/foswiki_edit_src.js

https://github.com/SvenDowideit/foswiki_releases · JavaScript · 184 lines · 109 code · 20 blank · 55 comment · 16 complexity · fac0362f575b2c8a63ccc0fe4df31532 MD5 · raw file

  1. /*
  2. Foswiki - The Free and Open Source Wiki, http://foswiki.org/
  3. Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
  4. are listed in the AUTHORS file in the root of this distribution.
  5. NOTE: Please extend that file, not this notice.
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version. For
  10. more details read LICENSE in the root of this distribution.
  11. This program 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.
  14. As per the GPL, removal of this notice is prohibited.
  15. */
  16. /**
  17. * Support for the "raw text" editor.
  18. * Requires JQUERYPLUGIN::FOSWIKI
  19. */
  20. var EDITBOX_ID = "topic";
  21. // edit box rows
  22. var EDITBOX_PREF_ROWS_ID = "EditTextareaRows";
  23. var EDITBOX_CHANGE_STEP_SIZE = 4;
  24. var EDITBOX_MIN_ROWCOUNT = 4;
  25. // edit box font style
  26. var EDITBOX_PREF_FONTSTYLE_ID = "EditTextareaFontStyle";
  27. var EDITBOX_FONTSTYLE_MONO = "mono";
  28. var EDITBOX_FONTSTYLE_PROPORTIONAL = "proportional";
  29. var EDITBOX_FONTSTYLE_MONO_STYLE = "foswikiEditboxStyleMono";
  30. var EDITBOX_FONTSTYLE_PROPORTIONAL_STYLE = "foswikiEditboxStyleProportional";
  31. (function($) {
  32. foswiki.Edit = {
  33. textareaInited: false,
  34. fontStyle: null,
  35. validateSuppressed: false
  36. };
  37. /**
  38. * Sets the font style of the edit box and the signature box. The change
  39. * is written to a cookie.
  40. * @param inFontStyle: either EDITBOX_FONTSTYLE_MONO or
  41. * EDITBOX_FONTSTYLE_PROPORTIONAL
  42. */
  43. foswiki.Edit.getFontStyle = function() {
  44. if (foswiki.Edit.fontStyle) {
  45. return foswiki.Edit.fontStyle;
  46. }
  47. var pref = foswiki.Pref.getPref(EDITBOX_PREF_FONTSTYLE_ID);
  48. if (!pref || (pref !== EDITBOX_FONTSTYLE_PROPORTIONAL
  49. && pref !== EDITBOX_FONTSTYLE_MONO)) {
  50. pref = EDITBOX_FONTSTYLE_PROPORTIONAL;
  51. }
  52. return pref;
  53. };
  54. /**
  55. * Sets the font style of the edit box and the signature box. The change
  56. * is written to a cookie.
  57. * @param inFontStyle: either EDITBOX_FONTSTYLE_MONO or
  58. * EDITBOX_FONTSTYLE_PROPORTIONAL
  59. */
  60. foswiki.Edit.setFontStyle = function(inFontStyle) {
  61. if (inFontStyle === EDITBOX_FONTSTYLE_MONO) {
  62. $('#' + EDITBOX_ID).removeClass(
  63. EDITBOX_FONTSTYLE_PROPORTIONAL_STYLE).addClass(
  64. EDITBOX_FONTSTYLE_MONO_STYLE);
  65. }
  66. if (inFontStyle === EDITBOX_FONTSTYLE_PROPORTIONAL) {
  67. $('#' + EDITBOX_ID).removeClass(
  68. EDITBOX_FONTSTYLE_MONO_STYLE).addClass(
  69. EDITBOX_FONTSTYLE_PROPORTIONAL_STYLE);
  70. }
  71. foswiki.Edit.fontStyle = inFontStyle;
  72. foswiki.Pref.setPref(EDITBOX_PREF_FONTSTYLE_ID, inFontStyle);
  73. };
  74. /**
  75. * Changes the height of the editbox textarea.
  76. * param inDirection : -1 (decrease) or 1 (increase).
  77. * If the new height is smaller than EDITBOX_MIN_ROWCOUNT the height
  78. * will become EDITBOX_MIN_ROWCOUNT.
  79. * Each change is written to a cookie.
  80. */
  81. foswiki.Edit.changeEditBox = function(inDirection) {
  82. var rowCount = $('#' + EDITBOX_ID).attr('rows');
  83. rowCount += (inDirection * EDITBOX_CHANGE_STEP_SIZE);
  84. rowCount = (rowCount < EDITBOX_MIN_ROWCOUNT)
  85. ? EDITBOX_MIN_ROWCOUNT : rowCount;
  86. $('#' + EDITBOX_ID).attr('rows', rowCount);
  87. foswiki.Pref.setPref(EDITBOX_PREF_ROWS_ID, rowCount);
  88. return false;
  89. };
  90. foswiki.Edit.validateMandatoryFields = function() {
  91. // Provided for use by editors that need to
  92. // validate form elements before navigating away
  93. if (foswiki.Edit.validateSuppressed) {
  94. return true;
  95. }
  96. var alerts = [];
  97. $('select.foswikiMandatory').each(function(index, el) {
  98. var one = false;
  99. var k;
  100. for (k = 0; k < el.options.length; k=k+1) {
  101. if (el.options[k].selected) {
  102. one = true;
  103. break;
  104. }
  105. }
  106. if (!one) {
  107. alerts.push("The required form field '"
  108. + el.name +
  109. "' has no value.");
  110. }
  111. });
  112. $('textarea.foswikiMandatory, input.foswikiMandatory').each(function(index, el) {
  113. if (el.value === null || el.value.length === 0) {
  114. alerts.push("The required form field '"
  115. + el.name +
  116. "' has no value.");
  117. }
  118. });
  119. if (alerts.length > 0) {
  120. alert(alerts.join("\n"));
  121. return false;
  122. } else {
  123. return true;
  124. }
  125. };
  126. $(function() {
  127. try {
  128. document.main.text.focus();
  129. } catch (er) {
  130. //
  131. }
  132. var prefRowsId = foswiki.Pref.getPref(EDITBOX_PREF_ROWS_ID);
  133. if (prefRowsId) {
  134. $('#' + EDITBOX_ID).attr('rows', parseInt(prefRowsId, 10) );
  135. }
  136. // Set the font style (monospace or proportional space) of the edit
  137. // box to the style read from cookie.
  138. var prefStyle = foswiki.Edit.getFontStyle();
  139. foswiki.Edit.setFontStyle(prefStyle);
  140. $(document.forms[name='main']).submit(function(e) {
  141. return foswiki.Edit.validateMandatoryFields();
  142. });
  143. $('.foswikiTextarea').keydown(function(e) {
  144. // Disables the use of ESCAPE in the edit box, because some
  145. // browsers will interpret this as cancel and will remove
  146. // all changes.
  147. var code;
  148. if (e.keyCode) {
  149. code = e.keyCode;
  150. }
  151. return (code !== 27); // ESC
  152. });
  153. $('.foswikiButtonCancel').click(function(e) {
  154. // Used to dynamically set validation suppression
  155. foswiki.Edit.validateSuppressed = true;
  156. });
  157. });
  158. }(jQuery));