bootstrap-accessibility.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* ========================================================================
  2. * Extends Bootstrap v3.1.1
  3. * Copyright (c) <2014> eBay Software Foundation
  4. * All rights reserved.
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. * Neither the name of eBay or any of its subsidiaries or affiliates nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. * ======================================================================== */
  11. (function($) {
  12. "use strict";
  13. var uniqueId = function(prefix) {
  14. return (prefix || 'ui-id') + '-' + Math.floor((Math.random()*1000)+1)
  15. }
  16. // Alert Extension
  17. // ===============================
  18. $('.alert').attr('role', 'alert')
  19. $('.close').removeAttr('aria-hidden').wrapInner('<span aria-hidden="true"></span>').append('<span class="sr-only">Fermer</span>')
  20. // TOOLTIP Extension
  21. // ===============================
  22. var showTooltip = $.fn.tooltip.Constructor.prototype.show
  23. , hideTooltip = $.fn.tooltip.Constructor.prototype.hide
  24. $.fn.tooltip.Constructor.prototype.show = function () {
  25. showTooltip.apply(this, arguments)
  26. var $tip = this.tip()
  27. , tooltipID = $tip.attr('id') || uniqueId('ui-tooltip')
  28. $tip.attr({'role':'tooltip','id' : tooltipID})
  29. this.$element.attr('aria-describedby', tooltipID)
  30. }
  31. $.fn.tooltip.Constructor.prototype.hide = function () {
  32. hideTooltip.apply(this, arguments)
  33. removeMultiValAttributes(this.$element, 'aria-describedby', this.tip().attr('id'))
  34. return this
  35. }
  36. // Popover Extension
  37. // ===============================
  38. var showPopover = $.fn.popover.Constructor.prototype.setContent
  39. , hideTPopover = $.fn.popover.Constructor.prototype.hide
  40. $.fn.popover.Constructor.prototype.setContent = function(){
  41. showPopover.apply(this, arguments)
  42. var $tip = this.tip()
  43. , tooltipID = $tip.attr('id') || uniqueId('ui-tooltip')
  44. $tip.attr({'role':'alert','id' : tooltipID})
  45. this.$element.attr('aria-describedby', tooltipID)
  46. this.$element.focus()
  47. }
  48. $.fn.popover.Constructor.prototype.hide = function(){
  49. hideTooltip.apply(this, arguments)
  50. removeMultiValAttributes(this.$element, 'aria-describedby', this.tip().attr('id'))
  51. }
  52. //Modal Extension
  53. $('.modal-dialog').attr( {'role' : 'document'})
  54. var modalhide = $.fn.modal.Constructor.prototype.hide
  55. $.fn.modal.Constructor.prototype.hide = function(){
  56. var modalOpener = this.$element.parent().find('[data-target="#' + this.$element.attr('id') + '"]')
  57. modalhide.apply(this, arguments)
  58. modalOpener.focus()
  59. }
  60. // DROPDOWN Extension
  61. // ===============================
  62. var toggle = '[data-toggle=dropdown]'
  63. , $par
  64. , firstItem
  65. , focusDelay = 200
  66. , menus = $(toggle).parent().find('ul').attr('role','menu')
  67. , lis = menus.find('li').attr('role','presentation')
  68. lis.find('a').attr({'role':'menuitem', 'tabIndex':'-1'})
  69. $(toggle).attr({ 'aria-haspopup':'true', 'aria-expanded': 'false'})
  70. $(toggle).parent().on('shown.bs.dropdown',function(e){
  71. $par = $(this)
  72. var $toggle = $par.find(toggle)
  73. $toggle.attr('aria-expanded','true')
  74. setTimeout(function(){
  75. firstItem = $('.dropdown-menu [role=menuitem]:visible', $par)[0]
  76. try{ firstItem.focus()} catch(ex) {}
  77. }, focusDelay)
  78. })
  79. $(toggle).parent().on('hidden.bs.dropdown',function(e){
  80. $par = $(this)
  81. var $toggle = $par.find(toggle)
  82. $toggle.attr('aria-expanded','false')
  83. })
  84. //Adding Space Key Behaviour, opens on spacebar
  85. $.fn.dropdown.Constructor.prototype.keydown = function (e) {
  86. var $par
  87. , firstItem
  88. if (!/(32)/.test(e.keyCode)) return
  89. $par = $(this).parent()
  90. $(this).trigger ("click")
  91. e.preventDefault() && e.stopPropagation()
  92. }
  93. $(document)
  94. .on('focusout.dropdown.data-api', '.dropdown-menu', function(e){
  95. var $this = $(this)
  96. , that = this
  97. setTimeout(function() {
  98. if(!$.contains(that, document.activeElement)){
  99. $this.parent().removeClass('open')
  100. $this.parent().find('[data-toggle=dropdown]').attr('aria-expanded','false')
  101. }
  102. }, 150)
  103. })
  104. .on('keydown.bs.dropdown.data-api', toggle + ', [role=menu]' , $.fn.dropdown.Constructor.prototype.keydown)
  105. // Tab Extension
  106. // ===============================
  107. var $tablist = $('.nav-tabs')
  108. , $lis = $tablist.children('li')
  109. , $tabs = $tablist.find('[data-toggle="tab"], [data-toggle="pill"]')
  110. $tablist.attr('role', 'tablist')
  111. $lis.attr('role', 'presentation')
  112. $tabs.attr('role', 'tab')
  113. $tabs.each(function( index ) {
  114. var tabpanel = $($(this).attr('href'))
  115. , tab = $(this)
  116. , tabid = tab.attr('id') || uniqueId('ui-tab')
  117. tab.attr('id', tabid)
  118. if(tab.parent().hasClass('active')){
  119. tab.attr( { 'tabIndex' : '0', 'aria-selected' : 'true', 'aria-controls': tab.attr('href').substr(1) } )
  120. tabpanel.attr({ 'role' : 'tabpanel', 'tabIndex' : '0', 'aria-hidden' : 'false', 'aria-labelledby':tabid })
  121. }else{
  122. tab.attr( { 'tabIndex' : '-1', 'aria-selected' : 'false', 'aria-controls': tab.attr('href').substr(1) } )
  123. tabpanel.attr( { 'role' : 'tabpanel', 'tabIndex' : '-1', 'aria-hidden' : 'true', 'aria-labelledby':tabid } )
  124. }
  125. })
  126. $.fn.tab.Constructor.prototype.keydown = function (e) {
  127. var $this = $(this)
  128. , $items
  129. , $ul = $this.closest('ul[role=tablist] ')
  130. , index
  131. , k = e.which || e.keyCode
  132. $this = $(this)
  133. if (!/(37|38|39|40)/.test(k)) return
  134. $items = $ul.find('[role=tab]:visible')
  135. index = $items.index($items.filter(':focus'))
  136. if (k == 38 || k == 37) index-- // up & left
  137. if (k == 39 || k == 40) index++ // down & right
  138. if(index < 0) index = $items.length -1
  139. if(index == $items.length) index = 0
  140. var nextTab = $items.eq(index)
  141. if(nextTab.attr('role') ==='tab'){
  142. nextTab.tab('show') //Comment this line for dynamically loaded tabPabels, to save Ajax requests on arrow key navigation
  143. .focus()
  144. }
  145. // nextTab.focus()
  146. e.preventDefault()
  147. e.stopPropagation()
  148. }
  149. $(document).on('keydown.tab.data-api','[data-toggle="tab"], [data-toggle="pill"]' , $.fn.tab.Constructor.prototype.keydown)
  150. var tabactivate = $.fn.tab.Constructor.prototype.activate;
  151. $.fn.tab.Constructor.prototype.activate = function (element, container, callback) {
  152. var $active = container.find('> .active')
  153. $active.find('[data-toggle=tab]').attr({ 'tabIndex' : '-1','aria-selected' : false })
  154. $active.filter('.tab-pane').attr({ 'aria-hidden' : true,'tabIndex' : '-1' })
  155. tabactivate.apply(this, arguments)
  156. element.addClass('active')
  157. element.find('[data-toggle=tab]').attr({ 'tabIndex' : '0','aria-selected' : true })
  158. element.filter('.tab-pane').attr({ 'aria-hidden' : false,'tabIndex' : '0' })
  159. }
  160. // Collapse Extension
  161. // ===============================
  162. var $colltabs = $('[data-toggle="collapse"]')
  163. $colltabs.attr({ 'role':'tab', 'aria-selected':'false', 'aria-expanded':'false' })
  164. $colltabs.each(function( index ) {
  165. var colltab = $(this)
  166. , collpanel = (colltab.attr('data-target')) ? $(colltab.attr('data-target')) : $(colltab.attr('href'))
  167. , parent = colltab.attr('data-parent')
  168. , collparent = parent && $(parent)
  169. , collid = colltab.attr('id') || uniqueId('ui-collapse')
  170. $(collparent).find('div:not(.collapse,.panel-body), h4').attr('role','presentation')
  171. colltab.attr('id', collid)
  172. if(collparent){
  173. collparent.attr({ 'role' : 'tablist', 'aria-multiselectable' : 'true' })
  174. if(collpanel.hasClass('in')){
  175. colltab.attr({ 'aria-controls': colltab.attr('href').substr(1), 'aria-selected':'true', 'aria-expanded':'true', 'tabindex':'0' })
  176. collpanel.attr({ 'role':'tabpanel', 'tabindex':'0', 'aria-labelledby':collid, 'aria-hidden':'false' })
  177. }else{
  178. colltab.attr({'aria-controls' : colltab.attr('href').substr(1), 'tabindex':'-1' })
  179. collpanel.attr({ 'role':'tabpanel', 'tabindex':'-1', 'aria-labelledby':collid, 'aria-hidden':'true' })
  180. }
  181. }
  182. })
  183. var collToggle = $.fn.collapse.Constructor.prototype.toggle
  184. $.fn.collapse.Constructor.prototype.toggle = function(){
  185. var prevTab = this.$parent && this.$parent.find('[aria-expanded="true"]') , href
  186. if(prevTab){
  187. var prevPanel = prevTab.attr('data-target') || (href = prevTab.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')
  188. , $prevPanel = $(prevPanel)
  189. , $curPanel = this.$element
  190. , par = this.$parent
  191. , curTab
  192. if (this.$parent) curTab = this.$parent.find('[data-toggle=collapse][href="#' + this.$element.attr('id') + '"]')
  193. collToggle.apply(this, arguments)
  194. if ($.support.transition) {
  195. this.$element.one($.support.transition.end, function(){
  196. prevTab.attr({ 'aria-selected':'false','aria-expanded':'false', 'tabIndex':'-1' })
  197. $prevPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1'})
  198. curTab.attr({ 'aria-selected':'true','aria-expanded':'true', 'tabIndex':'0' })
  199. if($curPanel.hasClass('in')){
  200. $curPanel.attr({ 'aria-hidden' : 'false','tabIndex' : '0' })
  201. }else{
  202. curTab.attr({ 'aria-selected':'false','aria-expanded':'false'})
  203. $curPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1' })
  204. }
  205. })
  206. }
  207. }else{
  208. collToggle.apply(this, arguments)
  209. }
  210. }
  211. $.fn.collapse.Constructor.prototype.keydown = function (e) {
  212. var $this = $(this)
  213. , $items
  214. , $tablist = $this.closest('div[role=tablist] ')
  215. , index
  216. , k = e.which || e.keyCode
  217. $this = $(this)
  218. if (!/(32|37|38|39|40)/.test(k)) return
  219. if(k==32) $this.click()
  220. $items = $tablist.find('[role=tab]')
  221. index = $items.index($items.filter(':focus'))
  222. if (k == 38 || k == 37) index-- // up & left
  223. if (k == 39 || k == 40) index++ // down & right
  224. if(index < 0) index = $items.length -1
  225. if(index == $items.length) index = 0
  226. $items.eq(index).focus()
  227. e.preventDefault()
  228. e.stopPropagation()
  229. }
  230. $(document).on('keydown.collapse.data-api','[data-toggle="collapse"]' , $.fn.collapse.Constructor.prototype.keydown)
  231. // Carousel Extension
  232. // ===============================
  233. $('.carousel').each(function (index) {
  234. var $this = $(this)
  235. , prev = $this.find('[data-slide="prev"]')
  236. , next = $this.find('[data-slide="next"]')
  237. , $options = $this.find('.item')
  238. , $listbox = $options.parent()
  239. $this.attr( { 'data-interval' : 'false', 'data-wrap' : 'false' } )
  240. $listbox.attr('role', 'listbox')
  241. $options.attr('role', 'option')
  242. var spanPrev = document.createElement('span')
  243. spanPrev.setAttribute('class', 'sr-only')
  244. spanPrev.innerHTML='Previous'
  245. var spanNext = document.createElement('span')
  246. spanNext.setAttribute('class', 'sr-only')
  247. spanNext.innerHTML='Next'
  248. prev.attr('role', 'button')
  249. next.attr('role', 'button')
  250. prev.append(spanPrev)
  251. next.append(spanNext)
  252. $options.each(function () {
  253. var item = $(this)
  254. if(item.hasClass('active')){
  255. item.attr({ 'aria-selected': 'true', 'tabindex' : '0' })
  256. }else{
  257. item.attr({ 'aria-selected': 'false', 'tabindex' : '-1' })
  258. }
  259. })
  260. })
  261. var slideCarousel = $.fn.carousel.Constructor.prototype.slide
  262. $.fn.carousel.Constructor.prototype.slide = function (type, next) {
  263. var $active = this.$element.find('.item.active')
  264. , $next = next || $active[type]()
  265. slideCarousel.apply(this, arguments)
  266. $active
  267. .one($.support.transition.end, function () {
  268. $active.attr({'aria-selected':false, 'tabIndex': '-1'})
  269. $next.attr({'aria-selected':true, 'tabIndex': '0'})
  270. //.focus()
  271. })
  272. }
  273. $.fn.carousel.Constructor.prototype.keydown = function (e) {
  274. var $this = $(this)
  275. , $ul = $this.closest('div[role=listbox]')
  276. , $items = $ul.find('[role=option]')
  277. , $parent = $ul.parent()
  278. , k = e.which || e.keyCode
  279. , index
  280. , i
  281. if (!/(37|38|39|40)/.test(k)) return
  282. index = $items.index($items.filter('.active'))
  283. if (k == 37 || k == 38) { // Up
  284. $parent.carousel('prev')
  285. index--
  286. if(index < 0) index = $items.length -1
  287. else $this.prev().focus()
  288. }
  289. if (k == 39 || k == 40) { // Down
  290. $parent.carousel('next')
  291. index++
  292. if(index == $items.length) index = 0
  293. else {
  294. $this.one($.support.transition.end, function () {
  295. $this.next().focus()
  296. })
  297. }
  298. }
  299. e.preventDefault()
  300. e.stopPropagation()
  301. }
  302. $(document).on('keydown.carousel.data-api', 'div[role=option]', $.fn.carousel.Constructor.prototype.keydown)
  303. // GENERAL UTILITY FUNCTIONS
  304. // ===============================
  305. var removeMultiValAttributes = function (el, attr, val) {
  306. var describedby = (el.attr( attr ) || "").split( /\s+/ )
  307. , index = $.inArray(val, describedby)
  308. if ( index !== -1 ) {
  309. describedby.splice( index, 1 )
  310. }
  311. describedby = $.trim( describedby.join( " " ) )
  312. if (describedby ) {
  313. el.attr( attr, describedby )
  314. } else {
  315. el.removeAttr( attr )
  316. }
  317. }
  318. })(jQuery);