bjui-ajaxtab.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-ajaxtab.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-ajaxtab.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. // AJAXTAB CLASS DEFINITION
  18. // ======================
  19. var Ajaxtab = function(element, options) {
  20. this.$element = $(element)
  21. this.options = options
  22. }
  23. Ajaxtab.DEFAULTS = {
  24. url : undefined,
  25. target : undefined,
  26. reload : false
  27. }
  28. Ajaxtab.prototype.init = function() {
  29. var options = this.options
  30. if (!(options.url)) {
  31. BJUI.debug('Ajaxtab Plugin: Error trying to open a tab, url is undefined!')
  32. return
  33. } else {
  34. options.url = decodeURI(options.url).replacePlh(this.$element.closest('.unitBox'))
  35. if (!options.url.isFinishedTm()) {
  36. this.$element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  37. BJUI.debug('Ajaxtab Plugin: The new ajaxtab\'s url is incorrect, url: '+ options.url)
  38. return
  39. }
  40. options.url = encodeURI(options.url)
  41. }
  42. if (!options.target) {
  43. BJUI.debug('Ajaxtab Plugin: Attribute \'target\' is not defined!')
  44. return
  45. }
  46. if (options.reload) {
  47. this.load()
  48. } else {
  49. var reload = this.$element.data('bjui.ajaxtab.reload')
  50. if (!reload) this.load()
  51. else this.$element.tab('show')
  52. }
  53. }
  54. Ajaxtab.prototype.load = function() {
  55. var $element = this.$element
  56. var options = this.options
  57. $(options.target).ajaxUrl({
  58. url : options.url,
  59. data : {},
  60. callback : function() {
  61. $element.data('bjui.ajaxtab.reload', true).tab('show')
  62. }
  63. })
  64. }
  65. // AJAXTAB PLUGIN DEFINITION
  66. // =======================
  67. function Plugin(option) {
  68. var args = arguments
  69. var property = option
  70. return this.each(function () {
  71. var $this = $(this)
  72. var options = $.extend({}, Ajaxtab.DEFAULTS, $this.data(), typeof option == 'object' && option)
  73. var data = $this.data('bjui.ajaxtab')
  74. if (!data) $this.data('bjui.ajaxtab', (data = new Ajaxtab(this, options)))
  75. if (typeof property == 'string' && $.isFunction(data[property])) {
  76. [].shift.apply(args)
  77. if (!args) data[property]()
  78. else data[property].apply(data, args)
  79. } else {
  80. data.init()
  81. }
  82. })
  83. }
  84. var old = $.fn.ajaxtab
  85. $.fn.ajaxtab = Plugin
  86. $.fn.ajaxtab.Constructor = Ajaxtab
  87. // AJAXTAB NO CONFLICT
  88. // =================
  89. $.fn.ajaxtab.noConflict = function () {
  90. $.fn.ajaxtab = old
  91. return this
  92. }
  93. // AJAXTAB DATA-API
  94. // ==============
  95. $(document).on('click.bjui.ajaxtab.data-api', '[data-toggle="ajaxtab"]', function(e) {
  96. var $this = $(this)
  97. var options = $this.data()
  98. if (!options.url) options.url = $this.attr('href')
  99. Plugin.call($this, options)
  100. e.preventDefault()
  101. })
  102. }(jQuery);