userpicker.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. var createUserPicker = function(conf) {
  2. conf = conf ? conf : {};
  3. var defaults = {
  4. modalId: 'userPicker',
  5. multiple: false,
  6. showExpression: false,
  7. url: '/mossle-web-user/default/rs/user/search'
  8. };
  9. for (var key in defaults) {
  10. if (!conf[key]) {
  11. conf[key] = defaults[key];
  12. }
  13. }
  14. if ($('#' + conf.modalId).length == 0) {
  15. $(document.body).append(
  16. '<div id="' + conf.modalId + '" class="modal hide fade">'
  17. +' <div class="modal-header">'
  18. +' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
  19. +' <h3>选择用户</h3>'
  20. +' </div>'
  21. +' <div class="modal-body">'
  22. +' <ul class="nav nav-tabs" role="tablist" id="userPickerTabs">'
  23. +' <li role="presentation" class="active"><a href="#user" aria-controls="user" role="tab" data-toggle="tab">选择用户</a></li>'
  24. +' <li role="presentation" ' + (conf.showExpression !== true ? 'style="display:none;"' : '') + '><a href="#common" aria-controls="common" role="tab" data-toggle="tab">常用语</a></li>'
  25. +' <li role="presentation" ' + (conf.showExpression !== true ? 'style="display:none;"' : '') + '><a href="#expr" aria-controls="expr" role="tab" data-toggle="tab">表达式</a></li>'
  26. +' </ul>'
  27. +' <div class="tab-content">'
  28. +' <div role="tabpanel" class="tab-pane active" id="user">'
  29. +' <article class="m-blank">'
  30. +' <div class="pull-left" style="display:table"><div style="display:table-cell">'
  31. +' <label for="' + conf.modalId + '_username" style="display:inline">账号:</label>'
  32. +' <input type="text" id="' + conf.modalId + '_username" value="" style="margin-bottom:0px;">'
  33. +' <button id="' + conf.modalId + '_search" class="btn btn-small">查询</button></div>'
  34. +' </div>'
  35. +' <div class="m-clear"></div>'
  36. +' </article>'
  37. +' <article class="m-widget">'
  38. +' <header class="header">'
  39. +' <h4 class="title">用户</h4>'
  40. +' </header>'
  41. +' <div class="content">'
  42. +' <table id="' + conf.modalId + '_grid" class="m-table table-hover">'
  43. +' <thead>'
  44. +' <tr>'
  45. +' <th width="10" class="m-table-check">&nbsp;</th>'
  46. +' <th>姓名</th>'
  47. +' </tr>'
  48. +' </thead>'
  49. +' <tbody id="' + conf.modalId + '_body">'
  50. +' <tr>'
  51. +' <td><input id="' + conf.modalId + '_item_1" type="' + (conf.multiple ? 'checkbox' : 'radio') + '" name="selectedItem" class="selectedItem" value="1" title="admin" style="margin-top:0px;"></td>'
  52. +' <td>admin</td>'
  53. +' </tr>'
  54. +' <tr>'
  55. +' <td><input id="' + conf.modalId + '_item_2" type="' + (conf.multiple ? 'checkbox' : 'radio') + '" name="selectedItem" class="selectedItem" value="2" title="user" style="margin-top:0px;"></td>'
  56. +' <td>user</td>'
  57. +' </tr>'
  58. +' </tbody>'
  59. +' </table>'
  60. +' </div>'
  61. +' </article>'
  62. +' </div>'
  63. +' <div role="tabpanel" class="tab-pane" id="common">'
  64. +' <article class="m-widget">'
  65. +' <header class="header">'
  66. +' <h4 class="title">用户</h4>'
  67. +' </header>'
  68. +' <div class="content">'
  69. +' <table id="' + conf.modalId + '_commonGrid" class="m-table table-hover">'
  70. +' <thead>'
  71. +' <tr>'
  72. +' <th width="10" class="m-table-check">&nbsp;</th>'
  73. +' <th>姓名</th>'
  74. +' </tr>'
  75. +' </thead>'
  76. +' <tbody id="' + conf.modalId + '_commonBody">'
  77. +' <tr>'
  78. +' <td><input id="' + conf.modalId + '_item_common_1" type="radio" name="selectedItem" class="selectedItem" value="常用语:直接上级" title="常用语:直接上级" style="margin-top:0px;"></td>'
  79. +' <td>常用语:直接上级</td>'
  80. +' </tr>'
  81. +' <tr>'
  82. +' <td><input id="' + conf.modalId + '_item_common_2" type="radio" name="selectedItem" class="selectedItem" value="岗位:经理" title="岗位:经理" style="margin-top:0px;"></td>'
  83. +' <td>岗位:经理</td>'
  84. +' </tr>'
  85. +' <tr>'
  86. +' <td><input id="' + conf.modalId + '_item_common_3" type="radio" name="selectedItem" class="selectedItem" value="岗位:总经理" title="岗位:总经理" style="margin-top:0px;"></td>'
  87. +' <td>岗位:总经理</td>'
  88. +' </tr>'
  89. +' </tbody>'
  90. +' </table>'
  91. +' </div>'
  92. +' </article>'
  93. +' </div>'
  94. +' <div role="tabpanel" class="tab-pane" id="expr">'
  95. +' <article class="m-widget">'
  96. +' <header class="header">'
  97. +' <h4 class="title">表达式</h4>'
  98. +' </header>'
  99. +' <div class="content">'
  100. +' <table id="' + conf.modalId + '_exprGrid" class="m-table table-hover">'
  101. +' <thead>'
  102. +' <tr>'
  103. +' <th>姓名</th>'
  104. +' </tr>'
  105. +' </thead>'
  106. +' <tbody id="' + conf.modalId + '_exprBody">'
  107. +' <tr>'
  108. +' <td><input id="' + conf.modalId + '_item_expr_1" type="text" name="selectedItem" class="selectedItem" value="${initiator}" title="${initator}" style="margin-top:0px;"></td>'
  109. +' </tr>'
  110. +' </tbody>'
  111. +' </table>'
  112. +' </div>'
  113. +' </article>'
  114. +' </div>'
  115. +' </div>'
  116. +' </div>'
  117. +' <div class="modal-footer">'
  118. +' <span id="' + conf.modalId + '_result" style="float:left;"></span>'
  119. +' <a id="' + conf.modalId + '_close" href="#" class="btn" data-dismiss="modal">关闭</a>'
  120. +' <a id="' + conf.modalId + '_select" href="#" class="btn btn-primary">选择</a>'
  121. +' </div>'
  122. +'</div>');
  123. $('#userPickerTabs a').click(function (e) {
  124. e.preventDefault();
  125. $(this).tab('show');
  126. })
  127. }
  128. var doSearch = function(username) {
  129. $.ajax({
  130. url: conf.url,
  131. data: {
  132. username: username
  133. },
  134. success: function(data) {
  135. var html = '';
  136. for (var i = 0; i < data.length; i++) {
  137. var item = data[i];
  138. html +=
  139. '<tr>'
  140. +'<td><input id="' + conf.modalId + '_item_' + i + '" type="' + (conf.multiple ? 'checkbox' : 'radio') + '" class="selectedItem" name="name" value="'
  141. + item.id + '" title="' + item.displayName + '"></td>'
  142. +'<td><label for="' + conf.modalId + '_item_' + i + '">' + item.displayName + '</label></td>'
  143. +'</tr>'
  144. }
  145. $('#' + conf.modalId + '_body').html(html);
  146. }
  147. });
  148. }
  149. $(document).delegate('.userPicker .add-on', 'click', function(e) {
  150. $('#' + conf.modalId).data('userPicker', $(this).parent());
  151. $('#' + conf.modalId).modal();
  152. doSearch('');
  153. });
  154. // $(document).delegate('#' + conf.modalId + '_body tr', 'click', function(e) {
  155. // $('input[type=radio].selectedItem').prop('checked', false);
  156. // $(this).find('.selectedItem').prop('checked', true);
  157. // });
  158. $(document).delegate('#' + conf.modalId + '_body .selectedItem', 'click', function(e) {
  159. if (conf.multiple) {
  160. var el = $(this);
  161. if (el.prop('checked')) {
  162. var html = '&nbsp;<span class="label" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="icon-minus-sign" style="cursor:pointer;"></i></span>';
  163. $('#' + conf.modalId + '_result').append(html);
  164. } else {
  165. $('#' + conf.modalId + '_result #' + el.val()).remove();
  166. }
  167. } else {
  168. var html = '<span class="label" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="icon-minus-sign" style="cursor:pointer;"></i></span>';
  169. $('#' + conf.modalId + '_result').html(html);
  170. }
  171. });
  172. $(document).delegate('#' + conf.modalId + '_commonBody .selectedItem', 'click', function(e) {
  173. var html = '<span class="label" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="icon-minus-sign" style="cursor:pointer;"></i></span>';
  174. $('#' + conf.modalId + '_result').html(html);
  175. });
  176. $(document).delegate('#' + conf.modalId + '_exprBody .selectedItem', 'click', function(e) {
  177. var html = '<span class="label" id="' + $(this).val() + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '<i class="icon-minus-sign" style="cursor:pointer;"></i></span>';
  178. $('#' + conf.modalId + '_result').html(html);
  179. });
  180. $(document).delegate('.icon-minus-sign', 'click', function(e) {
  181. var id = $(this).parent().attr('id');
  182. $('#' + conf.modalId + '_item_' + id).prop('checked', false);
  183. $(this).parent().remove();
  184. });
  185. $(document).delegate('#' + conf.modalId + '_search', 'click', function(e) {
  186. doSearch($('#' + conf.modalId + '_username').val());
  187. });
  188. $(document).delegate('#' + conf.modalId + '_select', 'click', function(e) {
  189. $('#' + conf.modalId).modal('hide');
  190. var userPickerElement = $('#' + conf.modalId).data('userPicker');
  191. if (conf.multiple) {
  192. var el = $('#' + conf.modalId + '_result .label');
  193. var ids = [];
  194. var names = [];
  195. el.each(function(index, item) {
  196. ids.push($(item).attr('id'));
  197. names.push($(item).attr('title'));
  198. });
  199. userPickerElement.children('input[type=hidden]').val(ids.join(','));
  200. userPickerElement.children('input[type=text]').val(names.join(','));
  201. } else {
  202. var el = $('#' + conf.modalId + '_result .label');
  203. userPickerElement.children('input[type=hidden]').val(el.attr('id'));
  204. userPickerElement.children('input[type=text]').val(el.attr('title'));
  205. }
  206. });
  207. }