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.
 
 
 
 
 
 

346 lines
9.7 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // wn._("Form")
  4. wn.ui.AppFrame = Class.extend({
  5. init: function(parent, title, module) {
  6. this.set_document_title = true;
  7. this.buttons = {};
  8. this.fields_dict = {};
  9. this.parent = parent;
  10. this.$title_area = $('<div class="title-area">\
  11. <h4>\
  12. <span class="title-icon text-muted" style="display: none"></span>\
  13. <span class="title-text"></span>\
  14. </h4></div>').appendTo(parent.find(".titlebar-item.text-center"));
  15. this.setup_iconbar();
  16. if(title)
  17. this.set_title(title);
  18. },
  19. setup_iconbar: function() {
  20. var me = this;
  21. this.iconbar = new wn.ui.IconBar(this.parent.find(".appframe-iconbar .container"), 3);
  22. this.iconbar.$wrapper.find(".iconbar-3").addClass("pull-right");
  23. this.iconbar.$wrapper.on("shown", function() {
  24. me.parent.find(".appframe-iconbar").removeClass("hide")
  25. })
  26. this.iconbar.$wrapper.on("hidden", function() {
  27. me.parent.find(".appframe-iconbar").addClass("hide")
  28. })
  29. },
  30. // appframe::title
  31. get_title_area: function() {
  32. return this.$title_area;
  33. },
  34. set_title: function(txt) {
  35. // strip icon
  36. this.title = txt;
  37. document.title = txt.replace(/<[^>]*>/g, "");
  38. this.$title_area.find(".title-text").html(txt);
  39. },
  40. set_title_left: function(txt, click) {
  41. return $("<a>")
  42. .html(txt)
  43. .on("click", function() { click.apply(this); })
  44. .appendTo(this.parent.find(".titlebar-item.text-left").empty());
  45. },
  46. set_title_right: function(txt, click, icon) {
  47. var $right = this.parent.find(".titlebar-item.text-right")
  48. if(txt) {
  49. this.title_right && this.title_right.remove();
  50. this.title_right = $("<a>")
  51. .html((icon ? '<i class="'+icon+'"></i> ' : "") + txt)
  52. .click(click)
  53. .appendTo($right.attr("data-text", txt));
  54. return this.title_right;
  55. } else {
  56. $right.empty().attr("data-text", "");
  57. this.title_right = null;
  58. this.primary_dropdown = null;
  59. this.primary_action = null;
  60. }
  61. },
  62. get_title_right_text: function() {
  63. return this.parent.find(".titlebar-item.text-right").attr("data-text");
  64. },
  65. add_primary_action: function(label, click, icon) {
  66. if(!this.primary_dropdown) {
  67. if(!this.primary_action) {
  68. var $right = this.parent.find(".titlebar-item.text-right");
  69. this.primary_action = $("<a>")
  70. .html(wn._("Actions") + " <i class='icon-caret-down'></i>")
  71. .css({"margin-right":"15px", "display":"inline-block"})
  72. .prependTo($right);
  73. }
  74. var id = "dropdown-" + wn.dom.set_unique_id();
  75. this.primary_action
  76. .attr("id", id)
  77. .attr("data-toggle", "dropdown")
  78. .addClass("dropdown-toggle")
  79. .parent()
  80. .addClass("dropdown")
  81. this.primary_dropdown = $('<ul class="dropdown-menu pull-right" role="menu" \
  82. aria-labelledby="'+ id +'"></ul>')
  83. .appendTo(this.primary_action.parent()).dropdown();
  84. }
  85. var $li = $('<li role="presentation"><a role="menuitem" class="text-left">'
  86. + (icon ? '<i class="'+icon+' icon-fixed-width"></i> ' : "") + label+'</a></li>')
  87. .appendTo(this.primary_dropdown)
  88. .on("click", function() { click && click.apply(this); });
  89. return $li;
  90. },
  91. set_views_for: function(doctype, active_view) {
  92. this.doctype = doctype;
  93. var me = this,
  94. meta = locals.DocType[doctype],
  95. views = [],
  96. module_info = wn.modules[meta.module];
  97. if(module_info) {
  98. views.push({
  99. icon: module_info.icon,
  100. route: module_info.link,
  101. type: "module"
  102. })
  103. }
  104. views.push({
  105. icon: "icon-file-alt",
  106. route: "",
  107. type: "form",
  108. set_route: function() {
  109. if(wn.views.formview[me.doctype]) {
  110. wn.set_route("Form", me.doctype, wn.views.formview[me.doctype].frm.docname);
  111. } else {
  112. new_doc(doctype);
  113. }
  114. }
  115. });
  116. if(!meta.issingle) {
  117. views.push({
  118. icon: "icon-list",
  119. route: "List/" + doctype,
  120. type: "list"
  121. });
  122. }
  123. if(wn.views.calendar[doctype]) {
  124. views.push({
  125. icon: "icon-calendar",
  126. route: "Calendar/" + doctype,
  127. type: "calendar"
  128. });
  129. }
  130. if(wn.views.calendar[doctype] && wn.views.calendar[doctype]) {
  131. views.push({
  132. icon: "icon-tasks",
  133. route: "Gantt/" + doctype,
  134. type: "gantt"
  135. });
  136. }
  137. if(wn.model.can_get_report(doctype)) {
  138. views.push({
  139. icon: "icon-table",
  140. route: "Report/" + doctype,
  141. type: "report"
  142. });
  143. }
  144. this.set_views(views, active_view);
  145. },
  146. set_views: function(views, active_view) {
  147. var me = this;
  148. $.each(views, function(i, e) {
  149. var btn = me.add_icon_btn("3", e.icon, wn._(toTitle(e.type)), function() {
  150. window.location.hash = "#" + $(this).attr("data-route");
  151. }).attr("data-route", e.route);
  152. if(e.type===active_view) {
  153. btn.find("i").css({"color": "#428bca"});
  154. }
  155. });
  156. },
  157. add_module_icon: function(module, doctype, onclick, sub_title) {
  158. var module_info = wn.modules[module];
  159. if(!module_info) {
  160. module_info = {
  161. icon: "icon-question-sign",
  162. color: "#ddd"
  163. }
  164. }
  165. var icon = wn.boot.doctype_icons[doctype] || module_info.icon;
  166. this.$title_area.find(".title-icon").html('<i class="'+icon+'"></i> ')
  167. .toggle(true)
  168. .attr("doctype-name", doctype)
  169. .attr("module-link", module_info.link)
  170. .click(onclick || function() {
  171. var route = wn.get_route();
  172. var doctype = $(this).attr("doctype-name");
  173. if(doctype && route[0]!=="List" && !locals["DocType"][doctype].issingle) {
  174. wn.set_route("List", doctype)
  175. } else if($(this).attr("module-link")!==route[0]){
  176. wn.set_route($(this).attr("module-link"));
  177. } else {
  178. wn.set_route("");
  179. }
  180. return false;
  181. });
  182. if(sub_title) {
  183. this.set_title_left('<i class="icon-angle-left"></i> ' + module,
  184. function() { wn.set_route($(this).attr("module-link")); }).attr("module-link", module_info.link)
  185. }
  186. },
  187. add_help_button: function(txt) {
  188. this.add_icon_btn("2", "icon-question-sign", wn._("Help"),
  189. function() { msgprint($(this).data('help-text'), 'Help'); })
  190. .data("help-text", txt);
  191. },
  192. add_icon_btn: function(group, icon, label, click) {
  193. return this.iconbar.add_btn(group, icon, label, click);
  194. },
  195. add_button: function(label, click, icon, is_title) {
  196. return this.iconbar.add_btn("1", icon, wn._(label), click);
  197. },
  198. add_dropdown_button: function(parent, label, click, icon) {
  199. wn.ui.toolbar.add_dropdown_button(parent, label, click, icon);
  200. },
  201. // appframe::form
  202. add_label: function(label) {
  203. this.show_form();
  204. return $("<label class='col-md-1'>"+label+" </label>")
  205. .appendTo(this.parent.find(".appframe-form .container"));
  206. },
  207. add_select: function(label, options) {
  208. var field = this.add_field({label:label, fieldtype:"Select"})
  209. return field.$wrapper.find("select").empty().add_options(options);
  210. },
  211. add_data: function(label) {
  212. var field = this.add_field({label: label, fieldtype: "Data"});
  213. return field.$wrapper.find("input").attr("placeholder", label);
  214. },
  215. add_date: function(label, date) {
  216. var field = this.add_field({label: label, fieldtype: "Date", "default": date});
  217. return field.$wrapper.find("input").attr("placeholder", label);
  218. },
  219. add_check: function(label) {
  220. return $("<div class='checkbox' style='margin-right: 10px; margin-top: 7px; float: left;'><label><input type='checkbox'>" + label + "</label></div>")
  221. .appendTo(this.parent.find(".appframe-form .container"))
  222. .find("input");
  223. },
  224. add_break: function() {
  225. // add further fields in the next line
  226. this.parent.find(".appframe-form .container")
  227. .append('<div class="clearfix invisible-xs"></div>');
  228. },
  229. add_field: function(df) {
  230. this.show_form();
  231. var f = wn.ui.form.make_control({
  232. df: df,
  233. parent: this.parent.find(".appframe-form .container"),
  234. only_input: df.fieldtype=="Check" ? false : true,
  235. })
  236. f.refresh();
  237. $(f.wrapper)
  238. .addClass('col-md-2')
  239. .css({
  240. "padding-left": "0px",
  241. "padding-right": "0px",
  242. "margin-right": "5px",
  243. })
  244. .attr("title", wn._(df.label)).tooltip();
  245. f.$input.attr("placeholder", wn._(df.label));
  246. if(df.fieldtype==="Check") {
  247. $(f.wrapper).find(":first-child")
  248. .removeClass("col-md-offset-4 col-md-8");
  249. }
  250. if(df["default"])
  251. f.set_input(df["default"])
  252. this.fields_dict[df.fieldname || df.label] = f;
  253. return f;
  254. },
  255. show_form: function() {
  256. this.parent.find(".appframe-form").removeClass("hide");
  257. },
  258. });
  259. // parent, title, single_column
  260. // standard page with appframe
  261. wn.ui.make_app_page = function(opts) {
  262. /* help: make a standard page layout with a toolbar and title */
  263. /* options: [
  264. "parent: [HTMLElement] parent element",
  265. "single_column: [Boolean] false/true",
  266. "title: [optional] set this title"
  267. ]
  268. */
  269. $wrapper = $(opts.parent)
  270. $('<div class="appframe-titlebar">\
  271. <div class="container">\
  272. <div class="row">\
  273. <div class="titlebar-item text-left col-xs-3"></div>\
  274. <div class="titlebar-item titlebar-center-item text-center col-xs-6"></div>\
  275. <div class="titlebar-item text-right col-xs-3"></div>\
  276. </div>\
  277. </div>\
  278. </div>\
  279. <div class="appframe-iconbar hide">\
  280. <div class="container">\
  281. </div>\
  282. </div>\
  283. <div class="appframe-form hide">\
  284. <div class="container">\
  285. </div>\
  286. </div>\
  287. <div class="appframe container">\
  288. <div class="appframe-timestamp hide"></div>\
  289. <div class="workflow-button-area btn-group pull-right hide"></div>\
  290. </div>\
  291. <div class="appframe-footer hide"></div>').appendTo($wrapper);
  292. if(opts.single_column) {
  293. $('<div class="layout-main"></div>').appendTo($wrapper.find(".appframe"));
  294. } else {
  295. $('<div class="row">\
  296. <div class="layout-main-section col-sm-9"></div>\
  297. <div class="layout-side-section col-sm-3"></div>\
  298. </div>').appendTo($wrapper.find(".appframe"));
  299. }
  300. opts.parent.appframe = new wn.ui.AppFrame($wrapper);
  301. if(opts.set_document_title!==undefined)
  302. opts.parent.appframe.set_document_title = opts.set_document_title;
  303. if(opts.title) opts.parent.appframe.set_title(opts.title);
  304. if(opts.icon) opts.parent.appframe.add_module_icon(null, opts.icon);
  305. }