index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. (function (factory) {
  2. if (typeof module === 'object' && module.export) {
  3. module.export = factory()
  4. } else if (typeof define === 'function' && (define.amd || define.cmd)) {
  5. define([], factory)
  6. } else if (typeof window !== 'undefined') {
  7. window.IndexSidebar = factory()
  8. }
  9. })(function () {
  10. var defaultOptions = {
  11. chars: '*ABCDEFGHIJKLMNOPQRSTUVWXYZ#',
  12. isAdjust: true,
  13. offsetTop: 70,
  14. offsetBottom: 10,
  15. lineScale: 0.7,
  16. charOffsetX: 80,
  17. charOffsetY: 20
  18. }
  19. function IndexSidebar(options) {
  20. options = options || {}
  21. for (var k in defaultOptions) {
  22. if (defaultOptions.hasOwnProperty(k)) {
  23. options[k] = options[k] || defaultOptions[k]
  24. }
  25. }
  26. this.options = options
  27. this.initialize(options)
  28. }
  29. IndexSidebar.prototype.initialize = function (options) {
  30. var chars = options.chars
  31. var el = document.createElement('div')
  32. el.className = 'index-sidebar-container'
  33. el.innerHTML = this.render(chars)
  34. document.body.appendChild(el)
  35. this.el = el
  36. this.elChar = el.querySelector('.current-char')
  37. this.chars = chars
  38. if (options.isAdjust) {
  39. this.adjust(options)
  40. }
  41. this.initEvents(options)
  42. }
  43. IndexSidebar.prototype.render = function (chars) {
  44. return (
  45. '<span class="current-char"></span>' +
  46. '<ul>' +
  47. [].map.call(chars, function (ch) {
  48. return '<li>' + ch + '</li>'
  49. }).join('') +
  50. '</ul>'
  51. )
  52. }
  53. IndexSidebar.prototype.initEvents = function (options) {
  54. var view = this
  55. var el = this.el
  56. var elChar = this.elChar
  57. var chars = this.chars
  58. var boxRect = el.getBoundingClientRect()
  59. var boxHeight = boxRect.height
  60. var boxClientTop = boxRect.top
  61. var charOffsetX = options.charOffsetX
  62. var charOffsetY = options.charOffsetY
  63. var touching = false
  64. var lastChar
  65. // touch events
  66. if ('ontouchstart' in document) {
  67. el.addEventListener('touchstart', function (e) {
  68. if (!touching) {
  69. e.preventDefault()
  70. var t = e.touches[0]
  71. start(t.clientX, t.clientY)
  72. }
  73. }, false)
  74. document.addEventListener('touchmove', function handler(e) {
  75. if (touching) {
  76. if (e.cancelable) {
  77. // 判断默认行为是否已经被禁用
  78. if (!e.defaultPrevented) {
  79. e.preventDefault();
  80. }
  81. }
  82. var t = e.touches[0]
  83. move(t.clientX, t.clientY)
  84. }
  85. }, false)
  86. document.addEventListener('touchend', function (e) {
  87. if (touching) {
  88. e.preventDefault()
  89. end()
  90. }
  91. }, false)
  92. }
  93. // mouse events
  94. else {
  95. el.addEventListener('mousedown', function (e) {
  96. if (!touching) {
  97. e.preventDefault()
  98. start(e.clientX, e.clientY)
  99. }
  100. })
  101. document.addEventListener('mousemove', function (e) {
  102. if (touching) {
  103. e.preventDefault()
  104. move(e.clientX, e.clientY)
  105. }
  106. })
  107. document.addEventListener('mouseup', function (e) {
  108. if (touching) {
  109. e.preventDefault()
  110. end()
  111. }
  112. })
  113. }
  114. function start(clientX, clientY) {
  115. touching = true
  116. elChar.style.display = 'block'
  117. move(clientX, clientY)
  118. }
  119. function move(clientX, clientY) {
  120. var offset = calcRelativePosition(clientY)
  121. var percent = offset / boxHeight
  122. var ch = getPositionChar(percent)
  123. updateChar(clientX, clientY, ch)
  124. }
  125. function end() {
  126. touching = false
  127. elChar.style.display = 'none'
  128. }
  129. function updateChar(clientX, clientY, ch) {
  130. var x = Math.max(clientX, charOffsetX)
  131. var yMin = boxClientTop
  132. var yMax = window.innerHeight - charOffsetY
  133. var y = Math.min(Math.max(clientY, yMin), yMax)
  134. elChar.textContent = ch
  135. elChar.style.left = x + 'px'
  136. elChar.style.top = y + 'px'
  137. if (ch && lastChar !== ch) {
  138. lastChar = ch
  139. view.trigger('charChange', ch)
  140. }
  141. }
  142. function calcRelativePosition(clientY) {
  143. var y = clientY - boxClientTop
  144. if (y < 0) {
  145. y = 0
  146. } else if (y > boxHeight) {
  147. y = boxHeight
  148. }
  149. return y
  150. }
  151. // yPercent {Number} in range of [0, 1]
  152. function getPositionChar(yPercent) {
  153. var min = 1
  154. var max = chars.length
  155. var index = Math.ceil(yPercent * max)
  156. if (index < min) {
  157. index = min
  158. } else if (index > max) {
  159. index = max
  160. }
  161. return chars[index - 1]
  162. }
  163. }
  164. IndexSidebar.prototype.adjust = function (options) {
  165. var charCount = options.chars.length
  166. var expectHeight = window.innerHeight - options.offsetTop - options.offsetBottom
  167. var expectLineHeight = expectHeight / charCount
  168. var expectFontSize = expectLineHeight * options.lineScale
  169. var style = this.el.querySelector('ul').style
  170. style.lineHeight = expectLineHeight + 'px'
  171. style.fontSize = expectFontSize + 'px'
  172. }
  173. /* Event Emitter API */
  174. IndexSidebar.prototype.trigger = function (event, data) {
  175. var listeners = this._listeners && this._listeners[event]
  176. if (listeners) {
  177. listeners.forEach(function (listener) {
  178. listener(data)
  179. })
  180. }
  181. }
  182. IndexSidebar.prototype.on = function (event, callback) {
  183. this._listeners = this._listeners || {}
  184. var listeners = this._listeners[event] || (this._listeners[event] = [])
  185. listeners.push(callback)
  186. }
  187. IndexSidebar.prototype.off = function (event, callback) {
  188. var listeners = this._listeners && this._listeners[event]
  189. if (listeners) {
  190. var i = listeners.indexOf(callback)
  191. if (i > -1) {
  192. listeners.splice(i, 1)
  193. if (listeners.length === 0) {
  194. this._listeners[event] = null
  195. }
  196. }
  197. }
  198. }
  199. return IndexSidebar
  200. })