bjui-ajax.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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-ajax.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.ajax.js (author:ZhangHuihua@msn.com)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-ajax.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. // BJUIAJAX GLOBAL ELEMENTS
  19. // ======================
  20. var autorefreshTimer
  21. // BJUIAJAX CLASS DEFINITION
  22. // ======================
  23. var Bjuiajax = function(element, options) {
  24. var $this = this
  25. this.$element = $(element)
  26. this.options = options
  27. this.tools = this.TOOLS()
  28. }
  29. Bjuiajax.DEFAULTS = {
  30. reload : true,
  31. loadingmask : true
  32. }
  33. Bjuiajax.NAVTAB = 'navtab'
  34. Bjuiajax.prototype.TOOLS = function() {
  35. var that = this
  36. var tools = {
  37. getPagerForm: function($parent, args) {
  38. var form = $parent.isTag('form') ? $parent[0] : $parent.find('#pagerForm:first')[0]
  39. var pageInfo = $.extend({}, BJUI.pageInfo)
  40. if ($parent.data('bjui.clientPaging')) {
  41. args = $.extend({}, $parent.data('bjui.clientPaging'), args)
  42. $parent.data('bjui.clientPaging', args)
  43. }
  44. if (form) {
  45. for (var key in pageInfo) {
  46. var val = ''
  47. if (args && args[key]) val = args[key]
  48. if (!form[pageInfo[key]]) $('<input type="hidden" name="'+ pageInfo[key] +'" value="'+ val +'">').appendTo($(form))
  49. else if (val) form[pageInfo[key]].value = val
  50. }
  51. }
  52. return form
  53. },
  54. getTarget: function() {
  55. if (that.$element.closest('.navtab-panel').length) return Bjuiajax.NAVTAB
  56. else return 'dialog'
  57. }
  58. }
  59. return tools
  60. }
  61. Bjuiajax.prototype.ajaxForm = function(options) {
  62. var that = this
  63. var $form = this.$element,
  64. callback = options && options.callback,
  65. enctype = $form.attr('enctype')
  66. options = $.extend({}, that.options, typeof options == 'object' && options)
  67. if (callback) callback = callback.toFunc()
  68. var successFn = function(data, textStatus, jqXHR) {
  69. callback ? callback.apply(that, [data, $form]) : $.proxy(that.ajaxCallback(data), that)
  70. }
  71. var _submitFn = function() {
  72. var op = {loadingmask:options.loadingmask, type:$form.attr('method'), url:$form.attr('action'), callback:successFn, error:$.proxy(that.ajaxError, that)}
  73. if (enctype && enctype == 'multipart/form-data') {
  74. if (window.FormData) {
  75. $.extend(op, {data:new FormData($form[0]), contentType:false, processData:false})
  76. } else {
  77. $.extend(op, {data:$form.serializeArray(), files:$form.find(':file'), iframe:true, processData:false})
  78. }
  79. } else {
  80. $.extend(op, {data:$form.serializeArray()})
  81. }
  82. $form.doAjax(op)
  83. }
  84. if (options.confirmMsg) {
  85. $form.alertmsg('confirm', options.confirmMsg, {okCall: _submitFn})
  86. } else {
  87. _submitFn()
  88. }
  89. }
  90. Bjuiajax.prototype.ajaxDone = function(json) {
  91. var $element = this.$element
  92. if (json[BJUI.keys.statusCode] == BJUI.statusCode.error) {
  93. if (json[BJUI.keys.message]) $element.alertmsg('error', json[BJUI.keys.message])
  94. } else if (json[BJUI.keys.statusCode] == BJUI.statusCode.timeout) {
  95. $element.alertmsg('info', json[BJUI.keys.message] || FRAG.sessionTimout)
  96. BJUI.loadLogin()
  97. } else {
  98. if (json[BJUI.keys.message]) $element.alertmsg('correct', json[BJUI.keys.message])
  99. }
  100. }
  101. Bjuiajax.prototype.ajaxError = function(xhr, ajaxOptions, thrownError) {
  102. var msg = xhr.responseText.trim()
  103. if (msg.startsWith('{')) {
  104. this.ajaxDone(msg.toObj())
  105. } else {
  106. this.$element.alertmsg('error', '<div>Http status: ' + xhr.status + ' ' + xhr.statusText + '</div>'
  107. + '<div>ajaxOptions: '+ ajaxOptions +' </div>'
  108. + '<div>thrownError: '+ thrownError +' </div>'
  109. + '<div>'+ msg +'</div>')
  110. }
  111. }
  112. Bjuiajax.prototype.ajaxCallback = function(json) {
  113. var that = this
  114. var $element = that.$element
  115. var $target = $element.closest('.bjui-layout')
  116. that.ajaxDone(json)
  117. if (json[BJUI.keys.statusCode] == BJUI.statusCode.ok) {
  118. if ($target && $target.length) {
  119. that.divCallback(json, $target)
  120. } else {
  121. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  122. that.navtabCallback(json)
  123. } else {
  124. that.dialogCallback(json)
  125. }
  126. }
  127. }
  128. }
  129. Bjuiajax.prototype.divCallback = function(json, $target) {
  130. var that = this
  131. if (json.tabid)
  132. setTimeout(function() { that.$element.navtab('reloadFlag', json.tabid) }, 100)
  133. if (json.dialogid)
  134. setTimeout(function() { that.$element.dialog('refresh', json.dialogid) }, 100)
  135. if (json.divid)
  136. setTimeout(function() { that.$element.bjuiajax('refreshDiv', json.divid) }, 100)
  137. if (that.options.reload) {
  138. var form = that.tools.getPagerForm($target)
  139. var url = null, type = null
  140. if (form) {
  141. url = form.attr('action')
  142. type = form.attr('method') || 'GET'
  143. } else {
  144. url = $target.data('url')
  145. type = $target.data('type') || 'GET'
  146. }
  147. if (url) $target.ajaxUrl({url: url, type: type})
  148. }
  149. if (that.options.reloadNavtab)
  150. setTimeout(function() { that.$element.navtab('refresh') }, 100)
  151. if (json.forward) {
  152. var _forward = function() {
  153. $target.ajaxUrl({url: json.forward})
  154. }
  155. if (json.forwardConfirm) {
  156. that.$element.alertmsg('confirm', json.forwardConfirm, {
  157. okCall: function() { _forward() }
  158. })
  159. } else {
  160. _forward()
  161. }
  162. }
  163. }
  164. Bjuiajax.prototype.navtabCallback = function(json) {
  165. var that = this
  166. if (json.tabid)
  167. setTimeout(function() { that.$element.navtab('reloadFlag', json.tabid) }, 100)
  168. if (json.dialogid)
  169. setTimeout(function() { that.$element.dialog('refresh', json.dialogid) }, 100)
  170. if (json.divid)
  171. setTimeout(function() { that.$element.bjuiajax('refreshDiv', json.divid) }, 100)
  172. if (json.datagrid) {
  173. setTimeout(function() {
  174. $.each(json.datagrid.join(','), function(i, n) {
  175. $('#'+ n.trim()).datagrid('refresh')
  176. })
  177. }, 100)
  178. }
  179. if (json.closeCurrent && !json.forward)
  180. that.$element.navtab('closeCurrentTab')
  181. else if (that.options.reload)
  182. setTimeout(function() { that.$element.navtab('refresh') }, 100)
  183. if (json.forward) {
  184. var _forward = function() {
  185. that.$element.navtab('reload', {url:json.forward})
  186. }
  187. if (json.forwardConfirm) {
  188. that.$element.alertmsg('confirm', json.forwardConfirm, {
  189. okCall: function() { _forward() },
  190. cancelCall: function() { if (json.closeCurrent) { that.$element.navtab('closeCurrentTab') } }
  191. })
  192. } else {
  193. _forward()
  194. }
  195. }
  196. }
  197. Bjuiajax.prototype.dialogCallback = function(json) {
  198. var that = this
  199. if (json.tabid)
  200. setTimeout(function() { that.$element.navtab('reloadFlag', json.tabid) }, 100)
  201. if (json.dialogid)
  202. setTimeout(function() { that.$element.dialog('refresh', json.dialogid) }, 100)
  203. if (json.divid)
  204. setTimeout(function() { that.$element.bjuiajax('refreshDiv', json.divid) }, 100)
  205. if (json.closeCurrent && !json.forward)
  206. that.$element.dialog('closeCurrent')
  207. else if (that.options.reload)
  208. setTimeout(function() { that.$element.dialog('refresh') }, 100)
  209. if (that.options.reloadNavtab)
  210. setTimeout(function() { that.$element.navtab('refresh') }, 100)
  211. if (json.forward) {
  212. var _forward = function() {
  213. that.$element.dialog('reload', {url:json.forward})
  214. }
  215. if (json.forwardConfirm) {
  216. that.$element.alertmsg('confirm', json.forwardConfirm, {
  217. okCall: function() { _forward() }
  218. })
  219. } else {
  220. _forward()
  221. }
  222. }
  223. }
  224. Bjuiajax.prototype.pageCallback = function(options, target) {
  225. var that = this
  226. var op = $.extend({}, Bjuiajax.DEFAULTS, options)
  227. var $target = target || null
  228. var form = null
  229. if ($target && $target.length) {
  230. form = that.tools.getPagerForm($target, op)
  231. if (form) {
  232. $.extend(op, $(form).data())
  233. that.reloadDiv($target, {type:$(form).attr('method') || 'POST', url:$(form).attr('action'), data:$(form).serializeArray(), loadingmask:op.loadingmask})
  234. }
  235. } else {
  236. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  237. $target = $.CurrentNavtab
  238. form = that.tools.getPagerForm($target, op)
  239. if (form) $.extend(op, $(form).data())
  240. that.$element.navtab('reloadForm', false, op)
  241. } else {
  242. $target = $.CurrentDialog
  243. form = that.tools.getPagerForm($target, op)
  244. if (form) $.extend(op, $(form).data())
  245. that.$element.dialog('reloadForm', false, op)
  246. }
  247. }
  248. }
  249. Bjuiajax.prototype.doSearch = function(options) {
  250. var that = this, $element = that.$element, form = null, op = {pageCurrent:1}, $target = $element.closest('.bjui-layout'), isValid = options.isValid
  251. options = $.extend({}, Bjuiajax.DEFAULTS, typeof options == 'object' && options)
  252. if (!options.url) options.url = $element.attr('action')
  253. if (!options.url) {
  254. BJUI.debug('Error trying to submit form action: action url is undefined!')
  255. return
  256. } else {
  257. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  258. if (!options.url.isFinishedTm()) {
  259. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  260. BJUI.debug('The submit form action is incorrect: '+ options.url)
  261. return
  262. }
  263. options.url = encodeURI(options.url)
  264. }
  265. var search = function() {
  266. if ($target && $target.length) {
  267. form = that.tools.getPagerForm($target, op)
  268. var data = $element.serializeJson(), _data = {}
  269. if (options.clearQuery) {
  270. var pageInfo = BJUI.pageInfo
  271. for (var key in pageInfo) {
  272. _data[pageInfo[key]] = data[pageInfo[key]]
  273. }
  274. data = _data
  275. }
  276. that.reloadDiv($target, {type:$element.attr('method') || 'POST', url:options.url, data:data, loadingmask:options.loadingmask})
  277. } else {
  278. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  279. $target = $.CurrentNavtab
  280. form = that.tools.getPagerForm($target, op)
  281. $element.navtab('reloadForm', options.clearQuery, options)
  282. } else {
  283. $target = $.CurrentDialog
  284. form = that.tools.getPagerForm($target, op)
  285. $element.dialog('reloadForm', options.clearQuery, options)
  286. }
  287. }
  288. }
  289. if (!isValid) {
  290. if ($.validator) {
  291. $element.isValid(function(v) {
  292. if (v) search()
  293. })
  294. } else {
  295. search()
  296. }
  297. } else {
  298. search()
  299. }
  300. }
  301. Bjuiajax.prototype.doLoad = function(options) {
  302. var that = this, $element = that.$element, $target = options.target ? $(options.target) : null
  303. options = $.extend({}, Bjuiajax.DEFAULTS, typeof options == 'object' && options)
  304. if (!options.url) {
  305. BJUI.debug('Error trying to open a ajax link: The url is undefined!')
  306. return
  307. } else {
  308. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  309. if (!options.url.isFinishedTm()) {
  310. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  311. BJUI.debug('The ajax link incorrect: '+ options.url)
  312. return
  313. }
  314. options.url = encodeURI(options.url)
  315. }
  316. if (!$target || !$target.length) {
  317. BJUI.debug('Not set loaded \'ajax\' content container, like [data-target].')
  318. return
  319. }
  320. if ($target && $target.length) {
  321. $target.removeData('bjui.clientPaging')
  322. that.reloadDiv($target, options)
  323. }
  324. }
  325. Bjuiajax.prototype.refreshLayout = function(options) {
  326. var that = this, $element = that.$element, $target = options.target ? $(options.target) : null
  327. options = $.extend({}, Bjuiajax.DEFAULTS, typeof options == 'object' && options)
  328. if (!$target || !$target.length) {
  329. if (autorefreshTimer) clearInterval(autorefreshTimer)
  330. BJUI.debug('Not set loaded \'ajax\' content container, like [data-target].')
  331. return
  332. }
  333. if ($target && $target.length) {
  334. $target.removeData('bjui.clientPaging')
  335. that.reloadDiv($target, $.extend({}, $target.data('options') || {}, options))
  336. }
  337. }
  338. Bjuiajax.prototype.reloadDiv = function($target, options) {
  339. var arefre = options.autorefresh && (isNaN(String(options.autorefresh)) ? 15 : options.autorefresh)
  340. $target
  341. .addClass('bjui-layout')
  342. .data('options', options)
  343. .ajaxUrl({ type:options.type, url:options.url, data:options.data, loadingmask:options.loadingmask, callback:function(html) {
  344. if (BJUI.ui.clientPaging && $target.data('bjui.clientPaging'))
  345. $target.pagination('setPagingAndOrderby', $target)
  346. if (options.callback)
  347. options.callback.apply(this, [$target])
  348. if (autorefreshTimer)
  349. clearInterval(autorefreshTimer)
  350. if (arefre)
  351. autorefreshTimer = setInterval(function() { $target.bjuiajax('refreshLayout', options) }, arefre * 1000)
  352. }
  353. })
  354. }
  355. Bjuiajax.prototype.refreshDiv = function(divid) {
  356. if (divid && typeof divid == 'string') {
  357. var arr = divid.split(',')
  358. for (var i = 0; i < arr.length; i++) {
  359. this.refreshLayout({target: '#'+ arr[i]})
  360. }
  361. }
  362. }
  363. Bjuiajax.prototype.doAjax = function(options) {
  364. var that = this, $element = that.$element
  365. options = $.extend({}, Bjuiajax.DEFAULTS, typeof options == 'object' && options)
  366. if (!options.url) {
  367. BJUI.debug('Error trying to open a ajax link: url is undefined!')
  368. return
  369. } else {
  370. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  371. if (!options.url.isFinishedTm()) {
  372. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  373. BJUI.debug('The ajax url is incorrect: '+ options.url)
  374. return
  375. }
  376. options.url = encodeURI(options.url)
  377. }
  378. var callback = options.callback && options.callback.toFunc()
  379. var todo = function() {
  380. $element.doAjax({type:options.type, url:options.url, data:options.data, callback:callback ? callback : $.proxy(function(data) {that.ajaxCallback(data)}, that)})
  381. }
  382. if (options.confirmMsg) {
  383. $element.alertmsg('confirm', options.confirmMsg, {
  384. okCall: function() {
  385. todo()
  386. }
  387. })
  388. } else {
  389. todo()
  390. }
  391. }
  392. Bjuiajax.prototype.doExport = function(options) {
  393. var that = this, $element = that.$element, $target = options.target ? $(options.target) : null, form
  394. if (!options.url) {
  395. BJUI.debug('Error trying to open a ajax link: url is undefined!')
  396. return
  397. } else {
  398. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  399. if (!options.url.isFinishedTm()) {
  400. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  401. BJUI.debug('The ajax url is incorrect: '+ options.url)
  402. return
  403. }
  404. }
  405. var todo = function() {
  406. if (!$target || !$target.length) {
  407. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  408. $target = $.CurrentNavtab
  409. } else {
  410. $target = $.CurrentDialog
  411. }
  412. }
  413. form = that.tools.getPagerForm($target)
  414. if (form) options.url = encodeURI(options.url + (options.url.indexOf('?') == -1 ? '?' : '&') + $(form).serialize())
  415. $.fileDownload(options.url, {
  416. failCallback: function(responseHtml, url) {
  417. if (responseHtml.trim().startsWith('{')) responseHtml = responseHtml.toObj()
  418. that.ajaxDone(responseHtml)
  419. }
  420. })
  421. }
  422. if (options.confirmMsg) {
  423. $element.alertmsg('confirm', options.confirmMsg, {
  424. okCall: function() {
  425. todo()
  426. }
  427. })
  428. } else {
  429. todo()
  430. }
  431. }
  432. Bjuiajax.prototype.doExportChecked = function(options) {
  433. var that = this, $element = that.$element, $target = options.target ? $(options.target) : null
  434. if (!options.url) {
  435. BJUI.debug('Error trying to open a export link: url is undefined!')
  436. return
  437. } else {
  438. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  439. if (!options.url.isFinishedTm()) {
  440. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  441. BJUI.debug('The ajax url is incorrect: '+ options.url)
  442. return
  443. }
  444. }
  445. var todo = function() {
  446. if (!options.group) {
  447. that.$element.alertmsg('error', options.warn || FRAG.alertNoCheckGroup.replace('#nocheckgroup#', BJUI.regional.nocheckgroup))
  448. return
  449. }
  450. if (!$target || !$target.length) {
  451. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  452. $target = $.CurrentNavtab
  453. } else {
  454. $target = $.CurrentDialog
  455. }
  456. }
  457. var ids = []
  458. var $checks = $target.find(':checkbox[name='+ options.group +']:checked')
  459. if ($checks.length == 0) {
  460. that.$element.alertmsg('error', FRAG.alertNotChecked.replace('#notchecked#', BJUI.regional.notchecked))
  461. return
  462. }
  463. $checks.each(function() {
  464. ids.push($(this).val())
  465. })
  466. options.url = options.url.setUrlParam((options.idname ? options.idname : 'ids'), ids.join(','))
  467. $.fileDownload(options.url, {
  468. failCallback: function(responseHtml, url) {
  469. if (responseHtml.trim().startsWith('{')) responseHtml = responseHtml.toObj()
  470. that.ajaxDone(responseHtml)
  471. }
  472. })
  473. }
  474. if (options.confirmMsg) {
  475. $element.alertmsg('confirm', options.confirmMsg, {
  476. okCall: function() {
  477. todo()
  478. }
  479. })
  480. } else {
  481. todo()
  482. }
  483. }
  484. Bjuiajax.prototype.doAjaxChecked = function(options) {
  485. var that = this, $element = that.$element, $target = options.target ? $(options.target) : null
  486. options = $.extend({}, Bjuiajax.DEFAULTS, typeof options == 'object' && options)
  487. if (!options.url) {
  488. BJUI.debug('Error trying to open a del link: url is undefined!')
  489. return
  490. } else {
  491. options.url = decodeURI(options.url).replacePlh($element.closest('.unitBox'))
  492. if (!options.url.isFinishedTm()) {
  493. $element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  494. BJUI.debug('The ajax url is incorrect: '+ options.url)
  495. return
  496. }
  497. }
  498. var todo = function() {
  499. if (!options.group) {
  500. $element.alertmsg('error', options.warn || FRAG.alertNoCheckGroup.replace('#nocheckgroup#', BJUI.regional.nocheckgroup))
  501. return
  502. }
  503. if (!$target || !$target.length) {
  504. if (that.tools.getTarget() == Bjuiajax.NAVTAB) {
  505. $target = $.CurrentNavtab
  506. } else {
  507. $target = $.CurrentDialog
  508. }
  509. }
  510. var ids = []
  511. var $checks = $target.find(':checkbox[name='+ options.group +']:checked')
  512. var callback = options.callback && options.callback.toFunc()
  513. if ($checks.length == 0) {
  514. $element.alertmsg('error', FRAG.alertNotChecked.replace('#notchecked#', BJUI.regional.notchecked))
  515. return
  516. }
  517. $checks.each(function() {
  518. ids.push($(this).val())
  519. })
  520. options.url = options.url.setUrlParam((options.idname ? options.idname : 'ids'), ids.join(','))
  521. $element.doAjax({type:options.type, url:options.url, data:options.data, callback:callback ? callback : $.proxy(function(data) {that.ajaxCallback(data)}, that)})
  522. }
  523. if (options.confirmMsg) {
  524. $element.alertmsg('confirm', options.confirmMsg, {
  525. okCall: function() {
  526. todo()
  527. }
  528. })
  529. } else {
  530. todo()
  531. }
  532. }
  533. // BJUIAJAX PLUGIN DEFINITION
  534. // =======================
  535. function Plugin(option) {
  536. var args = arguments
  537. var property = option
  538. return this.each(function () {
  539. var $this = $(this)
  540. var options = $.extend({}, Bjuiajax.DEFAULTS, $this.data(), typeof option == 'object' && option)
  541. var data = $this.data('bjui.bjuiajax')
  542. if (!data) $this.data('bjui.bjuiajax', (data = new Bjuiajax(this, options)))
  543. if (typeof property == 'string' && $.isFunction(data[property])) {
  544. [].shift.apply(args)
  545. if (!args) data[property]()
  546. else data[property].apply(data, args)
  547. } else {
  548. return
  549. }
  550. })
  551. }
  552. var old = $.fn.bjuiajax
  553. $.fn.bjuiajax = Plugin
  554. $.fn.bjuiajax.Constructor = Bjuiajax
  555. // BJUIAJAX NO CONFLICT
  556. // =================
  557. $.fn.bjuiajax.noConflict = function () {
  558. $.fn.bjuiajax = old
  559. return this
  560. }
  561. // BJUIAJAX DATA-API
  562. // ==============
  563. $(document).on('submit.bjui.bjuiajax.data-api', 'form[data-toggle="ajaxform"]', function(e) {
  564. var $this = $(this), options = $this.data()
  565. Plugin.call($this, 'ajaxForm', options)
  566. e.preventDefault()
  567. })
  568. /* doSearch */
  569. $(function() {
  570. if ($.validator) {
  571. $(document).on(BJUI.eventType.initUI, function(e) {
  572. $(e.target).find('form[data-toggle="ajaxsearch"]').each(function() {
  573. var $form = $(this), options = $form.data()
  574. options.isValid = true
  575. $form.validator({
  576. valid: function(form) {
  577. Plugin.call($form, 'doSearch', options)
  578. }
  579. })
  580. })
  581. })
  582. } else {
  583. $(document).on('submit.bjui.bjuiajax.data-api', 'form[data-toggle="ajaxsearch"]', function(e) {
  584. var $this = $(this), options = $this.data()
  585. Plugin.call($this, 'doSearch', options)
  586. e.preventDefault()
  587. })
  588. }
  589. })
  590. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="reloadsearch"]', function(e) {
  591. var $this = $(this), options
  592. var $form = $this.closest('form')
  593. if (!$form || !$form.length) return
  594. options = $form.data()
  595. options.clearQuery = $this.data('clearQuery') || true
  596. Plugin.call($form, 'doSearch', options)
  597. e.preventDefault()
  598. })
  599. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="ajaxload"]', function(e) {
  600. var $this = $(this)
  601. var options = $this.data()
  602. if (!options.url) options.url = $this.attr('href')
  603. Plugin.call($this, 'doLoad', options)
  604. e.preventDefault()
  605. })
  606. $(document).on(BJUI.eventType.initUI, function(e) {
  607. var $box = $(e.target).find('[data-toggle="autoajaxload"]')
  608. $box.each(function() {
  609. var $element = $(this), options = $element.data()
  610. options.target = this
  611. Plugin.call($element, 'doLoad', options)
  612. })
  613. })
  614. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="refreshlayout"]', function(e) {
  615. var $this = $(this)
  616. var options = $this.data()
  617. Plugin.call($this, 'refreshLayout', options)
  618. })
  619. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="doajax"]', function(e) {
  620. var $this = $(this)
  621. var options = $this.data()
  622. if (!options.url) options.url = $this.attr('href')
  623. Plugin.call($this, 'doAjax', options)
  624. e.preventDefault()
  625. })
  626. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="doexport"]', function(e) {
  627. var $this = $(this)
  628. var options = $this.data()
  629. if (!options.url) options.url = $this.attr('href')
  630. Plugin.call($this, 'doExport', options)
  631. e.preventDefault()
  632. })
  633. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="doexportchecked"]', function(e) {
  634. var $this = $(this)
  635. var options = $this.data()
  636. if (!options.url) options.url = $this.attr('href')
  637. Plugin.call($this, 'doExportChecked', options)
  638. e.preventDefault()
  639. })
  640. $(document).on('click.bjui.bjuiajax.data-api', '[data-toggle="doajaxchecked"]', function(e) {
  641. var $this = $(this)
  642. var options = $this.data()
  643. if (!options.url) options.url = $this.attr('href')
  644. Plugin.call($this, 'doAjaxChecked', options)
  645. e.preventDefault()
  646. })
  647. }(jQuery);