bjui-alertmsg.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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-alertmsg.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.alertMsg.js (author:ZhangHuihua@msn.com)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-alertmsg.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. // ALERTMSG GLOBAL ELEMENTS
  19. // ======================
  20. var $box, $alertbg, timer
  21. $(function() {
  22. var INIT_ALERTMSG = function() {
  23. $box = $(FRAG.alertBoxFrag).hide().html('')
  24. $alertbg = $(FRAG.alertBackground).hide().html('')
  25. $('body').append('<!-- alert msg box -->').append($box).append('<!-- alert msg box mask bg -->').append($alertbg)
  26. }
  27. INIT_ALERTMSG()
  28. })
  29. // ALERTMSG CLASS DEFINITION
  30. // ======================
  31. var Alertmsg = function(element, options) {
  32. this.$element = $(element)
  33. this.options = options
  34. this.tools = this.TOOLS()
  35. this.clearTime = null
  36. }
  37. Alertmsg.DEFAULTS = {
  38. displayPosition : 'topcenter', // Optional 'topleft, topcenter, topright, middleleft, middlecenter, middleright, bottomleft, bottomcenter, bottomright'
  39. displayMode : 'slide', // Optional 'none, fade, slide'
  40. autoClose : null,
  41. alertTimeout : 3000,
  42. mask : null,
  43. types : {error:'error', info:'info', warn:'warn', correct:'correct', confirm:'confirm'},
  44. fas : {error:'fa-times-circle', info:'fa-info-circle', warn:'fa-exclamation-circle', correct:'fa-check-circle', confirm:'fa-question-circle'}
  45. }
  46. Alertmsg.prototype.TOOLS = function() {
  47. var that = this, options = that.options
  48. var tools = {
  49. getTitle: function(key){
  50. return options.title || BJUI.regional.alertmsg.title[key]
  51. },
  52. keydownOk: function(event) {
  53. if (event.which == BJUI.keyCode.ENTER) {
  54. event.data.target.trigger('click')
  55. return false
  56. }
  57. return true
  58. },
  59. keydownEsc: function(event) {
  60. if (event.which == BJUI.keyCode.ESC) event.data.target.trigger('click')
  61. },
  62. openPosition: function() {
  63. var position = BJUI.alertMsg.displayPosition, mode = BJUI.alertMsg.displayMode, width = 460, height = $box.outerHeight(), startCss = {}, endCss = {}
  64. if (position) {
  65. if (options.displayPosition && options.displayPosition != 'topcenter')
  66. position = options.displayPosition
  67. } else {
  68. position = options.displayPosition
  69. }
  70. if (mode) {
  71. if (options.displayMode && options.displayMode != 'silde')
  72. mode = options.displayMode
  73. } else {
  74. mode = options.displayMode
  75. }
  76. switch (position) {
  77. case 'topleft':
  78. startCss = {top:0 - height, left:0, 'margin-left':0}
  79. endCss = {top:0}
  80. break
  81. case 'topcenter':
  82. startCss = {top:0 - height}
  83. endCss = {top:0}
  84. break
  85. case 'topright':
  86. startCss = {top:0 - height, left:'auto', right:0, 'margin-left':0}
  87. endCss = {top:0}
  88. break
  89. case 'middleleft':
  90. startCss = {top:'50%', left:0 - width, 'margin-left':0, 'margin-top':0 - height/2}
  91. endCss = {left:0}
  92. break
  93. case 'middlecenter':
  94. startCss = {top:'0', 'margin-top':0 - height/2}
  95. endCss = {top:'50%'}
  96. break
  97. case 'middleright':
  98. startCss = {top:'50%', left:'auto', right:0 - width, 'margin-top':0 - height/2}
  99. endCss = {right:0}
  100. break
  101. case 'bottomleft':
  102. startCss = {top:'auto', left:0, bottom:0 - height, 'margin-left':0}
  103. endCss = {bottom:0}
  104. break
  105. case 'bottomcenter':
  106. startCss = {top:'auto', bottom:0 - height}
  107. endCss = {bottom:0}
  108. break
  109. case 'bottomright':
  110. startCss = {top:'auto', left:'auto', right:0, bottom:0 - height, 'margin-left':0}
  111. endCss = {bottom:0}
  112. break
  113. }
  114. if (mode == 'slide') {
  115. $box.css(startCss).show().animate(endCss, 500)
  116. } else if (mode == 'fade') {
  117. startCss.opacity = 0.1
  118. $box.css(startCss).css(endCss).show().animate({opacity:1}, 500)
  119. } else {
  120. $box.css(startCss).css(endCss).show()
  121. }
  122. },
  123. closePosition: function() {
  124. var position = BJUI.alertMsg.displayPosition, mode = BJUI.alertMsg.displayMode, width = 460, height = $box.outerHeight(), endCss = {}
  125. if (position) {
  126. if (options.displayPosition && options.displayPosition != 'topcenter')
  127. position = options.displayPosition
  128. } else {
  129. position = options.displayPosition
  130. }
  131. if (mode) {
  132. if (options.displayMode && options.displayMode != 'silde')
  133. mode = options.displayMode
  134. } else {
  135. mode = options.displayMode
  136. }
  137. switch (position) {
  138. case 'topleft':
  139. endCss = {top:0 - height}
  140. break
  141. case 'topcenter':
  142. endCss = {top:0 - height}
  143. break
  144. case 'topright':
  145. endCss = {top:0 - height}
  146. break
  147. case 'middleleft':
  148. endCss = {left:0 - width}
  149. break
  150. case 'middlecenter':
  151. endCss = {top:0 - height}
  152. break
  153. case 'middleright':
  154. endCss = {right:0 - width}
  155. break
  156. case 'bottomleft':
  157. endCss = {bottom:0 - height}
  158. break
  159. case 'bottomcenter':
  160. endCss = {bottom:0 - height}
  161. break
  162. case 'bottomright':
  163. endCss = {bottom:0 - height}
  164. break
  165. }
  166. if (mode == 'slide') {
  167. $box.animate(endCss, 500, function() {
  168. $alertbg.hide()
  169. $(this).hide().empty()
  170. })
  171. } else if (mode == 'fade') {
  172. $box.animate({opacity:0}, 500, function() {
  173. $alertbg.hide()
  174. $(this).hide().empty()
  175. })
  176. } else {
  177. $box.hide().remove()
  178. $alertbg.hide()
  179. }
  180. },
  181. open: function(type, msg, buttons) {
  182. var tools = this, btnsHtml = '', $newbox, $btns, alertTimeout = BJUI.alertMsg.alertTimeout
  183. if (buttons) {
  184. for (var i = 0; i < buttons.length; i++) {
  185. var sRel = buttons[i].call ? 'callback' : ''
  186. var sCls = buttons[i].cls ? buttons[i].cls : 'default'
  187. var sIco = (buttons[i].cls && buttons[i].cls == 'green') ? 'check' : 'close'
  188. btnsHtml += FRAG.alertBtnFrag.replace('#btnMsg#', '<i class="fa fa-'+ sIco +'"></i> '+ buttons[i].name).replace('#callback#', sRel).replace('#class#', sCls)
  189. }
  190. }
  191. $newbox =
  192. $(FRAG.alertBoxFrag.replace('#type#', type)
  193. .replace('#fa#', options.fas[type])
  194. .replace('#title#', this.getTitle(type))
  195. .replace('#message#', msg)
  196. .replace('#btnFragment#', btnsHtml))
  197. .hide()
  198. .appendTo('body')
  199. if ($box && $box.length) $box.remove()
  200. $box = $newbox
  201. tools.openPosition()
  202. if (timer) {
  203. clearTimeout(timer)
  204. timer = null
  205. }
  206. if (options.mask == null) {
  207. if (!(options.types.info == type || options.types.correct == type))
  208. $alertbg.show()
  209. }
  210. if (options.autoClose == null) {
  211. if (options.types.info == type || options.types.correct == type) {
  212. if (alertTimeout) {
  213. if (options.alertTimeout && options.alertTimeout != 3000)
  214. alertTimeout = options.alertTimeout
  215. } else {
  216. alertTimeout = options.alertTimeout
  217. }
  218. timer = setTimeout(function() { tools.close() }, alertTimeout)
  219. }
  220. }
  221. $btns = $box.find('.btn')
  222. $btns.each(function(i) {
  223. $(this).on('click', $.proxy(function() {
  224. that.tools.close()
  225. var call = buttons[i].call
  226. if (typeof call == 'string') call = call.toFunc()
  227. if (typeof call == 'function') call.call()
  228. }, that)
  229. )
  230. if (buttons[i].keyCode == BJUI.keyCode.ENTER) {
  231. $(document).on('keydown.bjui.alertmsg.ok', {target:$btns.eq(i)}, tools.keydownOk)
  232. }
  233. if (buttons[i].keyCode == BJUI.keyCode.ESC) {
  234. $(document).on('keydown.bjui.alertmsg.esc', {target:$btns.eq(i)}, tools.keydownEsc)
  235. }
  236. })
  237. },
  238. alert: function(type, msg, btnoptions) {
  239. $.extend(options, typeof btnoptions == 'object' && btnoptions)
  240. var op = $.extend({}, {okName:BJUI.regional.alertmsg.btnMsg.ok, okCall:null}, options)
  241. var buttons = [
  242. {name:op.okName, call:op.okCall, cls:'default', keyCode:BJUI.keyCode.ENTER}
  243. ]
  244. this.open(type, msg, buttons)
  245. },
  246. close: function() {
  247. $(document).off('keydown.bjui.alertmsg.ok').off('keydown.bjui.alertmsg.esc')
  248. this.closePosition()
  249. }
  250. }
  251. return tools
  252. }
  253. Alertmsg.prototype.error = function(msg, btnoptions) {
  254. this.tools.alert(this.options.types.error, msg, btnoptions)
  255. }
  256. Alertmsg.prototype.info = function(msg, btnoptions) {
  257. this.tools.alert(this.options.types.info, msg, btnoptions)
  258. }
  259. Alertmsg.prototype.warn = function(msg, btnoptions) {
  260. this.tools.alert(this.options.types.warn, msg, btnoptions)
  261. }
  262. Alertmsg.prototype.ok = function(msg, btnoptions) {
  263. this.tools.alert(this.options.types.correct, msg, btnoptions)
  264. }
  265. Alertmsg.prototype.correct = function(msg, btnoptions) {
  266. this.tools.alert(this.options.types.correct, msg, btnoptions)
  267. }
  268. Alertmsg.prototype.confirm = function(msg, btnoptions) {
  269. $.extend(this.options, typeof btnoptions == 'object' && btnoptions)
  270. var op = $.extend({}, {okName:BJUI.regional.alertmsg.btnMsg.ok, okCall:null, cancelName:BJUI.regional.alertmsg.btnMsg.cancel, cancelCall:null}, this.options)
  271. var buttons = [
  272. {name:op.okName, call:op.okCall, cls:'green', keyCode:BJUI.keyCode.ENTER},
  273. {name:op.cancelName, call:op.cancelCall, cls:'red', keyCode:BJUI.keyCode.ESC}
  274. ]
  275. this.tools.open(this.options.types.confirm, msg, buttons)
  276. }
  277. // ALERTMSG PLUGIN DEFINITION
  278. // =======================
  279. function Plugin(option) {
  280. var args = arguments
  281. var property = option
  282. return this.each(function () {
  283. var $this = $(this)
  284. var options = $.extend({}, Alertmsg.DEFAULTS, $this.data(), typeof option == 'object' && option)
  285. var data = new Alertmsg(this, options)
  286. if (typeof property == 'string' && $.isFunction(data[property])) {
  287. [].shift.apply(args)
  288. if (!args) data[property]()
  289. else data[property].apply(data, args)
  290. } else {
  291. return
  292. }
  293. })
  294. }
  295. var old = $.fn.alertmsg
  296. $.fn.alertmsg = Plugin
  297. $.fn.alertmsg.Constructor = Alertmsg
  298. // ALERTMSG NO CONFLICT
  299. // =================
  300. $.fn.alertmsg.noConflict = function () {
  301. $.fn.alertmsg = old
  302. return this
  303. }
  304. // NAVTAB DATA-API
  305. // ==============
  306. $(document).on('click.bjui.alertmsg.data-api', '[data-toggle="alertmsg"]', function(e) {
  307. var $this = $(this), data = $this.data(), options = data.options, type, msg
  308. if (options) {
  309. if (typeof options == 'string') options = options.toObj()
  310. if (typeof options == 'object') {
  311. $.extend(data, options)
  312. }
  313. }
  314. type = data.type
  315. if (!type) return false
  316. Plugin.call($this, type, data.msg || type, data)
  317. e.preventDefault()
  318. })
  319. }(jQuery);