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.
 
 
 
 
 
 

117 lines
3.4 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. $.extend(wn.model, {
  23. sync: function(doclist, sync_in) {
  24. if(!sync_in)
  25. sync_in = locals;
  26. if(doclist._kl)
  27. doclist = wn.model.expand(doclist);
  28. if(doclist && sync_in==locals)
  29. wn.model.clear_doclist(doclist[0].doctype, doclist[0].name)
  30. $.each(doclist, function(i, d) {
  31. if(!d.name) // get name (local if required)
  32. d.name = wn.model.get_new_name(d.doctype);
  33. if(!sync_in[d.doctype])
  34. sync_in[d.doctype] = {};
  35. sync_in[d.doctype][d.name] = d;
  36. d.__last_sync_on = new Date();
  37. if(cur_frm && cur_frm.doctype==d.doctype && cur_frm.docname==d.name) {
  38. cur_frm.doc = d;
  39. }
  40. if(d.doctype=='DocField') wn.meta.add_field(d);
  41. if(d.doctype=='DocType') wn.meta.sync_messages(d);
  42. if(d.localname) {
  43. wn.model.new_names[d.localname] = d.name;
  44. $(document).trigger('rename', [d.doctype, d.localname, d.name]);
  45. delete sync_in[d.doctype][d.localname];
  46. }
  47. });
  48. },
  49. expand: function(data) {
  50. function zip(k,v) {
  51. var obj = {};
  52. for(var i=0;i<k.length;i++) {
  53. obj[k[i]] = v[i];
  54. }
  55. return obj;
  56. }
  57. var l = [];
  58. for(var i=0;i<data._vl.length;i++) {
  59. l.push(zip(data._kl[data._vl[i][0]], data._vl[i]));
  60. }
  61. return l;
  62. },
  63. compress: function(doclist) {
  64. var keys = {}; var values = [];
  65. function get_key_list(doctype) {
  66. // valid standard keys
  67. var key_list = ['doctype', 'name', 'docstatus', 'owner', 'parent',
  68. 'parentfield', 'parenttype', 'idx', 'creation', 'modified',
  69. 'modified_by', '__islocal', '__newname', '__modified',
  70. '_user_tags', '__temp'];
  71. for(key in wn.meta.docfield_map[doctype]) { // all other values
  72. if(!in_list(key_list, key)
  73. && !in_list(no_value_fields, wn.meta.docfield_map[doctype][key].fieldtype)
  74. && !wn.meta.docfield_map[doctype][key].no_column) {
  75. key_list[key_list.length] = key
  76. }
  77. }
  78. return key_list;
  79. }
  80. for(var i=0; i<doclist.length;i++) {
  81. var doc = doclist[i];
  82. // make keys
  83. if(!keys[doc.doctype]) {
  84. keys[doc.doctype] = get_key_list(doc.doctype);
  85. // doctype must be first
  86. }
  87. var row = []
  88. var key_list = keys[doc.doctype];
  89. // make data rows
  90. for(var j=0;j<key_list.length;j++) {
  91. row.push(doc[key_list[j]]);
  92. }
  93. values.push(row);
  94. }
  95. return JSON.stringify({'_vl':values, '_kl':keys});
  96. }
  97. });