<commit_before>// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
// MIT License. See license.txt

$.extend(wn.model, {
	docinfo: {},
	sync: function(r) {
		/* docs:
			extract doclist, docinfo (attachments, comments, assignments)
			from incoming request and set in `locals` and `wn.model.docinfo`
		*/
		var doclist = r.docs ? r.docs : r;

		if(doclist._kl)
			doclist = wn.model.expand(doclist);

		if(doclist && doclist.length)
			wn.model.clear_doclist(doclist[0].doctype, doclist[0].name)

		var last_parent_name = null;
		$.each(doclist, function(i, d) {
			if(!d.name && d.__islocal) { // get name (local if required)
				d.name = wn.model.get_new_name(d.doctype);
				wn.provide("wn.model.docinfo." + d.doctype + "." + d.name);	
				if(!d.parenttype)
					last_parent_name = d.name;
			}

			// set parent for subsequent orphans
			if(d.parenttype && !d.parent && d.__islocal) {
				d.parent = last_parent_name;
			}

			if(!locals[d.doctype])
				locals[d.doctype] = {};

			locals[d.doctype][d.name] = d;
			d.__last_sync_on = new Date();

			if(cur_frm && cur_frm.doctype==d.doctype && cur_frm.docname==d.name) {
				cur_frm.doc = d;
			}

			if(d.doctype=='DocField') wn.meta.add_field(d);
			if(d.doctype=='DocType') wn.meta.sync_messages(d);

			if(d.localname) {
				wn.model.new_names[d.localname] = d.name;
				$(document).trigger('rename', [d.doctype, d.localname, d.name]);
				delete locals[d.doctype][d.localname];
				
				// update docinfo to new dict keys
				if(i===0) {
					wn.model.docinfo[d.doctype][d.name] = wn.model.docinfo[d.doctype][d.localname];
					wn.model.docinfo[d.doctype][d.localname] = undefined;
				}
			}
		});
		
		// set docinfo
		if(r.docinfo) {
			var doc = doclist[0]
			if(!wn.model.docinfo[doc.doctype])
				wn.model.docinfo[doc.doctype] = {};
			wn.model.docinfo[doc.doctype][doc.name] = r.docinfo;
		}
		
		return doclist;
	},
	
	expand: function(data) {
		function zip(k,v) {
			var obj = {};
			for(var i=0;i<k.length;i++) {
				obj[k[i]] = v[i];
			}
			return obj;
		}

		var l = [];
		for(var i=0;i<data._vl.length;i++) {
			l.push(zip(data._kl[data._vl[i][0]], data._vl[i]));<commit_msg>[fix] [minor] call dirty when synced with local doclist<commit_after>// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
// MIT License. See license.txt

$.extend(wn.model, {
	docinfo: {},
	sync: function(r) {
		/* docs:
			extract doclist, docinfo (attachments, comments, assignments)
			from incoming request and set in `locals` and `wn.model.docinfo`
		*/
		var doclist = r.docs ? r.docs : r;

		if(doclist._kl)
			doclist = wn.model.expand(doclist);

		if(doclist && doclist.length)
			wn.model.clear_doclist(doclist[0].doctype, doclist[0].name)

		var last_parent_name = null;
		var dirty = [];
		$.each(doclist, function(i, d) {
			if(!d.name && d.__islocal) { // get name (local if required)
				d.name = wn.model.get_new_name(d.doctype);
				wn.provide("wn.model.docinfo." + d.doctype + "." + d.name);	
				if(!d.parenttype)
					last_parent_name = d.name;
					
				if(!dirty.indexOf(d.parenttype || d.doctype)) dirty.push(d.parenttype || d.doctype);
			}

			// set parent for subsequent orphans
			if(d.parenttype && !d.parent && d.__islocal) {
				d.parent = last_parent_name;
			}

			if(!locals[d.doctype])
				locals[d.doctype] = {};

			locals[d.doctype][d.name] = d;
			d.__last_sync_on = new Date();

			if(cur_frm && cur_frm.doctype==d.doctype && cur_frm.docname==d.name) {
				cur_frm.doc = d;
			}

			if(d.doctype=='DocField') wn.meta.add_field(d);
			if(d.doctype=='DocType') wn.meta.sync_messages(d);

			if(d.localname) {
				wn.model.new_names[d.localname] = d.name;
				$(document).trigger('rename', [d.doctype, d.localname, d.name]);
				delete locals[d.doctype][d.localname];
				
				// update docinfo to new dict keys
				if(i===0) {
					wn.model.docinfo[d.doctype][d.name] = wn.model.docinfo[d.doctype][d.localname];
					wn.model.docinfo[d.doctype][d.localname] = undefined;
				}
			}
		});
		
		// set docinfo
		if(r.docinfo) {
			var doc = doclist[0]
			if(!wn.model.docinfo[doc.doctype])
				wn.model.docinfo[doc.doctype] = {};
			wn.model.docinfo[doc.doctype][doc.name] = r.docinfo;
		}
		
		if(cur_frm && dirty.indexOf(cur_frm.doctype)) cur_frm.dirty();
		
		return doclist;
	},
	
	expand: function(data) {
		function zip(k,v) {
			var obj = {};
			for(var i=0;i<k.length;i++) {
				obj[k[i]] = v[i];
			}
			return obj;
		}

		var l = [];
		for(var i=0;i<data._vl.length;i++) {
			l.push(zip(data._kl[data._vl[i][0]], data._vl[i]));
{'fraction_non_alphanumeric': 0.18494813506952107, 'fraction_numerical': 0.0048554403001544915, 'mean_word_length': 3.1087941976427924, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7360747', 'n_tokens_mistral': 2171, 'n_tokens_neox': 1810, 'n_words': 300}