Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. app.pageInitFuncs.watchlistTabs = ->
  2.  
  3. # CALL FLOW:
  4. # new tabLoader({
  5. # tabContainer: SELECTOR,
  6. # tab: TABSELECTOR,
  7. # success: ajaxSuccessCallback
  8. # })
  9. # --> tabLoader.init()
  10. # --> $(SELECTOR TABSELECTOR).each( on-click: tabClickHandler )
  11. # tabClickHandler
  12. # --> ajax call
  13. # --> success: ajaxSuccessCallback(data)
  14. #
  15. #
  16. # tabLoader options:
  17. # tabContainer: selector for element containing the tabs
  18. # tab: selector for all tabs
  19. # contentContainer: selector for element to load content into (will destory existing contents)
  20. # success: callback for ajax success, containing the resulting data/html
  21. # success: function(tabLoader, data){ //do something }
  22. # error: callback for ajax error, containing the resulting xhr and err
  23. # error: function(tabLoader, xhr, xhrStatus, xhrErrString){ //do something }
  24. # complete: callback after success or error, always called at end of ajax
  25. # complete: function(tabLoader){ //do something }
  26. tabLoader = (options)->
  27. self = this
  28. @options = options
  29. @selector = options.tabContainer || '.ajax-tabs'
  30. @childSelector = options.tab || '.tab'
  31. @childSelector = @selector + ' ' + @childSelector
  32. @contentSelector = options.contentContainer || '.ajax-tabs-content'
  33. @loadingDiv = $ '<div class="loader-round"></div>'
  34.  
  35. # setup eventHandlers which will trigger the ajaxRequest
  36. @init = ->
  37. $(@childSelector).each self.attachHandlers;
  38. self
  39.  
  40. @attachHandlers = ->
  41. $(this).on 'click', self.tabClickHandler
  42.  
  43. # click event: call ajax
  44. # note: "@" will be element clicked
  45. @tabClickHandler = (e) ->
  46. e.preventDefault()
  47. if self.preventCollision 1
  48. self.activeElement = @
  49. if typeof self.options.tabClick == 'function'
  50. self.options.tabClick self
  51. self.ajaxRequest @
  52.  
  53. # build and send ajax, attach callback handlers
  54. @ajaxRequest = (element) ->
  55. $.ajax
  56. type : 'GET'
  57. url : $(element).data 'href'
  58. cache : false
  59. dataType: 'json'
  60. success : self.ajaxSuccess
  61. error : self.ajaxError
  62.  
  63. # success: load the data
  64. @ajaxSuccess = (data) ->
  65. if typeof self.options.success == 'function'
  66. self.options.success self, data
  67. self.ajaxComplete()
  68.  
  69. # error: cleanup
  70. @ajaxError = (xhr, status, errStr) ->
  71. if typeof self.options.error == 'function'
  72. self.options.error self, xhr, status, errStr
  73. self.ajaxComplete()
  74.  
  75. # final cleanup
  76. @ajaxComplete = ->
  77. if typeof self.options.complete == 'function'
  78. self.options.complete self
  79. self.activeElement = null
  80. self.preventCollision 0
  81.  
  82.  
  83. @preventCollision = (status) ->
  84. # 1: begin, 0: end
  85. if status
  86. if !$(@selector).prop 'disabled'
  87. $(@selector).prop 'disabled', true
  88. $(@childSelector).css 'cursor','wait'
  89. $(@contentSelector).prepend @loadingDiv
  90.  
  91. return true
  92. else
  93. $(@selector).removeProp 'disabled'
  94. $(@childSelector).css 'cursor',''
  95. $(@loadingDiv).remove()
  96.  
  97. return false
  98.  
  99. @init()
  100.  
  101. jscrollCleanup = (tabLoader) ->
  102. # workaround for known jscroll bug: https://github.com/pklauzinski/jscroll/issues/77
  103. $(tabLoader.contentSelector)
  104. .unbind '.jscroll'
  105. .removeData 'jscroll'
  106.  
  107. loadContent = (tabLoader, data) ->
  108. if data.replaceContent
  109. jscrollCleanup tabLoader
  110.  
  111. # replaces targeted content (#watchlist-list)
  112. contentSelector = data.replaceContent.selector || tabLoader.contentSelector
  113. $(contentSelector).html data.replaceContent.content
  114.  
  115. # show the watchlist guide if no results
  116. if !data.count
  117. $('.guide').show()
  118.  
  119. # update the total on the tabs
  120. $(tabLoader.activeElement).closest('.count').html data.count
  121.  
  122. # reveal lot images
  123. if ( typeof revealImages == 'function' )
  124. revealImages $('.vehicle-cards-content')
  125. else
  126. $(contentSelector + '.picture-wrap img').opacity 1
  127.  
  128. app.pageInitFuncs.lazyLoad();
  129. $('.lazyload-next').hide();
  130.  
  131. # change active tab
  132. $(tabLoader.childSelector).removeClass 'active'
  133. $(tabLoader.activeElement).addClass 'active'
  134.  
  135. new tabLoader
  136. tabContainer: '.watchlist-tab-panel'
  137. contentContainer: '#watchlist-list'
  138. success: loadContent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement