bjui-lookup.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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-lookup.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.database.js (author:ZhangHuihua@msn.com)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-lookup.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. // LOOKUP GLOBAL ELEMENTS
  19. // ======================
  20. var group, suffix, $currentLookup
  21. // LOOKUP CLASS DEFINITION
  22. // ======================
  23. var Lookup = function(element, options) {
  24. this.$element = $(element)
  25. this.options = options
  26. this.$lookBtn = null
  27. }
  28. Lookup.DEFAULTS = {
  29. url : null,
  30. id : null,
  31. mask : true,
  32. width : 600,
  33. height : 400,
  34. title : 'Lookup',
  35. maxable : true,
  36. resizable : true
  37. }
  38. Lookup.EVENTS = {
  39. afterChange : 'afterchange.bjui.lookup'
  40. }
  41. Lookup.prototype.init = function() {
  42. var that = this, options = this.options, tools = this.tools
  43. if (!options.url) {
  44. BJUI.debug('Lookup Plugin: Error trying open a lookup dialog, url is undefined!')
  45. return false
  46. } else {
  47. options.url = decodeURI(options.url).replacePlh(that.$element.closest('.unitBox'))
  48. if (!options.url.isFinishedTm()) {
  49. that.$element.alertmsg('error', (options.warn || FRAG.alertPlhMsg.replace('#plhmsg#', BJUI.regional.plhmsg)))
  50. BJUI.debug('Lookup Plugin: The lookup\'s url is incorrect, url:'+ options.url)
  51. return false
  52. }
  53. options.url = encodeURI(options.url)
  54. }
  55. group = this.options.group || null
  56. suffix = this.options.suffix || null
  57. $currentLookup = this.$element
  58. if (suffix) suffix = suffix.trim()
  59. this.open(that.$element)
  60. }
  61. Lookup.prototype.addBtn = function() {
  62. var that = this, $element = that.$element
  63. if (!this.$lookBtn && !$element.parent().hasClass('wrap_bjui_btn_box')) {
  64. this.$lookBtn = $(FRAG.lookupBtn)
  65. this.$element.css({'paddingRight':'15px'}).wrap('<span class="wrap_bjui_btn_box"></span>')
  66. var $box = this.$element.parent()
  67. var height = this.$element.addClass('form-control').innerHeight()
  68. $box.css({'position':'relative', 'display':'inline-block'})
  69. $.each(that.options, function(key, val) {
  70. if (key != 'toggle') that.$lookBtn.data(key, val)
  71. })
  72. this.$lookBtn.css({'height':height, 'lineHeight':height +'px'}).appendTo($box)
  73. this.$lookBtn.on('selectstart', function() { return false })
  74. }
  75. }
  76. Lookup.prototype.open = function($obj) {
  77. var that = this, options = this.options
  78. $obj.dialog({id:options.id || 'lookup_dialog', url:options.url, title:options.title, width:options.width, height:options.height, mask:options.mask, maxable:options.maxable, resizable:options.resizable})
  79. }
  80. Lookup.prototype.getField = function(key) {
  81. return (group ? (group +'.') : '') + (key) + (suffix ? suffix : '')
  82. }
  83. Lookup.prototype.setSingle = function(args,type) {
  84. if (typeof args == 'string')
  85. args = new Function('return '+ args)()
  86. this.setVal(args,type)
  87. }
  88. Lookup.prototype.setMult = function(id,type) {
  89. var args = {}
  90. var $unitBox = this.$element.closest('.unitBox')
  91. $unitBox.find('[name="'+ id +'"]').filter(':checked').each(function() {
  92. var _args = new Function('return '+ $(this).val())()
  93. for (var key in _args) {
  94. var value = args[key] ? args[key] +',' : ''
  95. args[key] = value + _args[key]
  96. }
  97. })
  98. if ($.isEmptyObject(args)) {
  99. this.$element.alertmsg('error', this.$element.data('warn') || FRAG.alertSelectMsg)
  100. return
  101. }
  102. this.setVal(args,type)
  103. }
  104. Lookup.prototype.setVal = function(args, type) {
  105. var that = this
  106. var $box = $currentLookup.closest('.unitBox')
  107. var newValue /* @description 增加 @author 小策一喋 */
  108. // for datagrid
  109. if ($currentLookup.data('customEvent')) {
  110. $currentLookup.trigger('customEvent.bjui.lookup', [args])
  111. } else {
  112. $box.find(':input').each(function() {
  113. var $input = $(this), inputName = $input.attr('name')
  114. for (var key in args) {
  115. var name = that.getField(key)
  116. if (name == inputName) {
  117. /* @description 增加 追加参数 @author 小策一喋 */
  118. if(type == 1)
  119. newValue = $input.val() ? $input.val() + ',' + args[key] : args[key]
  120. else
  121. newValue = args[key]
  122. $input
  123. .val(newValue) /* @description 修改 args[key] 为 newValue @author 小策一喋 */
  124. .trigger(Lookup.EVENTS.afterChange, {value:args[key]})
  125. break
  126. }
  127. }
  128. })
  129. }
  130. this.$element.dialog('closeCurrent')
  131. }
  132. // LOOKUP PLUGIN DEFINITION
  133. // =======================
  134. function Plugin(option) {
  135. var args = arguments
  136. var property = option
  137. return this.each(function () {
  138. var $this = $(this)
  139. var options = $.extend({}, Lookup.DEFAULTS, $this.data(), typeof option == 'object' && option)
  140. var data = $this.data('bjui.lookup')
  141. if (!data) {
  142. $this.data('bjui.lookup', (data = new Lookup(this, options)))
  143. } else if ($this.data('newurl')) {
  144. data.options.url = $this.data('newurl')
  145. $this.data('bjui.dialog', null)
  146. }
  147. if (typeof property == 'string' && $.isFunction(data[property])) {
  148. [].shift.apply(args)
  149. if (!args) data[property]()
  150. else data[property].apply(data, args)
  151. } else {
  152. data.init()
  153. }
  154. })
  155. }
  156. var old = $.fn.lookup
  157. $.fn.lookup = Plugin
  158. $.fn.lookup.Constructor = Lookup
  159. // LOOKUP NO CONFLICT
  160. // =================
  161. $.fn.lookup.noConflict = function () {
  162. $.fn.lookup = old
  163. return this
  164. }
  165. // LOOKUP DATA-API
  166. // ==============
  167. $(document).on(BJUI.eventType.initUI, function(e) {
  168. var $this = $(e.target).find('[data-toggle="lookup"]')
  169. if (!$this.length) return
  170. Plugin.call($this, 'addBtn')
  171. })
  172. $(document).on('click.bjui.lookup.data-api', '[data-toggle="lookupbtn"]', function(e) {
  173. var $this = $(this)
  174. if ($this.attr('href') && !$this.data('url')) $this.attr('data-url', $this.attr('href'))
  175. if (!$this.data('title')) $this.attr('data-title', $this.text())
  176. Plugin.call($this)
  177. e.preventDefault()
  178. })
  179. $(document).on('click.bjui.lookupback.data-api', '[data-toggle="lookupback"]', function(e) {
  180. var $this = $(this)
  181. var args = $this.data('args')
  182. var mult = $this.data('lookupid')
  183. var type = $('input[name="lookupType"]:checked').val() /* @description 新增 获取是否追加框值 @author 小策一喋 */
  184. if (args)
  185. Plugin.call($this, 'setSingle', args, type) /* @description 修改 增加type参数 @author 小策一喋 */
  186. else if (mult)
  187. Plugin.call($this, 'setMult', mult, type) /* @description 修改 增加type参数 @author 小策一喋 */
  188. e.preventDefault()
  189. })
  190. }(jQuery);