blueimp-helper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * blueimp helper JS
  3. * https://github.com/blueimp/Gallery
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, window, document */
  12. ;(function () {
  13. 'use strict'
  14. function extend (obj1, obj2) {
  15. var prop
  16. for (prop in obj2) {
  17. if (obj2.hasOwnProperty(prop)) {
  18. obj1[prop] = obj2[prop]
  19. }
  20. }
  21. return obj1
  22. }
  23. function Helper (query) {
  24. if (!this || this.find !== Helper.prototype.find) {
  25. // Called as function instead of as constructor,
  26. // so we simply return a new instance:
  27. return new Helper(query)
  28. }
  29. this.length = 0
  30. if (query) {
  31. if (typeof query === 'string') {
  32. query = this.find(query)
  33. }
  34. if (query.nodeType || query === query.window) {
  35. // Single HTML element
  36. this.length = 1
  37. this[0] = query
  38. } else {
  39. // HTML element collection
  40. var i = query.length
  41. this.length = i
  42. while (i) {
  43. i -= 1
  44. this[i] = query[i]
  45. }
  46. }
  47. }
  48. }
  49. Helper.extend = extend
  50. Helper.contains = function (container, element) {
  51. do {
  52. element = element.parentNode
  53. if (element === container) {
  54. return true
  55. }
  56. } while (element)
  57. return false
  58. }
  59. Helper.parseJSON = function (string) {
  60. return window.JSON && JSON.parse(string)
  61. }
  62. extend(Helper.prototype, {
  63. find: function (query) {
  64. var container = this[0] || document
  65. if (typeof query === 'string') {
  66. if (container.querySelectorAll) {
  67. query = container.querySelectorAll(query)
  68. } else if (query.charAt(0) === '#') {
  69. query = container.getElementById(query.slice(1))
  70. } else {
  71. query = container.getElementsByTagName(query)
  72. }
  73. }
  74. return new Helper(query)
  75. },
  76. hasClass: function (className) {
  77. if (!this[0]) {
  78. return false
  79. }
  80. return new RegExp('(^|\\s+)' + className + '(\\s+|$)').test(
  81. this[0].className
  82. )
  83. },
  84. addClass: function (className) {
  85. var i = this.length
  86. var element
  87. while (i) {
  88. i -= 1
  89. element = this[i]
  90. if (!element.className) {
  91. element.className = className
  92. return this
  93. }
  94. if (this.hasClass(className)) {
  95. return this
  96. }
  97. element.className += ' ' + className
  98. }
  99. return this
  100. },
  101. removeClass: function (className) {
  102. var regexp = new RegExp('(^|\\s+)' + className + '(\\s+|$)')
  103. var i = this.length
  104. var element
  105. while (i) {
  106. i -= 1
  107. element = this[i]
  108. element.className = element.className.replace(regexp, ' ')
  109. }
  110. return this
  111. },
  112. on: function (eventName, handler) {
  113. var eventNames = eventName.split(/\s+/)
  114. var i
  115. var element
  116. while (eventNames.length) {
  117. eventName = eventNames.shift()
  118. i = this.length
  119. while (i) {
  120. i -= 1
  121. element = this[i]
  122. if (element.addEventListener) {
  123. element.addEventListener(eventName, handler, false)
  124. } else if (element.attachEvent) {
  125. element.attachEvent('on' + eventName, handler)
  126. }
  127. }
  128. }
  129. return this
  130. },
  131. off: function (eventName, handler) {
  132. var eventNames = eventName.split(/\s+/)
  133. var i
  134. var element
  135. while (eventNames.length) {
  136. eventName = eventNames.shift()
  137. i = this.length
  138. while (i) {
  139. i -= 1
  140. element = this[i]
  141. if (element.removeEventListener) {
  142. element.removeEventListener(eventName, handler, false)
  143. } else if (element.detachEvent) {
  144. element.detachEvent('on' + eventName, handler)
  145. }
  146. }
  147. }
  148. return this
  149. },
  150. empty: function () {
  151. var i = this.length
  152. var element
  153. while (i) {
  154. i -= 1
  155. element = this[i]
  156. while (element.hasChildNodes()) {
  157. element.removeChild(element.lastChild)
  158. }
  159. }
  160. return this
  161. },
  162. first: function () {
  163. return new Helper(this[0])
  164. }
  165. })
  166. if (typeof define === 'function' && define.amd) {
  167. define(function () {
  168. return Helper
  169. })
  170. } else {
  171. window.blueimp = window.blueimp || {}
  172. window.blueimp.helper = Helper
  173. }
  174. })()