yii.admin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. yii.admin = (function ($) {
  2. var _onSearch = false;
  3. var pub = {
  4. userId: undefined,
  5. roleName: undefined,
  6. assignUrl: undefined,
  7. searchUrl: undefined,
  8. assign: function (action) {
  9. var params = {
  10. id: pub.userId,
  11. action: action,
  12. roles: $('#list-' + (action == 'assign' ? 'avaliable' : 'assigned')).val(),
  13. };
  14. $.ajax({
  15. url:pub.assignUrl,
  16. method:'post',
  17. data:params,
  18. success:function () {
  19. pub.searchAssignmet('avaliable', true);
  20. pub.searchAssignmet('assigned', true);
  21. },
  22. error:function (xhr, textStatus) {
  23. $('#alert-info').find('.modal-body').text(xhr.responseText);
  24. $('#alert-info').modal('show')
  25. }
  26. });
  27. },
  28. searchAssignmet: function (target, force) {
  29. if (!_onSearch || force) {
  30. _onSearch = true;
  31. var $inp = $('#search-' + target);
  32. setTimeout(function () {
  33. var data = {
  34. id: pub.userId,
  35. target: target,
  36. term: $inp.val(),
  37. };
  38. $.get(pub.searchUrl, data,
  39. function (r) {
  40. var $list = $('#list-' + target);
  41. $list.html('');
  42. if (r.Roles) {
  43. var $group = $('<optgroup label="Roles">');
  44. $.each(r.Roles, function () {
  45. $('<option>').val(this).text(this).appendTo($group);
  46. });
  47. $group.appendTo($list);
  48. }
  49. if (r.Permissions) {
  50. var $group = $('<optgroup label="Permissions">');
  51. $.each(r.Permissions, function () {
  52. $('<option>').val(this).text(this).appendTo($group);
  53. });
  54. $group.appendTo($list);
  55. }
  56. }).done(function () {
  57. _onSearch = false;
  58. });
  59. }, 500);
  60. }
  61. },
  62. // role & permission
  63. addChild: function (action) {
  64. var params = {
  65. id: pub.roleName,
  66. action: action,
  67. roles: $('#list-' + (action == 'assign' ? 'avaliable' : 'assigned')).val(),
  68. };
  69. $.ajax({
  70. url:pub.assignUrl,
  71. method:'post',
  72. data:params,
  73. success:function () {
  74. pub.searchRole('avaliable', true);
  75. pub.searchRole('assigned', true);
  76. },
  77. error:function (xhr, textStatus) {
  78. $('#alert-info').find('.modal-body').text(xhr.responseText);
  79. $('#alert-info').modal('show')
  80. }
  81. });
  82. },
  83. searchRole: function (target, force) {
  84. if (!_onSearch || force) {
  85. _onSearch = true;
  86. var $inp = $('#search-' + target);
  87. setTimeout(function () {
  88. var data = {
  89. id: pub.roleName,
  90. target: target,
  91. term: $inp.val(),
  92. };
  93. $.get(pub.searchUrl, data,
  94. function (r) {
  95. var $list = $('#list-' + target);
  96. $list.html('');
  97. if (r.Roles) {
  98. var $group = $('<optgroup label="Roles">');
  99. $.each(r.Roles, function () {
  100. $('<option>').val(this).text(this).appendTo($group);
  101. });
  102. $group.appendTo($list);
  103. }
  104. if (r.Permissions) {
  105. var $group = $('<optgroup label="Permissions">');
  106. $.each(r.Permissions, function () {
  107. $('<option>').val(this).text(this).appendTo($group);
  108. });
  109. $group.appendTo($list);
  110. }
  111. if (r.Routes) {
  112. var $group = $('<optgroup label="Routes">');
  113. $.each(r.Routes, function () {
  114. $('<option>').val(this).text(this).appendTo($group);
  115. });
  116. $group.appendTo($list);
  117. }
  118. }).done(function () {
  119. _onSearch = false;
  120. });
  121. }, 500);
  122. }
  123. },
  124. // route
  125. assignRoute: function (action) {
  126. var params = {
  127. action: action,
  128. routes: $('#list-' + (action == 'assign' ? 'avaliable' : 'assigned')).val(),
  129. };
  130. $.post(pub.assignUrl, params,
  131. function () {
  132. pub.searchRoute('avaliable', 0, true);
  133. pub.searchRoute('assigned', 0, true);
  134. });
  135. },
  136. searchRoute: function (target, refresh, force) {
  137. if (!_onSearch || force) {
  138. _onSearch = true;
  139. var $inp = $('#search-' + target);
  140. setTimeout(function () {
  141. var data = {
  142. target: target,
  143. term: $inp.val(),
  144. refresh: refresh,
  145. };
  146. $.get(pub.searchUrl, data,
  147. function (r) {
  148. var $list = $('#list-' + target);
  149. $list.html('');
  150. $.each(r, function (key, val) {
  151. var $opt = $('<option>').val(key).text(key);
  152. if (!val) {
  153. $opt.addClass('lost');
  154. }
  155. $opt.appendTo($list);
  156. });
  157. }).done(function () {
  158. _onSearch = false;
  159. });
  160. }, 500);
  161. }
  162. },
  163. initProperties: function (properties) {
  164. $.each(properties, function (key, val) {
  165. pub[key] = val;
  166. });
  167. },
  168. }
  169. return pub;
  170. })(jQuery)