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.

formatters.js 3.0 KiB

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