1/*jslint sloppy: true */ 2/*global jQuery, LANG, document, alert */ 3 4/** 5 * Automatic behaviours 6 * 7 * This class wraps various JavaScript functionalities that are triggered 8 * automatically whenever a certain object is in the DOM or a certain CSS 9 * class was found 10 */ 11var dw_behaviour = { 12 13 init: function(){ 14 dw_behaviour.focusMarker(); 15 dw_behaviour.scrollToMarker(); 16 dw_behaviour.removeHighlightOnClick(); 17 dw_behaviour.quickSelect(); 18 dw_behaviour.checkWindowsShares(); 19 dw_behaviour.initTocToggle(); 20 dw_behaviour.subscription(); 21 22 dw_behaviour.revisionBoxHandler(); 23 jQuery('#page__revisions input[type=checkbox]').click( 24 dw_behaviour.revisionBoxHandler 25 ); 26 }, 27 28 /** 29 * Looks for an element with the ID scroll__here at scrolls to it 30 */ 31 scrollToMarker: function(){ 32 var $obj = jQuery('#scroll__here'); 33 if($obj.length) { 34 $obj[0].scrollIntoView(); 35 } 36 }, 37 38 /** 39 * Looks for an element with the ID focus__this at sets focus to it 40 */ 41 focusMarker: function(){ 42 jQuery('#focus__this').focus(); 43 }, 44 45 /** 46 * Remove all search highlighting when clicking on a highlighted term 47 * 48 * @FIXME would be nice to have it fade out 49 */ 50 removeHighlightOnClick: function(){ 51 jQuery('span.search_hit').click( 52 function(e){ 53 jQuery(e.target).removeClass('search_hit'); 54 } 55 ); 56 }, 57 58 /** 59 * Autosubmit quick select forms 60 * 61 * When a <select> tag has the class "quickselect", this script will 62 * automatically submit its parent form when the select value changes. 63 * It also hides the submit button of the form. 64 * 65 * @author Andreas Gohr <andi@splitbrain.org> 66 */ 67 quickSelect: function(){ 68 jQuery('select.quickselect') 69 .change(function(e){ e.target.form.submit(); }) 70 .parents('form').find('input[type=submit]').hide(); 71 }, 72 73 /** 74 * Display error for Windows Shares on browsers other than IE 75 * 76 * @author Michael Klier <chi@chimeric.de> 77 */ 78 checkWindowsShares: function() { 79 if(!LANG.nosmblinks || document.all !== null) { 80 // No warning requested or none necessary 81 return; 82 } 83 84 jQuery('a.windows').live('click', function(){ 85 alert(LANG.nosmblinks); 86 }); 87 }, 88 89 /** 90 * Adds the toggle switch to the TOC 91 */ 92 initTocToggle: function() { 93 var $header = jQuery('#toc__header'); 94 if(!$header.length) return; 95 var $toc = jQuery('#toc__inside'); 96 97 var $clicky = jQuery(document.createElement('span')) 98 .attr('id','toc__toggle') 99 .css('cursor','pointer') 100 .click(function(){ 101 $toc.slideToggle(200); 102 setClicky(); 103 }); 104 $header.prepend($clicky); 105 106 var setClicky = function(){ 107 if($toc.css('display') == 'none'){ 108 $clicky.html('<span>+</span>'); 109 $clicky[0].className = 'toc_open'; 110 }else{ 111 $clicky.html('<span>−</span>'); 112 $clicky[0].className = 'toc_close'; 113 } 114 }; 115 116 setClicky(); 117 }, 118 119 /** 120 * Hide list subscription style if target is a page 121 * 122 * @author Adrian Lang <lang@cosmocode.de> 123 * @author Pierre Spring <pierre.spring@caillou.ch> 124 */ 125 subscription: function(){ 126 var $form, $list, $digest; 127 128 $form = jQuery('#subscribe__form'); 129 if (0 === $form.length) return; 130 131 $list = $form.find("input[name='sub_style'][value='list']"); 132 $digest = $form.find("input[name='sub_style'][value='digest']"); 133 134 $form.find("input[name='sub_target']") 135 .click( 136 function () { 137 var $this = jQuery(this), show_list; 138 if (!$this.prop('checked')) { 139 return; 140 } 141 142 show_list = $this.val().match(/:$/); 143 $list.parent().dw_toggle(show_list); 144 if (!show_list && $list.prop('checked')) { 145 $digest.prop('checked', 'checked'); 146 } 147 } 148 ) 149 .filter(':checked') 150 .click(); 151 }, 152 153 /** 154 * disable multiple revisions checkboxes if two are checked 155 * 156 * @author Andreas Gohr <andi@splitbrain.org> 157 */ 158 revisionBoxHandler: function(){ 159 var $checked = jQuery('#page__revisions input[type=checkbox]:checked'); 160 var $all = jQuery('#page__revisions input[type=checkbox]'); 161 162 if($checked.length < 2){ 163 $all.attr('disabled',false); 164 jQuery('#page__revisions input[type=submit]').attr('disabled',true); 165 }else{ 166 $all.attr('disabled',true); 167 jQuery('#page__revisions input[type=submit]').attr('disabled',false); 168 for(var i=0; i<$checked.length; i++){ 169 $checked[i].disabled = false; 170 if(i>1){ 171 $checked[i].checked = false; 172 } 173 } 174 } 175 } 176 177}; 178 179/** 180 * Hides elements with a slide animation 181 * 182 * @param fn optional callback to run after hiding 183 * @author Adrian Lang <mail@adrianlang.de> 184 */ 185jQuery.fn.dw_hide = function(fn) { 186 return this.slideUp('fast', fn); 187}; 188 189/** 190 * Unhides elements with a slide animation 191 * 192 * @param fn optional callback to run after hiding 193 * @author Adrian Lang <mail@adrianlang.de> 194 */ 195jQuery.fn.dw_show = function(fn) { 196 return this.slideDown('fast', fn); 197}; 198 199/** 200 * Toggles visibility of an element using a slide element 201 * 202 * @param bool the current state of the element (optional) 203 */ 204jQuery.fn.dw_toggle = function(bool) { 205 return this.each(function() { 206 var $this = jQuery(this); 207 if (typeof bool === 'undefined') { 208 bool = $this.is(':hidden'); 209 } 210 $this[bool ? "dw_show" : "dw_hide" ](); 211 }); 212}; 213 214jQuery(dw_behaviour.init); 215