bjui-initui.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-initui.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-initui.js
  11. * ========================================================================
  12. * Copyright 2014 K'naan.
  13. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  14. * ======================================================================== */
  15. +function ($) {
  16. 'use strict';
  17. // INITUI CLASS DEFINITION
  18. // ======================
  19. var Initui = function(element, options) {
  20. var $this = this
  21. this.$element = $(element)
  22. this.options = options
  23. }
  24. Initui.DEFAULTS = {}
  25. Initui.prototype.init = function() {
  26. var that = this, $element = that.$element
  27. $.when(that.initUI()).done(function(){
  28. $element.trigger(BJUI.eventType.afterInitUI)
  29. })
  30. }
  31. Initui.prototype.initUI = function() {
  32. var $element = this.$element
  33. $.when($element.trigger(BJUI.eventType.beforeInitUI)).done(function(){
  34. $element.trigger(BJUI.eventType.initUI)
  35. })
  36. }
  37. // INITUI PLUGIN DEFINITION
  38. // =======================
  39. function Plugin(option) {
  40. var args = arguments
  41. var property = option
  42. return this.each(function () {
  43. var $this = $(this)
  44. var options = $.extend({}, Initui.DEFAULTS, $this.data(), typeof option == 'object' && option)
  45. var data = $this.data('bjui.initui')
  46. if (!data) $this.data('bjui.initui', (data = new Initui(this, options)))
  47. if (typeof property == 'string' && $.isFunction(data[property])) {
  48. [].shift.apply(args)
  49. if (!args) data[property]()
  50. else data[property].apply(data, args)
  51. } else {
  52. data.init()
  53. }
  54. })
  55. }
  56. var old = $.fn.initui
  57. $.fn.initui = Plugin
  58. $.fn.initui.Constructor = Initui
  59. // INITUI NO CONFLICT
  60. // =================
  61. $.fn.initui.noConflict = function () {
  62. $.fn.initui = old
  63. return this
  64. }
  65. // INITUI DATA-API
  66. // ==============
  67. $(document).on('click.bjui.initui.data-api', '[data-toggle="initui"]', function(e) {
  68. Plugin.call($this, $this.data())
  69. e.preventDefault()
  70. })
  71. /* beforeInitUI */
  72. $(document).on(BJUI.eventType.beforeInitUI, function(e) {
  73. var $box = $(e.target)
  74. var noinits = []
  75. var $noinit = $box.find('[data-noinit]')
  76. //progress
  77. $box.find('> .bjui-maskProgress').find('.progress').stop().animate({width:'85%'}, 'fast')
  78. // Hide not need to initialize the UI DOM
  79. $noinit.each(function(i) {
  80. var $this = $(this)
  81. var pos = {}
  82. pos.$target = $this
  83. pos.$next = $this.next()
  84. pos.$prev = $this.prev()
  85. pos.$parent = $this.parent()
  86. pos.visible = $this.is(':visible') ? true : false
  87. noinits.push(pos)
  88. $this.remove()
  89. })
  90. $box.data('bjui.noinit', noinits)
  91. })
  92. /* initUI */
  93. $(document).on(BJUI.eventType.initUI, function(e) {
  94. var $box = $(e.target)
  95. //progress
  96. $box.find('> .bjui-maskProgress').find('.progress').stop().animate({width:'95%'}, 'fast')
  97. })
  98. /* afterInitUI */
  99. $(document).on(BJUI.eventType.afterInitUI, function(e) {
  100. var $box = $(e.target)
  101. var noinits = $box.data('bjui.noinit')
  102. var $form = $box.find('> .bjui-pageContent').find('form')
  103. // Recovery not need to initialize the UI DOM
  104. if (noinits) {
  105. $.each(noinits, function(i, n) {
  106. if (n.$next.length) n.$next.before(n.$target)
  107. else if (n.$prev.length) n.$prev.after(n.$target)
  108. else if (n.$parent.length) n.$parent.append(n.$target)
  109. if (n.visible) n.$target.show()
  110. $box.removeData('bjui.noinit')
  111. })
  112. }
  113. /* resizePageH */
  114. $box.resizePageH()
  115. //submit
  116. if ($form.length) {
  117. $box.find('> .bjui-pageFooter').find(':submit').on('click.bjui.submit', function(e) {
  118. e.preventDefault()
  119. $form.submit()
  120. })
  121. }
  122. //progress
  123. $box.find('.bjui-maskProgress').find('.progress').stop().animate({width:'100%'}, 'fast', function() {
  124. $box.find('.bjui-ajax-mask').fadeOut('normal', function() { $(this).remove() })
  125. })
  126. })
  127. /* Lateral Navigation */
  128. $(document).one(BJUI.eventType.afterInitUI, function(e) {
  129. var $hnavbar = $('#bjui-hnav-navbar'), $active = $hnavbar.find('> li.active')
  130. if ($active.length && $active.find('> .items').length) {
  131. $active.find('> a').trigger('click')
  132. }
  133. })
  134. /* ajaxStatus */
  135. var bjui_ajaxStatus = function($target) {
  136. var $this = $target
  137. var $offset = $this
  138. var position = $this.css('position')
  139. if (position == 'static') $offset = $this.offsetParent()
  140. var zIndex = parseInt($offset.css('zIndex')) || 0
  141. var $ajaxBackground = $this.find('> .bjui-maskBackground')
  142. var $ajaxProgress = $this.find('> .bjui-maskProgress')
  143. if (!$ajaxBackground.length) {
  144. $ajaxBackground = $(FRAG.maskBackground)
  145. $ajaxProgress = $(FRAG.maskProgress.replace('#msg#', BJUI.regional.progressmsg))
  146. $this.prepend($ajaxBackground).prepend($ajaxProgress)
  147. }
  148. var bgZindex = parseInt($ajaxBackground.css('zIndex')) || 0
  149. var prZindex = parseInt($ajaxProgress.css('zIndex')) || 0
  150. $ajaxBackground.css('zIndex', zIndex + 1)
  151. $ajaxProgress.css('zIndex', zIndex + 2)
  152. return {$bg:$ajaxBackground, $pr:$ajaxProgress}
  153. }
  154. $(document)
  155. .on('bjui.ajaxStart', function(e, timeout, callback) {
  156. var ajaxMask = bjui_ajaxStatus($(e.target))
  157. ajaxMask.$bg.fadeIn()
  158. ajaxMask.$pr.fadeIn()
  159. ajaxMask.$pr.find('.progress').animate({width:'80%'}, timeout || 500)
  160. if (callback) {
  161. setTimeout(function() {
  162. callback.toFunc().call(this)
  163. }, 25)
  164. }
  165. })
  166. .on('bjui.ajaxStop', function(e) {
  167. var ajaxMask = bjui_ajaxStatus($(e.target))
  168. ajaxMask.$pr.find('.progress').animate({width:'100%'}, 'fast', function() {
  169. ajaxMask.$bg.remove()
  170. ajaxMask.$pr.remove()
  171. })
  172. })
  173. .on('bjui.ajaxError', function(e) {
  174. var ajaxMask = bjui_ajaxStatus($(e.target))
  175. ajaxMask.$bg.remove()
  176. ajaxMask.$pr.remove()
  177. })
  178. $(document).on(BJUI.eventType.ajaxStatus, function(e) {
  179. var $target = $(e.target), ajaxMask = bjui_ajaxStatus($target)
  180. $target
  181. .one('ajaxStart', function() {
  182. ajaxMask.$bg.fadeIn()
  183. ajaxMask.$pr.fadeIn()
  184. ajaxMask.$pr.find('.progress').animate({width:'10%'}, 'fast')
  185. })
  186. .one('ajaxStop', function() {
  187. //ajaxMask.$bg.fadeOut()
  188. //ajaxMask.$pr.fadeOut()
  189. //ajaxMask.$pr.find('.progress').animate({width:'80%'}, 'fast')
  190. })
  191. .one('ajaxError', function() {
  192. ajaxMask.$bg.remove()
  193. ajaxMask.$pr.remove()
  194. })
  195. })
  196. /* Clean plugins generated 'Dom elements' in the body */
  197. var bodyClear = function($target) {
  198. $target.find('select[data-toggle="selectpicker"]').selectpicker('destroyMenu')
  199. $target.find('[data-toggle="selectztree"]').trigger('destroy.bjui.selectztree')
  200. }
  201. $(document).on(BJUI.eventType.beforeLoadDialog, function(e) {
  202. }).on(BJUI.eventType.beforeAjaxLoad, function(e) {
  203. bodyClear($(e.target))
  204. }).on(BJUI.eventType.beforeCloseNavtab, function(e) {
  205. bodyClear($(e.target))
  206. }).on(BJUI.eventType.beforeCloseDialog, function(e) {
  207. bodyClear($(e.target))
  208. })
  209. /* other */
  210. $(function() {
  211. $(document).on('keydown keyup', function(e) {
  212. if (e.which === BJUI.keyCode.CTRL) {
  213. BJUI.KeyPressed.ctrl = e.type == 'keydown' ? true : false
  214. }
  215. if (e.which === BJUI.keyCode.SHIFT) {
  216. BJUI.KeyPressed.shift = e.type == 'keydown' ? true : false
  217. }
  218. })
  219. })
  220. }(jQuery);