behaviour.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Hides elements with a slide animation
  3. *
  4. * @param {function} fn optional callback to run after hiding
  5. * @param {bool} noaria supress aria-expanded state setting
  6. * @author Adrian Lang <mail@adrianlang.de>
  7. */
  8. jQuery.fn.dw_hide = function(fn, noaria) {
  9. if(!noaria) this.attr('aria-expanded', 'false');
  10. return this.slideUp('fast', fn);
  11. };
  12. /**
  13. * Unhides elements with a slide animation
  14. *
  15. * @param {function} fn optional callback to run after hiding
  16. * @param {bool} noaria supress aria-expanded state setting
  17. * @author Adrian Lang <mail@adrianlang.de>
  18. */
  19. jQuery.fn.dw_show = function(fn, noaria) {
  20. if(!noaria) this.attr('aria-expanded', 'true');
  21. return this.slideDown('fast', fn);
  22. };
  23. /**
  24. * Toggles visibility of an element using a slide element
  25. *
  26. * @param {bool} state the current state of the element (optional)
  27. * @param {function} fn callback after the state has been toggled
  28. * @param {bool} noaria supress aria-expanded state setting
  29. */
  30. jQuery.fn.dw_toggle = function(state, fn, noaria) {
  31. return this.each(function() {
  32. var $this = jQuery(this);
  33. if (typeof state === 'undefined') {
  34. state = $this.is(':hidden');
  35. }
  36. $this[state ? "dw_show" : "dw_hide" ](fn, noaria);
  37. });
  38. };
  39. /**
  40. * Automatic behaviours
  41. *
  42. * This class wraps various JavaScript functionalities that are triggered
  43. * automatically whenever a certain object is in the DOM or a certain CSS
  44. * class was found
  45. */
  46. var dw_behaviour = {
  47. init: function(){
  48. dw_behaviour.focusMarker();
  49. dw_behaviour.scrollToMarker();
  50. dw_behaviour.removeHighlightOnClick();
  51. dw_behaviour.quickSelect();
  52. dw_behaviour.checkWindowsShares();
  53. dw_behaviour.subscription();
  54. dw_behaviour.pageRestoreConfirm();
  55. dw_behaviour.securityCheck();
  56. dw_behaviour.revisionBoxHandler();
  57. jQuery(document).on('click','#page__revisions input[type=checkbox]',
  58. dw_behaviour.revisionBoxHandler
  59. );
  60. jQuery('.bounce').effect('bounce', {times:10}, 2000 );
  61. },
  62. /**
  63. * Looks for an element with the ID scroll__here at scrolls to it
  64. */
  65. scrollToMarker: function(){
  66. var $obj = jQuery('#scroll__here');
  67. if($obj.length) {
  68. if($obj.offset().top != 0) {
  69. jQuery('html, body').animate({
  70. scrollTop: $obj.offset().top - 100
  71. }, 500);
  72. } else {
  73. // hidden object have no offset but can still be scrolled into view
  74. $obj[0].scrollIntoView();
  75. }
  76. }
  77. },
  78. /**
  79. * Display confirm dialog on page restore action
  80. */
  81. pageRestoreConfirm: function(){
  82. jQuery('#dokuwiki__pagetools li.revert a').on('click',
  83. function() {
  84. return confirm(LANG.restore_confirm);
  85. }
  86. );
  87. },
  88. /**
  89. * Looks for an element with the ID focus__this at sets focus to it
  90. */
  91. focusMarker: function(){
  92. jQuery('#focus__this').trigger('focus');
  93. },
  94. /**
  95. * Remove all search highlighting when clicking on a highlighted term
  96. */
  97. removeHighlightOnClick: function(){
  98. jQuery('span.search_hit').on('click',
  99. function(e){
  100. jQuery(e.target).removeClass('search_hit', 1000);
  101. }
  102. );
  103. },
  104. /**
  105. * Autosubmit quick select forms
  106. *
  107. * When a <select> or <input> tag has the class "quickselect", this script will
  108. * automatically submit its parent form when the select value changes.
  109. * It also hides the submit button of the form.
  110. *
  111. * This includes a workaround a weird behaviour when the submit button has a name
  112. *
  113. * @link https://trackjs.com/blog/when-form-submit-is-not-a-function/
  114. * @author Andreas Gohr <andi@splitbrain.org>
  115. */
  116. quickSelect: function(){
  117. jQuery('.quickselect')
  118. .change(function(e){ HTMLFormElement.prototype.submit.call(e.target.form); })
  119. .closest('form').find(':button').not('.show').hide();
  120. },
  121. /**
  122. * Display error for Windows Shares on browsers other than IE
  123. *
  124. * @author Michael Klier <chi@chimeric.de>
  125. */
  126. checkWindowsShares: function() {
  127. if(!LANG.nosmblinks || navigator.userAgent.match(/(Trident|MSIE|Edge)/)) {
  128. // No warning requested or none necessary
  129. return;
  130. }
  131. jQuery('a.windows').on('click', function(){
  132. alert(LANG.nosmblinks.replace(/\\n/,"\n"));
  133. });
  134. },
  135. /**
  136. * Hide list subscription style if target is a page
  137. *
  138. * @author Adrian Lang <lang@cosmocode.de>
  139. * @author Pierre Spring <pierre.spring@caillou.ch>
  140. */
  141. subscription: function(){
  142. var $form, $list, $digest;
  143. $form = jQuery('#subscribe__form');
  144. if (0 === $form.length) return;
  145. $list = $form.find("input[name='sub_style'][value='list']");
  146. $digest = $form.find("input[name='sub_style'][value='digest']");
  147. $form.find("input[name='sub_target']")
  148. .on('click',
  149. function () {
  150. var $this = jQuery(this), show_list;
  151. if (!$this.prop('checked')) {
  152. return;
  153. }
  154. show_list = $this.val().match(/:$/);
  155. $list.parent().dw_toggle(show_list);
  156. if (!show_list && $list.prop('checked')) {
  157. $digest.prop('checked', 'checked');
  158. }
  159. }
  160. )
  161. .filter(':checked')
  162. .trigger('click');
  163. },
  164. /**
  165. * disable multiple revisions checkboxes if two are checked
  166. *
  167. * @author Andreas Gohr <andi@splitbrain.org>
  168. * @author Anika Henke <anika@selfthinker.org>
  169. */
  170. revisionBoxHandler: function() {
  171. var $revisions = jQuery('#page__revisions');
  172. var $all = jQuery('input[type="checkbox"][name="rev2[]"]', $revisions);
  173. var $checked = $all.filter(':checked');
  174. var $button = jQuery('button', $revisions);
  175. if($checked.length < 2) {
  176. $all.prop('disabled', false);
  177. $button.prop('disabled', true);
  178. } else {
  179. $all.prop('disabled', true);
  180. $button.prop('disabled', false);
  181. $checked.each(function(i) {
  182. jQuery(this).prop('disabled', false);
  183. if(i>1) {
  184. jQuery(this).prop('checked', false);
  185. }
  186. });
  187. }
  188. },
  189. /**
  190. * Check that access to the data directory is properly secured
  191. *
  192. * A successful check (a 403 error was returned when loading the image) is saved
  193. * to session storage and not repeated again until the next browser session. This
  194. * avoids overeager security bans (see #3363)
  195. */
  196. securityCheck: function () {
  197. var $checkDiv = jQuery('#security__check');
  198. if (!$checkDiv.length) return;
  199. if (sessionStorage.getItem('dw-security-check:' + DOKU_BASE)) {
  200. // check was already executed successfully
  201. $checkDiv.remove();
  202. return;
  203. }
  204. var img = new Image();
  205. img.onerror = function () {
  206. // successful check will not be repeated during session
  207. $checkDiv.remove();
  208. sessionStorage.setItem('dw-security-check:' + DOKU_BASE, true);
  209. };
  210. img.onload = function () {
  211. // check failed, display a warning message
  212. $checkDiv.html(LANG.data_insecure);
  213. $checkDiv.addClass('error');
  214. };
  215. img.src = $checkDiv.data('src') + '?t=' + Date.now();
  216. }
  217. };
  218. jQuery(dw_behaviour.init);