PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fsn-site-central/public/js/filetree/src/jQueryFileTree.coffee

https://gitlab.com/team_fsn/fsn-php
CoffeeScript | 207 lines | 151 code | 26 blank | 30 comment | 20 complexity | c7f278e38b2500c99e98ec4504010c98 MD5 | raw file
  1. ###
  2. # jQueryFileTree Plugin
  3. #
  4. # @author - Cory S.N. LaViska - A Beautiful Site (http://abeautifulsite.net/) - 24 March 2008
  5. # @author - Dave Rogers - (https://github.com/daverogers/)
  6. #
  7. # Usage: $('.fileTreeDemo').fileTree({ options }, callback )
  8. #
  9. # TERMS OF USE
  10. #
  11. # This plugin is dual-licensed under the GNU General Public License and the MIT License and
  12. # is copyright 2008 A Beautiful Site, LLC.
  13. ###
  14. do($ = window.jQuery, window) ->
  15. # Define the plugin class
  16. class FileTree
  17. constructor: (el, args, callback) ->
  18. $el = $(el)
  19. _this = @
  20. defaults = {
  21. root: '/'
  22. script: '/files/filetree'
  23. folderEvent: 'click'
  24. expandSpeed: 500
  25. collapseSpeed: 500
  26. expandEasing: 'swing'
  27. collapseEasing: 'swing'
  28. multiFolder: true
  29. loadMessage: 'Loading...'
  30. errorMessage: 'Unable to get file tree information'
  31. multiSelect: false
  32. onlyFolders: false
  33. onlyFiles: false
  34. preventLinkAction: false
  35. additional_data: {}
  36. }
  37. @jqft = {
  38. container: $el # initiator element
  39. }
  40. @options = $.extend(defaults, args)
  41. @callback = callback
  42. @data = {}
  43. # Loading message
  44. $el.html('<ul class="jqueryFileTree start"><li class="wait">' + @options.loadMessage + '<li></ul>')
  45. # Get the initial file list
  46. _this.showTree( $el, escape(@options.root), () ->
  47. _this._trigger('filetreeinitiated', {})
  48. )
  49. # set delegate event handler for clicks
  50. $el.delegate "li a", @options.folderEvent, _this.onEvent
  51. onEvent: (event) =>
  52. $ev = $(event.target)
  53. options = @options
  54. jqft = @jqft
  55. _this = @
  56. callback = @callback
  57. # set up data object to send back via trigger
  58. _this.data = {}
  59. _this.data.li = $ev.closest('li')
  60. _this.data.type = ( _this.data.li.hasClass('directory') ? 'directory' : 'file' )
  61. _this.data.value = $ev.text()
  62. _this.data.rel = $ev.prop('rel')
  63. _this.data.container = jqft.container
  64. if options.preventLinkAction
  65. event.preventDefault()
  66. if $ev.parent().hasClass('directory')
  67. if $ev.parent().hasClass('collapsed')
  68. # Expand
  69. if !options.multiFolder
  70. $ev.parent().parent().find('UL').slideUp {
  71. duration: options.collapseSpeed,
  72. easing: options.collapseEasing
  73. }
  74. $ev.parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed')
  75. $ev.parent().removeClass('collapsed').addClass('expanded')
  76. $ev.parent().find('UL').remove() # cleanup
  77. _this.showTree $ev.parent(), $ev.attr('rel'), ->
  78. # return expanded event with data
  79. _this._trigger('filetreeexpanded', _this.data)
  80. callback?
  81. else
  82. # Collapse
  83. $ev.parent().find('UL').slideUp {
  84. duration: options.collapseSpeed,
  85. easing: options.collapseEasing,
  86. start: ->
  87. _this._trigger('filetreecollapse', _this.data)
  88. complete: ->
  89. $ev.parent().removeClass('expanded').addClass('collapsed')
  90. _this._trigger('filetreecollapsed', _this.data)
  91. callback?
  92. }
  93. else
  94. # this is a file click, return file information
  95. if !options.multiSelect
  96. # remove "selected" class if set, then append class to currently selected file
  97. jqft.container.find('li').removeClass('selected')
  98. $ev.parent().addClass('selected')
  99. else
  100. # since it's multiselect, more than one element can have the 'selected' class
  101. if $ev.parent().find('input').is(':checked')
  102. $ev.parent().find('input').prop('checked', false)
  103. $ev.parent().removeClass('selected')
  104. else
  105. $ev.parent().find('input').prop('checked', true)
  106. $ev.parent().addClass('selected')
  107. _this._trigger('filetreeclicked', _this.data)
  108. # perform return
  109. callback? $ev.attr('rel')
  110. showTree: (el, dir, finishCallback) ->
  111. $el = $(el)
  112. options = @options
  113. _this = @
  114. $el.addClass('wait')
  115. $(".jqueryFileTree.start").remove()
  116. data =
  117. dir: dir
  118. onlyFolders: options.onlyFolders
  119. onlyFiles: options.onlyFiles
  120. multiSelect: options.multiSelect
  121. for key, val of options.additional_data
  122. if(typeof data[key] == "undefined")
  123. data[key] = val
  124. handleResult = (result) ->
  125. $el.find('.start').html('')
  126. $el.removeClass('wait').append( result )
  127. if options.root == dir
  128. $el.find('UL:hidden').show( callback? )
  129. else
  130. # ensure an easing library is loaded if custom easing is used
  131. if jQuery.easing[options.expandEasing] == undefined
  132. console.log 'Easing library not loaded. Include jQueryUI or 3rd party lib.'
  133. options.expandEasing = 'swing' # revert to swing (default)
  134. $el.find('UL:hidden').slideDown {
  135. duration: options.expandSpeed,
  136. easing: options.expandEasing,
  137. start: ->
  138. _this._trigger('filetreeexpand', _this.data)
  139. complete: finishCallback
  140. }
  141. # if multiselect is on and the parent folder is selected, propagate check to child elements
  142. li = $('[rel="'+decodeURIComponent(dir)+'"]').parent()
  143. if options.multiSelect && li.children('input').is(':checked')
  144. li.find('ul li input').each () ->
  145. $(this).prop('checked', true)
  146. $(this).parent().addClass('selected')
  147. return false;
  148. handleFail = () ->
  149. $el.find('.start').html('')
  150. $el.removeClass('wait').append("<p>"+options.errorMessage+"</p>")
  151. return false
  152. if typeof options.script is 'function'
  153. result = options.script(data)
  154. if typeof result is 'string' or result instanceof jQuery
  155. handleResult(result)
  156. else
  157. handleFail()
  158. else
  159. $.ajax
  160. url: options.script
  161. type: 'POST'
  162. dataType: 'HTML'
  163. data: data
  164. .done (result) ->
  165. handleResult(result)
  166. .fail () ->
  167. handleFail()
  168. # end showTree()
  169. # wrapper to append trigger type to data
  170. _trigger: (eventType, data) ->
  171. $el = @jqft.container
  172. $el.triggerHandler(eventType, data)
  173. # Define the plugin
  174. $.fn.extend fileTree: (args, callback) ->
  175. @each ->
  176. $this = $(this)
  177. data = $this.data('fileTree')
  178. if !data
  179. $this.data 'fileTree', (data = new FileTree(this, args, callback))
  180. if typeof args == 'string'
  181. data[option].apply(data)