<commit_before>package org.swellrt.api.js.generic;

import com.google.gwt.core.client.JavaScriptObject;

import org.swellrt.api.js.WaveClientJS;
import org.swellrt.model.generic.Model;
import org.waveprotocol.wave.model.wave.ParticipantId;

/**
 * A JavaScript Wrapper for the CommunityWavelet class
 *
 * @author <EMAIL>
 *
 */
public class ModelJS extends JavaScriptObject implements Model.Listener {


  //private final static AdapterJS itemAdapterJS = new ItemAdapterJS();


  public native static ModelJS create(Model delegate) /*-{

    var jso = {

     callbackMap: new Object(),

     eventHandlers: new Object(),

     registerEventHandler: function(event, handler) {
      this.eventHandlers[event] = handler;
     },

     unregisterEventHandler: function(event, handler) {
      this.eventHandlers[event] = null;
     },

     id: function() {
       return delegate.@org.swellrt.model.generic.Model::getId()();
     },


     getParticipants: function() {
      var _participants = delegate.@org.swellrt.model.generic.Model::getParticipants()();
      return @org.swellrt.api.SwellRTUtils::participantIterableToJs(Ljava/lang/Iterable;)(_participants);
     },

     addParticipant: function(address) {
      return delegate.@org.swellrt.model.generic.Model::addParticipant(Ljava/lang/String;)(address);
     },

     removeParticipant: function(address) {
      return delegate.@org.swellrt.model.generic.Model::removeParticipant(Ljava/lang/String;)(address);
     },

     createMap: function() {
       var _map = delegate.@org.swellrt.model.generic.Model::createMap()();
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_map);
     },

     createString: function(strValue) {
       var _str = delegate.@org.swellrt.model.generic.Model::createString(Ljava/lang/String;)(strValue);
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_str);
     },

     createList: function() {
       var _list = delegate.@org.swellrt.model.generic.Model::createList()();
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_list);
     },

     createText: function(textOrXml) {
       var _txt = delegate.@org.swellrt.model.generic.Model::createText(Ljava/lang/String;)(textOrXml);
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_txt);
     },

     createFile: function(file, onComplete, progressListener) {


        if (!file instanceof File) {
          throw "INVALID_ARGUMENT";
        }

        if (typeof onComplete !== "function") {
          throw "INVALID_ARGUMENT";
        }

        if (progressListener && typeof progressListener !== "function") {
          throw "INVALID_ARGUMENT";
        }


        var attachmentId = delegate.@org.swellrt.model.generic.Model::generateAttachmentId()();
        var id = attachmentId.@org.waveprotocol.wave.media.model.AttachmentId::getId()();

        var waveRef = @org.swellrt.api.SwellRTUtils::encodeWaveRefUri(Lorg/swellrt/model/generic/Model;)(delegate);
        var url = @org.swellrt.api.SwellRTUtils::buildAttachmentUploadUrl(Ljava/lang/String;)(id);

        var formData = new FormData();

        formData.append("attachmentId", id);
        formData.append("waveRef", waveRef);
        formData.append("uploadFormElement", file, file.name);

        var request = new XMLHttpRequest();

        request.onload = function(event) {
          if (request.status == 201) {
            var _file = delegate.@org.swellrt.model.generic.Model::createFile(Lorg/waveprotocol/wave/media/model/AttachmentId;Ljava/lang/String;)(attachmentId, file.type);
            onComplete(@org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_file));
          } else {
            onComplete({ error : "SERVICE_EXCEPTION" });
          }
        };

        if (progressListener) {
          request.addEventListener("progress", progressListener);
        }

        request.open("POST", url);
        request.setRequestHeader("Accept", "text/plain");
        @org.swellrt.api.SwellRTUtils::addCommonRequestHeaders(Lcom/google/gwt/core/client/JavaScriptObject;)(request);
        request.send(formData);

        return true;
     },

     // For debug purpose only
     getModelDocuments: function() {
       var _docs = delegate.@org.swellrt.model.generic.Model::getModelDocuments()();
       return @org.swellrt.api.SwellRTUtils::stringIterableToJs(Ljava/lang/Iterable;)(_docs);
     },

     getModelDocument: function(docId) {
       return delegate.@org.swellrt.model.generic.Model::getModelDocument(Ljava/lang/String;)(docId);
     }

    }; // jso

    // Initialize the JS root map
    jso.root = @org.swellrt.api.js.generic.ModelJS::createRootJs(Lorg/swellrt/model/generic/Model;)(delegate);
<commit_msg>A method to debug object events<commit_after>package org.swellrt.api.js.generic;

import com.google.gwt.core.client.JavaScriptObject;

import org.swellrt.api.js.WaveClientJS;
import org.swellrt.model.generic.Model;
import org.waveprotocol.wave.model.wave.ParticipantId;

/**
 * A JavaScript Wrapper for the CommunityWavelet class
 *
 * @author <EMAIL>
 *
 */
public class ModelJS extends JavaScriptObject implements Model.Listener {


  //private final static AdapterJS itemAdapterJS = new ItemAdapterJS();


  public native static ModelJS create(Model delegate) /*-{

    var jso = {

     callbackMap: new Object(),

     eventHandlers: new Object(),

     registerEventHandler: function(event, handler) {
      this.eventHandlers[event] = handler;
     },

     unregisterEventHandler: function(event, handler) {
      this.eventHandlers[event] = null;
     },

     id: function() {
       return delegate.@org.swellrt.model.generic.Model::getId()();
     },


     getParticipants: function() {
      var _participants = delegate.@org.swellrt.model.generic.Model::getParticipants()();
      return @org.swellrt.api.SwellRTUtils::participantIterableToJs(Ljava/lang/Iterable;)(_participants);
     },

     addParticipant: function(address) {
      return delegate.@org.swellrt.model.generic.Model::addParticipant(Ljava/lang/String;)(address);
     },

     removeParticipant: function(address) {
      return delegate.@org.swellrt.model.generic.Model::removeParticipant(Ljava/lang/String;)(address);
     },

     createMap: function() {
       var _map = delegate.@org.swellrt.model.generic.Model::createMap()();
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_map);
     },

     createString: function(strValue) {
       var _str = delegate.@org.swellrt.model.generic.Model::createString(Ljava/lang/String;)(strValue);
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_str);
     },

     createList: function() {
       var _list = delegate.@org.swellrt.model.generic.Model::createList()();
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_list);
     },

     createText: function(textOrXml) {
       var _txt = delegate.@org.swellrt.model.generic.Model::createText(Ljava/lang/String;)(textOrXml);
       return @org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_txt);
     },

     createFile: function(file, onComplete, progressListener) {


        if (!file instanceof File) {
          throw "INVALID_ARGUMENT";
        }

        if (typeof onComplete !== "function") {
          throw "INVALID_ARGUMENT";
        }

        if (progressListener && typeof progressListener !== "function") {
          throw "INVALID_ARGUMENT";
        }


        var attachmentId = delegate.@org.swellrt.model.generic.Model::generateAttachmentId()();
        var id = attachmentId.@org.waveprotocol.wave.media.model.AttachmentId::getId()();

        var waveRef = @org.swellrt.api.SwellRTUtils::encodeWaveRefUri(Lorg/swellrt/model/generic/Model;)(delegate);
        var url = @org.swellrt.api.SwellRTUtils::buildAttachmentUploadUrl(Ljava/lang/String;)(id);

        var formData = new FormData();

        formData.append("attachmentId", id);
        formData.append("waveRef", waveRef);
        formData.append("uploadFormElement", file, file.name);

        var request = new XMLHttpRequest();

        request.onload = function(event) {
          if (request.status == 201) {
            var _file = delegate.@org.swellrt.model.generic.Model::createFile(Lorg/waveprotocol/wave/media/model/AttachmentId;Ljava/lang/String;)(attachmentId, file.type);
            onComplete(@org.swellrt.api.js.generic.AdapterTypeJS::adapt(Lorg/swellrt/model/generic/Type;)(_file));
          } else {
            onComplete({ error : "SERVICE_EXCEPTION" });
          }
        };

        if (progressListener) {
          request.addEventListener("progress", progressListener);
        }

        request.open("POST", url);
        request.setRequestHeader("Accept", "text/plain");
        @org.swellrt.api.SwellRTUtils::addCommonRequestHeaders(Lcom/google/gwt/core/client/JavaScriptObject;)(request);
        request.send(formData);

        return true;
     },

     // For debug purpose only
     getDocuments: function() {
       var _docs = delegate.@org.swellrt.model.generic.Model::getModelDocuments()();
       return @org.swellrt.api.SwellRTUtils::stringIterableToJs(Ljava/lang/Iterable;)(_docs);
     },

     getDocument: function(docId) {
       return delegate.@org.swellrt.model.generic.Model::getModelDocument(Ljava/lang/String;)(docId);
     },
     
     debugDocumentEvents: function(docId, enable) {
       return delegate.@org.swellrt.model.generic.Model::debugDocumentEvents(Ljava/lang/String;Z)(docId, enable);
     }

    }; // jso

    // Initialize the JS root map
    jso.root = @org.swellrt.api.js.generic.ModelJS::createRootJs(Lorg/swellrt/model/generic/Model;)(delegate);

{'fraction_non_alphanumeric': 0.1393525035190026, 'fraction_numerical': 0.0006032575909913533, 'mean_word_length': 3.332317073170732, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 27, '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': '254613', 'n_tokens_mistral': 3139, 'n_tokens_neox': 3024, 'n_words': 417}