bjui-taskbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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-taskbar.js v1.2
  9. * reference: bjui-dialog.js
  10. * @author K'naan (xknaan@163.com)
  11. * -- Modified from dwz.taskBar.js (author:Roger Wu)
  12. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-taskbar.js
  13. * ========================================================================
  14. * Copyright 2014 K'naan.
  15. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  16. * ======================================================================== */
  17. +function ($) {
  18. 'use strict';
  19. // TASKBAR GLOBAL ELEMENTS
  20. // ======================
  21. var $resizable
  22. var $taskBar, $taskBox, $taskList, $prevBtn, $nextBtn, taskDisabled, taskSelected, taskMargin
  23. $(function() {
  24. var INIT_TASKBAR = function() {
  25. $resizable = $('#bjui-resizable')
  26. $taskBar = $(FRAG.taskbar)
  27. $taskBox = $taskBar.find('.taskbarContent')
  28. $taskList = $taskBox.find('> ul')
  29. $prevBtn = $taskBar.find('.taskbarLeft')
  30. $nextBtn = $taskBar.find('.taskbarRight')
  31. taskDisabled = 'disabled'
  32. taskSelected = 'selected'
  33. taskMargin = 'taskbarMargin'
  34. $('body').append('<!-- dialog task bar -->').append($taskBar)
  35. //events
  36. $prevBtn.click(function(e) { $(this).taskbar('scrollLeft') })
  37. $nextBtn.click(function(e) { $(this).taskbar('scrollRight') })
  38. }
  39. INIT_TASKBAR()
  40. })
  41. // TASKBAR CLASS DEFINITION
  42. // ======================
  43. var Taskbar = function(element, options) {
  44. this.$element = $(element)
  45. this.$task = null
  46. this.options = options
  47. this.tools = this.TOOLS()
  48. }
  49. Taskbar.DEFAULTS = {
  50. id: undefined,
  51. title: undefined
  52. }
  53. Taskbar.prototype.init = function() {
  54. var that = this
  55. var $task = $taskList.find('#'+ this.options.id)
  56. this.show()
  57. if (!$task.length) {
  58. var taskFrag = '<li id="#taskid#"><div class="taskbutton"><span><i class="fa fa-th-large"></i></span> <span class="title">#title#</span></div><div class="close"><i class="fa fa-times-circle"></i></div></li>';
  59. $task = $(taskFrag.replace('#taskid#', this.options.id).replace('#title#', this.options.title))
  60. $task.appendTo($taskList)
  61. } else {
  62. $task.find('> div > span.title').html(this.options.title)
  63. }
  64. this.contextmenu($task)
  65. this.switchTask($task)
  66. this.tools.scrollTask($task)
  67. //events
  68. $task.click(function(e) {
  69. if ($(e.target).closest('div').hasClass('close') || $(e.target).hasClass('close')) {
  70. $task.dialog('close', that.options.id)
  71. } else {
  72. var $dialog = $('body').data(that.options.id)
  73. if ($task.hasClass('selected')) {
  74. $dialog.find('.dialogHeader a.minimize').trigger('click')
  75. } else {
  76. if ($dialog.is(':hidden')) {
  77. that.restoreDialog($dialog)
  78. } else {
  79. $dialog.trigger('click')
  80. $task.addClass(taskSelected)
  81. }
  82. }
  83. that.scrollCurrent($task)
  84. }
  85. return false
  86. })
  87. }
  88. Taskbar.prototype.TOOLS = function() {
  89. var that = this
  90. var tools = {
  91. scrollCurrent: function() {
  92. var iW = this.tasksW(this.getTasks())
  93. if (iW > this.getTaskBarW()) {
  94. var $tools = this
  95. var lTask = $taskList.find('> li:last-child')
  96. var left = this.getTaskBarW() - lTask.position().left - lTask.outerWidth(true)
  97. $taskList.animate({left: left}, 200, function() {
  98. $tools.ctrlScrollBtn()
  99. })
  100. } else {
  101. this.ctrlScrollBtn()
  102. }
  103. },
  104. getTaskBarW: function() {
  105. return $taskBox.width()- ($prevBtn.is(':hidden') ? $prevBtn.width() + 2 : 0) - ($nextBtn.is(':hidden') ? $nextBtn.width() + 2 : 0)
  106. },
  107. scrollTask: function($task) {
  108. var $tools = this
  109. if ($task.position().left + this.getLeft() + $task.outerWidth() > this.getBarWidth()) {
  110. var left = this.getTaskBarW() - $task.position().left - $task.outerWidth(true) - 2
  111. $taskList.animate({left:left}, 200, function() {
  112. $tools.ctrlScrollBtn()
  113. })
  114. } else if ($task.position().left + this.getLeft() < 0) {
  115. var left = this.getLeft() - ($task.position().left + this.getLeft())
  116. $taskList.animate({left:left}, 200, function() {
  117. $tools.ctrlScrollBtn()
  118. })
  119. }
  120. },
  121. ctrlScrollBtn: function() {
  122. var iW = this.tasksW(this.getTasks())
  123. if (this.getTaskBarW() > iW) {
  124. $taskBox.removeClass(taskMargin)
  125. $nextBtn.hide()
  126. $prevBtn.hide()
  127. if (this.getTasks().eq(0).length) this.scrollTask(this.getTasks().eq(0))
  128. } else {
  129. $taskBox.addClass(taskMargin)
  130. $nextBtn.show().removeClass(taskDisabled)
  131. $prevBtn.show().removeClass(taskDisabled)
  132. if (this.getLeft() >= 0) $prevBtn.addClass(taskDisabled)
  133. if (this.getLeft() <= this.getTaskBarW() - iW) $nextBtn.addClass(taskDisabled)
  134. }
  135. },
  136. getLeft: function(){
  137. return $taskList.position().left
  138. },
  139. visibleStart: function() {
  140. var iLeft = this.getLeft()
  141. var jTasks = this.getTasks()
  142. for (var i = 0; i < jTasks.size(); i++) {
  143. if (jTasks.eq(i).position().left + jTasks.eq(i).outerWidth(true) + iLeft >= 0) return jTasks.eq(i)
  144. }
  145. return jTasks.eq(0)
  146. },
  147. visibleEnd: function() {
  148. var iLeft = this.getLeft()
  149. var jTasks = this.getTasks()
  150. for (var i = 0; i < jTasks.size(); i++) {
  151. if (jTasks.eq(i).position().left + jTasks.eq(i).outerWidth(true) + iLeft > this.getBarWidth()) return jTasks.eq(i)
  152. }
  153. return jTasks.eq(jTasks.size() - 1)
  154. },
  155. getTasks: function() {
  156. return $taskList.find('> li')
  157. },
  158. tasksW: function(jTasks) {
  159. var iW = 0
  160. jTasks.each(function() {
  161. iW += $(this).outerWidth(true)
  162. })
  163. return iW
  164. },
  165. getBarWidth: function() {
  166. return $taskBar.innerWidth()
  167. },
  168. getCurrent: function() {
  169. return $taskList.find('li.'+ taskSelected)
  170. }
  171. }
  172. return tools
  173. }
  174. Taskbar.prototype.contextmenu = function($obj) {
  175. var that = this
  176. $obj.contextmenu({
  177. id: 'dialogCM',
  178. bindings: {
  179. reload: function(t) {
  180. t.dialog('refresh', that.options.id)
  181. },
  182. closeCurrent: function(t, m) {
  183. var $obj = t.isTag('li') ? t : that.tools.getCurrent()
  184. $obj.find('.close').trigger('click')
  185. },
  186. closeOther: function(t, m){
  187. var $tasks = $taskList.find('> li').not(t)
  188. $tasks.each(function(i) {
  189. $(this).find('.close').trigger('click')
  190. })
  191. },
  192. closeAll: function(t, m) {
  193. var $tasks = that.tools.getTasks()
  194. $tasks.each(function(i) {
  195. $(this).find('.close').trigger('click')
  196. })
  197. }
  198. },
  199. ctrSub: function(t, m) {
  200. var mCur = m.find('[rel="closeCurrent"]')
  201. var mOther = m.find('[rel="closeOther"]')
  202. if (!that.tools.getCurrent().length) {
  203. mCur.addClass(taskDisabled)
  204. mOther.addClass(taskDisabled)
  205. } else {
  206. if (that.tools.getTasks().size() == 1)
  207. mOther.addClass(taskDisabled)
  208. }
  209. }
  210. })
  211. }
  212. Taskbar.prototype.closeDialog = function(task) {
  213. var $task = (typeof task == 'string') ? this.getTask(task) : task
  214. if (!$task || !$task.length) return
  215. $task.remove()
  216. if (this.tools.getTasks().size() == 0) {
  217. this.hide()
  218. }
  219. this.tools.scrollCurrent()
  220. this.$element.removeData('bjui.taskbar')
  221. }
  222. Taskbar.prototype.minimize = function(dialog) {
  223. var that = this
  224. var $dialog = (typeof dialog == 'string') ? $('body').data('dialog') : dialog
  225. var $task = this.getTask($dialog.data('options').id)
  226. $resizable.css({
  227. top: $dialog.css('top'),
  228. left: $dialog.css('left'),
  229. height: $dialog.css('height'),
  230. width: $dialog.css('width')
  231. }).show().animate({top:$(window).height() - 60, left:$task.position().left, width:$task.outerWidth(), height:$task.outerHeight()}, 250, function() {
  232. $(this).hide()
  233. that.inactive($task)
  234. })
  235. }
  236. /**
  237. * @param {Object} id or dialog
  238. */
  239. Taskbar.prototype.restoreDialog = function($dialog) {
  240. var $task = this.getTask($dialog.data('options').id)
  241. $resizable.css({top:$(window).height() - 60, left:$task.position().left, height:$task.outerHeight(), width:$task.outerWidth()})
  242. .show()
  243. .animate({top:$dialog.css('top'), left:$dialog.css('left'), width:$dialog.css('width'), height:$dialog.css('height')}, 250, function() {
  244. $(this).hide()
  245. $dialog.show()
  246. })
  247. this.switchTask($task)
  248. }
  249. /**
  250. * @param {Object} id
  251. */
  252. Taskbar.prototype.inactive = function(task) {
  253. var $task = (typeof task == 'string') ? this.getTask(task) : task
  254. $task.removeClass(taskSelected)
  255. }
  256. Taskbar.prototype.scrollLeft = function() {
  257. var $task = this.tools.visibleStart()
  258. this.tools.scrollTask($task)
  259. }
  260. Taskbar.prototype.scrollRight = function() {
  261. var $task = this.tools.visibleEnd()
  262. this.tools.scrollTask($task)
  263. }
  264. Taskbar.prototype.scrollCurrent = function($task) {
  265. this.tools.scrollTask($task)
  266. }
  267. /**
  268. * @param {Object} id or $task
  269. */
  270. Taskbar.prototype.switchTask = function(task) {
  271. this.tools.getCurrent().removeClass(taskSelected)
  272. var $task = (typeof task == 'string') ? this.getTask(task) : task
  273. $task.addClass(taskSelected)
  274. }
  275. Taskbar.prototype.getTask = function(id) {
  276. return $taskList.find('#'+ id)
  277. }
  278. Taskbar.prototype.changeTitle = function(id, title) {
  279. var $task = this.getTask(id)
  280. if ($task && title) $task.find('.title').html(title)
  281. }
  282. Taskbar.prototype.show = function() {
  283. if ($taskBar.is(':hidden')) $taskBar.show().animate({bottom:0}, 500)
  284. }
  285. Taskbar.prototype.hide = function() {
  286. if ($taskBar.is(':visible')) $taskBar.animate({bottom:-50}, 500, function() { $taskBar.hide() })
  287. }
  288. // TASKBAR PLUGIN DEFINITION
  289. // =======================
  290. function Plugin(option) {
  291. var args = arguments
  292. var property = option
  293. return this.each(function () {
  294. var $this = $(this)
  295. var options = $.extend({}, Taskbar.DEFAULTS, $this.data(), typeof option == 'object' && option)
  296. var data = $this.data('bjui.taskbar')
  297. if (!data) $this.data('bjui.taskbar', (data = new Taskbar(this, options)))
  298. else if (data.options.id != options.id) $this.data('bjui.taskbar', (data = new Taskbar(this, options)))
  299. if (typeof property == 'string' && $.isFunction(data[property])) {
  300. [].shift.apply(args)
  301. if (!args) data[property]()
  302. else data[property].apply(data, args)
  303. } else {
  304. data.init()
  305. }
  306. })
  307. }
  308. var old = $.fn.taskbar
  309. $.fn.taskbar = Plugin
  310. $.fn.taskbar.Constructor = Taskbar
  311. // TASKBAR NO CONFLICT
  312. // =================
  313. $.fn.taskbar.noConflict = function () {
  314. $.fn.taskbar = old
  315. return this
  316. }
  317. }(jQuery);