andreybotanic

limitedUserPicker.js

Dec 6th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createSelect2(customFieldId) {
  2.     AJS.$("#" + customFieldId).auiSelect2({
  3.         ajax: {
  4.             url:  AJS.contextPath() + "/rest/limited_user_picker/2.0/search",
  5.             data: function(params) {
  6.                 console.log(params);
  7.                 return {
  8.                     template: params
  9.                 };
  10.             },
  11.             dataType: 'json',
  12.             type: "GET",
  13.             delay: 250,
  14.             results: function (data) {
  15.                 var users = $.map(data.users, function(user) {
  16.                     return {
  17.                         id: user.username,
  18.                         text: user.displayName
  19.                     };
  20.                 });
  21.                 var groups = $.map(data.groups, function(group) {
  22.                     return {
  23.                         id: group,
  24.                         text: group,
  25.                         type: "group"
  26.                     };
  27.                 });
  28.  
  29.                 var result = [];
  30.                 if (groups.length) {
  31.                     result.push({
  32.                         text: "Группы",
  33.                         children: groups
  34.                     });
  35.                 }
  36.                 if (users.length) {
  37.                     result.push({
  38.                         text: "Пользователи",
  39.                         children: users
  40.                     });
  41.                 }
  42.                 return {
  43.                     results: result
  44.                 };
  45.             },
  46.             cache: true
  47.         },
  48.         minimumInputLength: 2,
  49.         formatResult: resultFormat,
  50.         formatSelection: selectFormat,
  51.         escapeMarkup: function(m) { return m; },
  52.         placeholder: "Users or groups",
  53.         allowClear: true,
  54.         multiple: true
  55.     });
  56. }
  57.  
  58. function resultFormat(result, container, query) {
  59.     var pattern = new RegExp(query.term, 'g');
  60.     if (!result.id) {
  61.         return result.text;
  62.     } else if (!result.type) {
  63.         return (result.text + " - " + result.id).replace(pattern, "<strong>$&</strong>");
  64.     } else {
  65.         return result.text.replace(pattern, "<strong>$&</strong>");
  66.     }
  67. }
  68.  
  69. function selectFormat(result) {
  70.     if (!result.type) {
  71.         return "<span class=\"aui-icon aui-icon-small aui-iconfont-user limited-user-picker-icon\">User</span>" + result.id;
  72.     } else {
  73.         return "<span class=\"aui-icon aui-icon-small aui-iconfont-group limited-user-picker-icon\">Group</span>" + result.id;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment