bjui-slidebar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*!
  2. * B-JUI v1.2 (http://b-jui.com)
  3. * Git@OSC (http://git.oschina.net/xknaan/B-JUI)
  4. * Copyright 2014 K'naan (xknaan@163.com).
  5. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  6. */
  7. /* ========================================================================
  8. * B-JUI: bjui-slidebar.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.barDrag.js (author:Roger Wu)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-slidebar.js
  12. * ========================================================================
  13. * Copyright 2014 K'naan.
  14. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  15. * ======================================================================== */
  16. +function ($) {
  17. 'use strict';
  18. // SLIDEBAR CLASS INSTANCE
  19. // ======================
  20. $(function() {
  21. $('#bjui-leftside').after('<!-- Adjust the width of Left slide -->').after(FRAG.splitBar).after(FRAG.splitBarProxy)
  22. })
  23. // SLIDEBAR CLASS DEFINITION
  24. // ======================
  25. var Slidebar = function(element, options) {
  26. this.$element = $(element)
  27. this.$bar = this.$element.find('#bjui-sidebar')
  28. this.$sbar = this.$element.find('#bjui-sidebar-s')
  29. this.$lock = this.$bar.find('> .toggleCollapse > .lock')
  30. this.$navtab = $('#bjui-navtab')
  31. this.$collapse = this.$sbar.find('.collapse')
  32. this.$split = $('#bjui-splitBar')
  33. this.$split2 = $('#bjui-splitBarProxy')
  34. this.isfloat = false
  35. this.options = options
  36. }
  37. Slidebar.prototype.lock = function() {
  38. var that = this
  39. var cleft = that.$bar.outerWidth() + 4
  40. var cwidth = BJUI.windowWidth - $('#bjui-sidebar').width() - 6
  41. that.faLock()
  42. that.hoverLock()
  43. that.$sbar.animate({left: -10}, 20)
  44. that.$bar.removeClass('shadown')
  45. that.isfloat = false
  46. that.$navtab.animate({left:cleft, width:cwidth}, 500, function() {
  47. $(window).trigger(BJUI.eventType.resizeGrid)
  48. })
  49. that.$split.show()
  50. }
  51. Slidebar.prototype.unlock = function() {
  52. var that = this
  53. var barleft = 0 - that.$bar.outerWidth() - 0
  54. var cwidth = BJUI.windowWidth - 6
  55. that.faUnLock()
  56. that.hoverUnLock()
  57. that.$navtab.animate({left:6, width:cwidth}, 400)
  58. that.$bar.animate({left: barleft}, 500, function() {
  59. that.$sbar.animate({left:0}, 200)
  60. that.$split.hide()
  61. $(window).trigger(BJUI.eventType.resizeGrid)
  62. })
  63. that.isfloat = false
  64. }
  65. Slidebar.prototype.float = function() {
  66. var that = this
  67. that.$sbar.animate({left:-10}, 200)
  68. that.$bar.addClass('shadown').animate({left: 0}, 500)
  69. that.isfloat = true
  70. }
  71. Slidebar.prototype.hideFloat = function() {
  72. var that = this
  73. var barleft = 0 - that.$bar.outerWidth() - 0
  74. that.$bar.animate({left: barleft - 10}, 500, function() {
  75. that.$sbar.animate({left:0}, 100)
  76. })
  77. that.isfloat = false
  78. }
  79. Slidebar.prototype.hoverLock = function() {
  80. var that = this
  81. that.$lock
  82. .hover(function() {
  83. that.tipUnLock()
  84. that.faUnLock()
  85. }, function() {
  86. that.tipLock()
  87. that.faLock()
  88. })
  89. }
  90. Slidebar.prototype.hoverUnLock = function() {
  91. var that = this
  92. that.$lock
  93. .hover(function() {
  94. that.tipLock()
  95. that.faLock()
  96. }, function() {
  97. that.tipUnLock()
  98. that.faUnLock()
  99. })
  100. }
  101. Slidebar.prototype.tipLock = function() {
  102. this.$lock.tooltip('destroy').tooltip({ title:'保持锁定,始终显示导航栏', container:'body' })
  103. }
  104. Slidebar.prototype.tipUnLock = function() {
  105. this.$lock.tooltip('destroy').tooltip({ title:'解除锁定,自动隐藏导航栏', container:'body' })
  106. }
  107. Slidebar.prototype.faLock = function() {
  108. this.$lock.find('> i').attr('class', 'fa fa-lock')
  109. }
  110. Slidebar.prototype.faUnLock = function() {
  111. this.$lock.find('> i').attr('class', 'fa fa-unlock-alt')
  112. }
  113. Slidebar.prototype.init = function() {
  114. var that = this
  115. if (!BJUI.ui.showSlidebar) {
  116. that.unlock()
  117. } else {
  118. that.hoverLock()
  119. }
  120. this.$lock.off('click.bjui.slidebar').on('click.bjui.slidebar', function() {
  121. if (that.isfloat) {
  122. that.lock()
  123. } else {
  124. that.unlock()
  125. }
  126. BJUI.ui.showSlidebar = !BJUI.ui.showSlidebar
  127. })
  128. this.$collapse.hover(function() {
  129. that.float()
  130. that.$navtab.click(function() {
  131. if (that.isfloat) that.hideFloat()
  132. })
  133. })
  134. this.$split.mousedown(function(e) {
  135. that.$split2.each(function() {
  136. var $spbar2 = $(this)
  137. setTimeout(function() { $spbar2.show() }, 100)
  138. $spbar2
  139. .css({visibility:'visible', left: that.$split.css('left')})
  140. .basedrag($.extend(that.options, {obj:that.$bar, move:'horizontal', event:e, stop: function() {
  141. $(this).css('visibility', 'hidden')
  142. var move = parseInt($(this).css('left')) - parseInt(that.$split.css('left'))
  143. var sbarwidth = that.$bar.outerWidth() + move
  144. var cleft = parseInt(that.$navtab.css('left')) + move
  145. var cwidth = that.$navtab.outerWidth() - move
  146. that.$bar.css('width', sbarwidth)
  147. that.$split.css('left', $(this).css('left'))
  148. that.$navtab.css({left:cleft, width:cwidth})
  149. }}))
  150. return false
  151. })
  152. })
  153. // move hnav
  154. if ($('#bjui-hnav-navbar-box').length) {
  155. that.moveHnav()
  156. }
  157. }
  158. Slidebar.prototype.moveHnav = function() {
  159. var $hnavbox = $('#bjui-hnav-navbar-box'),
  160. $hnavbar = $hnavbox.find('> #bjui-hnav-navbar'),
  161. $hmoreL = $hnavbox.prev(),
  162. $hmoreR = $hnavbox.next()
  163. $hmoreL.hover(function() {
  164. $hnavbar.stop().animate({left:0}, 2000, function() {
  165. $hmoreL.hide()
  166. })
  167. }, function() {
  168. $hnavbar.stop()
  169. if ($hnavbox.data('hnav.move')) {
  170. $hmoreR.show()
  171. }
  172. }).on('click', function() {
  173. $hnavbar.stop().animate({left:0}, 'fast', function() {
  174. $hmoreL.hide()
  175. })
  176. return false
  177. })
  178. $hmoreR.hover(function() {
  179. $hnavbar.stop().animate({left:($hnavbox.width() - $hnavbox.data('hnav.liw') - 10)}, 2000, function() {
  180. $hmoreR.hide()
  181. })
  182. }, function() {
  183. $hnavbar.stop()
  184. if ($hnavbar.css('left') != '0px') {
  185. $hmoreL.show()
  186. }
  187. }).on('click', function() {
  188. $hnavbar.stop().animate({left:($hnavbox.width() - $hnavbox.data('hnav.liw') - 10)}, 'fast', function() {
  189. $hmoreR.hide()
  190. })
  191. return false
  192. })
  193. }
  194. Slidebar.prototype.initHnav = function() {
  195. var that = this,
  196. title = that.$element.text().trim(),
  197. $li = that.$element.parent(),
  198. $box = $('#bjui-accordionmenu'),
  199. $trees, $items, $panel, $array
  200. $trees = $li.find('> .items > ul.ztree')
  201. $items = $li.find('> .items > ul.menu-items')
  202. if (!($trees.length || $items.length)) return
  203. if ($trees.length) $array = $trees
  204. if ($items.length) {
  205. if (!$array) $array = $items
  206. else $array = $array.add($items)
  207. $items.find('a').each(function() {
  208. var $a = $(this), options = $a.data('options')
  209. if (!$a.data('icon.init') && options && typeof options == 'string') {
  210. options = options.toObj()
  211. if (options && options.faicon) {
  212. options.faicon = options.faicon.trim()
  213. if (options.faicon.startsWith('fa-')) options.faicon = options.faicon.substr(3)
  214. $a.prepend('<i class="fa fa-'+ options.faicon +'"></i>').data('icon.init', true).attr('title', $a.text())
  215. }
  216. }
  217. })
  218. }
  219. $box.empty()
  220. $array.each(function(i) {
  221. var $t = $(this), panel, cls, bodycls, faicon = $t.data('faicon'), faiconClose = $t.data('faiconClose'), icon = faicon ? faicon : 'dot-circle-o'
  222. if ($t.data('tit')) title = $t.data('tit')
  223. cls = i ? 'collapsed' : ''
  224. bodycls = i ? '' : ' in'
  225. panel = FRAG.slidePanel
  226. .replaceAll('#id#', 'bjui-collapse'+ i)
  227. .replaceAll('#title#', title)
  228. .replaceAll('#righticon#', '<i class="fa fa-angle-down"></i>')
  229. .replaceAll('#class#', cls)
  230. .replaceAll('#bodyclass#', bodycls)
  231. if (icon) panel = panel.replaceAll('#icon#', '<i class="fa fa-'+ icon +'"></i>')
  232. else panel = panel.replaceAll('#icon#', '')
  233. $panel = $(panel)
  234. $panel.find('> .panel-collapse > .panel-body').append($t.removeAttr('data-noinit'))
  235. $box.append($panel)
  236. if (!i) $panel.collapse('show')
  237. })
  238. $('#bjui-sidebar').initui()
  239. $li
  240. .addClass('active')
  241. .data('bjui.slidebar.hnav.panels', $box.find('> .panel'))
  242. .siblings().removeClass('active')
  243. }
  244. // SLIDEBAR PLUGIN DEFINITION
  245. // =======================
  246. function Plugin(option) {
  247. var args = arguments
  248. var property = option
  249. return this.each(function () {
  250. var $this = $(this)
  251. var options = $.extend({}, $this.data(), typeof option == 'object' && option)
  252. var data = $this.data('bjui.slidebar')
  253. if (!data) $this.data('bjui.slidebar', (data = new Slidebar(this, options)))
  254. if (typeof property == 'string' && $.isFunction(data[property])) {
  255. [].shift.apply(args)
  256. if (!args) data[property]()
  257. else data[property].apply(data, args)
  258. } else {
  259. data.init()
  260. }
  261. })
  262. }
  263. var old = $.fn.slidebar
  264. $.fn.slidebar = Plugin
  265. $.fn.slidebar.Constructor = Slidebar
  266. // SLIDEBAR NO CONFLICT
  267. // =================
  268. $.fn.basedrag.noConflict = function () {
  269. $.fn.slidebar = old
  270. return this
  271. }
  272. // SLIDEBAR DATA-API
  273. // ==============
  274. $(document).one(BJUI.eventType.afterInitUI, function(e) {
  275. $('#bjui-leftside').slidebar({minW:150, maxW:700})
  276. })
  277. $(document).on('click.bjui.slidebar.data-api', '[data-toggle="slidebar"]', function(e) {
  278. var $li = $(this).parent(), $box = $('#bjui-accordionmenu'), $panels = $li.data('bjui.slidebar.hnav.panels')
  279. $box.find('> .panel').detach()
  280. if ($panels && $panels.length) {
  281. $box.append($panels)
  282. $li.addClass('active').siblings().removeClass('active')
  283. } else {
  284. Plugin.call($(this), 'initHnav')
  285. }
  286. e.preventDefault()
  287. })
  288. }(jQuery);