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.
 
 
 
 
 
 

122 rivejä
3.6 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // for license information please see license.txt
  4. wn.provide("wn.form.formatters");
  5. wn.form.formatters = {
  6. Data: function(value) {
  7. return value==null ? "" : value
  8. },
  9. Float: function(value, docfield) {
  10. var decimals = cint(docfield.options, null) || cint(wn.boot.sysdefaults.float_precision, null);
  11. return "<div style='text-align: right'>" +
  12. ((value==null || value==="") ? "" :
  13. format_number(value, null, decimals)) + "</div>";
  14. },
  15. Int: function(value) {
  16. return value==null ? "": "<div style='text-align: right'>" + cint(value) + "</div>";
  17. },
  18. Percent: function(value) {
  19. return "<div style='text-align: right'>" + flt(value, 2) + "%" + "</div>";
  20. },
  21. Currency: function(value, docfield, options, doc) {
  22. var currency = wn.meta.get_field_currency(docfield, doc);
  23. return "<div style='text-align: right'>" + format_currency(value, currency) + "</div>";
  24. },
  25. Check: function(value) {
  26. return value ? "<i class='icon-check'></i>" : "<i class='icon-check-empty'></i>";
  27. },
  28. Link: function(value, docfield, options) {
  29. if(options && options.for_print)
  30. return value;
  31. if(!value)
  32. return "";
  33. if(docfield && docfield.options) {
  34. return repl('%(icon)s<a href="#Form/%(doctype)s/%(name)s">%(label)s</a>', {
  35. doctype: encodeURIComponent(docfield.options),
  36. name: encodeURIComponent(value),
  37. label: value,
  38. icon: (options && options.no_icon) ? "" :
  39. ('<i class="icon-fixed-width '+wn.boot.doctype_icons[docfield.options]+'"></i> ')
  40. });
  41. } else {
  42. return value;
  43. }
  44. },
  45. Date: function(value) {
  46. return value ? dateutil.str_to_user(value) : "";
  47. },
  48. Text: function(value) {
  49. if(value) {
  50. var tags = ["<p[^>]>", "<div[^>]>", "<br[^>]>"];
  51. var match = false;
  52. for(var i=0; i<tags.length; i++) {
  53. if(value.match(tags[i])) {
  54. match = true;
  55. }
  56. }
  57. if(!match) {
  58. return replace_newlines(value);
  59. }
  60. }
  61. return wn.form.formatters.Data(value);
  62. },
  63. Tag: function(value) {
  64. var html = "";
  65. $.each((value || "").split(","), function(i, v) {
  66. if(v) html+= '<span class="label label-info" \
  67. style="margin-right: 7px; cursor: pointer;"\
  68. data-field="_user_tags" data-label="'+v+'">'+v +'</span>';
  69. });
  70. return html;
  71. },
  72. Comment: function(value) {
  73. var html = "";
  74. $.each(JSON.parse(value || "[]"), function(i, v) {
  75. if(v) html+= '<span class="label label-warning" \
  76. style="margin-right: 7px;"\
  77. data-field="_comments" data-label="'+v.name+'">'+v.comment+'</span>';
  78. });
  79. return html;
  80. },
  81. SmallText: function(value) {
  82. return wn.form.formatters.Text(value);
  83. },
  84. TextEditor: function(value) {
  85. return wn.form.formatters.Text(wn.utils.remove_script_and_style(value));
  86. },
  87. Code: function(value) {
  88. return "<pre>" + (value==null ? "" : $("<div>").text(value).html()) + "</pre>"
  89. },
  90. WorkflowState: function(value) {
  91. workflow_state = wn.model.get("Workflow State", value)[0];
  92. if(workflow_state) {
  93. return repl("<span class='label label-%(style)s' \
  94. data-workflow-state='%(value)s'\
  95. style='padding-bottom: 4px; cursor: pointer;'>\
  96. <i class='icon-small icon-white icon-%(icon)s'></i> %(value)s</span>", {
  97. value: value,
  98. style: workflow_state.style.toLowerCase(),
  99. icon: workflow_state.icon
  100. });
  101. } else {
  102. return "<span class='label'>" + value + "</span>";
  103. }
  104. }
  105. }
  106. wn.form.get_formatter = function(fieldtype) {
  107. if(!fieldtype) fieldtype = "Data";
  108. return wn.form.formatters[fieldtype.replace(/ /g, "")] || wn.form.formatters.Data;
  109. }
  110. wn.format = function(value, df, options, doc) {
  111. if(!df) df = {"fieldtype":"Data"};
  112. return wn.form.get_formatter(df.fieldtype)(value, df, options, doc);
  113. }