You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

224 line
6.0 KiB

  1. wn.pages['user-properties'].onload = function(wrapper) {
  2. wn.ui.make_app_page({
  3. parent: wrapper,
  4. title: 'User Properties',
  5. single_column: true
  6. });
  7. $(wrapper).find(".layout-main").html("<div class='user-settings'></div>\
  8. <table class='table table-bordered' style='background-color: #f9f9f9;'>\
  9. <tr><td>\
  10. <h4><i class='icon-question-sign'></i> Quick Help for User Properties:</h4>\
  11. <ol>\
  12. <li>You can set various 'properties' to Users.</li>\
  13. <li>These properties are Link Type fields from all Documents.</li>\
  14. <li>These properties will appear as values in forms that contain them.</li>\
  15. <li>These properties can also be used to 'assign' a particular document, \
  16. whose property matches with the User's property to a User. These can be set\
  17. using the <a href='#permission-manager'>Permission Manager</a></li>\
  18. <li>A user can have multiple values for a property.</li>\
  19. </ol>\
  20. </tr></td>\
  21. </table>");
  22. wrapper.user_properties = new wn.UserProperties(wrapper);
  23. }
  24. wn.pages['user-properties'].refresh = function(wrapper) {
  25. wrapper.user_properties.set_from_route();
  26. }
  27. wn.UserProperties = Class.extend({
  28. init: function(wrapper) {
  29. this.wrapper = wrapper;
  30. this.body = $(this.wrapper).find(".user-settings");
  31. this.make();
  32. this.refresh();
  33. },
  34. make: function() {
  35. var me = this;
  36. wn.call({
  37. module:"core",
  38. page:"user_properties",
  39. method: "get_users_and_links",
  40. callback: function(r) {
  41. me.options = r.message;
  42. me.user_select =
  43. me.wrapper.appframe.add_select("users",
  44. ["Select User..."].concat(r.message.users))
  45. .css("width", "200px")
  46. .change(function() {
  47. wn.set_route("user-properties", $(this).val())
  48. });
  49. me.property_select =
  50. me.wrapper.appframe.add_select("links",
  51. ["Select Property..."].concat(me.get_link_names()))
  52. .css("width", "200px")
  53. .change(function() {
  54. me.refresh();
  55. });
  56. me.set_from_route();
  57. }
  58. });
  59. },
  60. get_link_names: function() {
  61. return $.map(this.options.link_fields, function(l) { return l[0]; });
  62. },
  63. set_from_route: function() {
  64. if(wn.get_route()[1] && this.user_select) {
  65. this.user_select.val(wn.get_route()[1]);
  66. this.refresh();
  67. } else {
  68. this.refresh();
  69. }
  70. },
  71. get_user: function() {
  72. var user = this.user_select.val();
  73. return user=="Select User..." ? null : user;
  74. },
  75. get_property: function() {
  76. var property = this.property_select.val();
  77. return property=="Select Property..." ? null : property;
  78. },
  79. render: function(prop_list) {
  80. this.body.empty();
  81. this.prop_list = prop_list;
  82. if(!prop_list.length) {
  83. this.body.html("<div class='alert'>No User Properties found.</div>");
  84. } else {
  85. this.show_property_table();
  86. }
  87. this.show_add_property();
  88. },
  89. refresh: function() {
  90. var me = this;
  91. if(!me.user_select) {
  92. this.body.html("<div class='alert'>Loading...</div>");
  93. return;
  94. }
  95. if(!me.get_user() && !me.get_property()) {
  96. this.body.html("<div class='alert'>Select User or Property to start.</div>");
  97. return;
  98. }
  99. // get permissions
  100. wn.call({
  101. module: "core",
  102. page: "user_properties",
  103. method: "get_properties",
  104. args: {
  105. user: me.get_user(),
  106. key: me.get_property()
  107. },
  108. callback: function(r) {
  109. me.render(r.message);
  110. }
  111. });
  112. },
  113. show_property_table: function() {
  114. var me = this;
  115. this.table = $("<table class='table table-bordered'>\
  116. <thead><tr></tr></thead>\
  117. <tbody></tbody>\
  118. </table>").appendTo(this.body);
  119. $.each([["User", 150], ["Property", 150], ["Value",150], ["", 50]],
  120. function(i, col) {
  121. $("<th>").html(col[0]).css("width", col[1]+"px")
  122. .appendTo(me.table.find("thead tr"));
  123. });
  124. $.each(this.prop_list, function(i, d) {
  125. var row = $("<tr>").appendTo(me.table.find("tbody"));
  126. $("<td>").html('<a href="#Form/Profile/'+d.parent+'">'
  127. +d.parent+'</a>').appendTo(row);
  128. $("<td>").html(d.defkey).appendTo(row);
  129. $("<td>").html(d.defvalue).appendTo(row);
  130. me.add_delete_button(row, d);
  131. });
  132. },
  133. add_delete_button: function(row, d) {
  134. var me = this;
  135. $("<button class='btn btn-small'><i class='icon-remove'></i></button>")
  136. .appendTo($("<td>").appendTo(row))
  137. .attr("data-name", d.name)
  138. .attr("data-user", d.parent)
  139. .click(function() {
  140. wn.call({
  141. module: "core",
  142. page: "user_properties",
  143. method: "remove",
  144. args: {
  145. name: $(this).attr("data-name"),
  146. user: $(this).attr("data-user")
  147. },
  148. callback: function(r) {
  149. if(r.exc) {
  150. msgprint("Did not remove.");
  151. } else {
  152. me.refresh();
  153. }
  154. }
  155. })
  156. });
  157. },
  158. show_add_property: function() {
  159. var me = this;
  160. $("<button class='btn btn-info'>Add A Property</button>")
  161. .appendTo($("<p>").appendTo(this.body))
  162. .click(function() {
  163. var d = new wn.ui.Dialog({
  164. title: "Add New Property",
  165. fields: [
  166. {fieldtype:"Select", label:"User",
  167. options:me.options.users, reqd:1, fieldname:"parent"},
  168. {fieldtype:"Select", label:"Property", fieldname:"defkey",
  169. options:me.get_link_names(), reqd:1},
  170. {fieldtype:"Link", label:"Value", fieldname:"defvalue",
  171. options:'[Select]', reqd:1},
  172. {fieldtype:"Button", label:"Add"},
  173. ]
  174. });
  175. if(me.get_user()) {
  176. d.set_value("parent", me.get_user());
  177. d.get_input("parent").attr("disabled", true);
  178. }
  179. if(me.get_property()) {
  180. d.set_value("defkey", me.get_property());
  181. d.get_input("defkey").attr("disabled", true);
  182. }
  183. d.fields_dict["defvalue"].get_query = function(txt) {
  184. var key = d.get_value("defkey");
  185. var doctype = $.map(me.options.link_fields, function(l) {
  186. if(l[0]==key) return l[1];
  187. })[0];
  188. return 'select name from `tab'+doctype
  189. +'` where name like "%s" limit 20'
  190. }
  191. d.get_input("add").click(function() {
  192. var args = d.get_values();
  193. if(!args) {
  194. return;
  195. }
  196. wn.call({
  197. module: "core",
  198. page: "user_properties",
  199. method: "add",
  200. args: args,
  201. callback: function(r) {
  202. if(r.exc) {
  203. msgprint("Did not add.");
  204. } else {
  205. me.refresh();
  206. }
  207. }
  208. })
  209. d.hide();
  210. });
  211. d.show();
  212. });
  213. }
  214. })