blueimp-helper.js 4.6 KB

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