您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

formatters.js 2.7 KiB

12 年前
12 年前
12 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = wn.boot.sysdefaults.float_precision ?
  9. parseInt(wn.boot.sysdefaults.float_precision) : null;
  10. return "<div style='text-align: right'>" +
  11. format_number(flt(value, decimals), null, decimals) + "</div>";
  12. },
  13. Int: function(value) {
  14. return cint(value);
  15. },
  16. Percent: function(value) {
  17. return cint(value) + "%";
  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('<a href="#Form/%(doctype)s/%(name)s">%(name)s</a>', {
  33. doctype: docfield.options,
  34. name: value
  35. });
  36. } else {
  37. return value;
  38. }
  39. },
  40. Date: function(value) {
  41. return dateutil.str_to_user(value);
  42. },
  43. Text: function(value) {
  44. if(value) {
  45. var tags = ["<p[^>]>", "<div[^>]>", "<br[^>]>"];
  46. var match = false;
  47. for(var i=0; i<tags.length; i++) {
  48. if(value.match(tags[i])) {
  49. match = true;
  50. }
  51. }
  52. if(!match) {
  53. return replace_newlines(value);
  54. }
  55. }
  56. return wn.form.formatters.Data(value);
  57. },
  58. Tag: function(value) {
  59. var html = "";
  60. $.each((value || "").split(","), function(i, v) {
  61. if(v) html+= '<span class="label label-info" \
  62. style="margin-right: 7px; cursor: pointer;"\
  63. data-field="_user_tags" data-label="'+v+'">'+v +'</span>';
  64. });
  65. return html;
  66. },
  67. SmallText: function(value) {
  68. return wn.form.formatters.Text(value);
  69. },
  70. WorkflowState: function(value) {
  71. workflow_state = wn.model.get("Workflow State", value)[0];
  72. if(workflow_state) {
  73. return repl("<span class='label label-%(style)s' \
  74. data-workflow-state='%(value)s'\
  75. style='padding-bottom: 4px; cursor: pointer;'>\
  76. <i class='icon-small icon-white icon-%(icon)s'></i> %(value)s</span>", {
  77. value: value,
  78. style: workflow_state.style.toLowerCase(),
  79. icon: workflow_state.icon
  80. });
  81. } else {
  82. return "<span class='label'>" + value + "</span>";
  83. }
  84. }
  85. }
  86. wn.form.get_formatter = function(fieldtype) {
  87. if(!fieldtype) fieldtype = "Data";
  88. return wn.form.formatters[fieldtype.replace(/ /g, "")] || wn.form.formatters.Data;
  89. }
  90. wn.format = function(value, df, options, doc) {
  91. if(!df) df = {"fieldtype":"Data"};
  92. return wn.form.get_formatter(df.fieldtype)(value, df, options, doc);
  93. }