<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(["nbextensions/widgets/widgets/js/manager",
        "underscore",
        "backbone",
        "base/js/utils",
        "base/js/namespace",
], function(widgetmanager, _, Backbone, utils, IPython){
    "use strict";

    var unpack_models = function unpack_models(value, model) {
        /**
         * Replace model ids with models recursively.
         */
        var unpacked;
        if (_.isArray(value)) {
            unpacked = [];
            _.each(value, function(sub_value, key) {
                unpacked.push(unpack_models(sub_value, model));
            });
            return Promise.all(unpacked);
        } else if (value instanceof Object) {
            unpacked = {};
            _.each(value, function(sub_value, key) {
                unpacked[key] = unpack_models(sub_value, model);
            });
            return utils.resolve_promises_dict(unpacked);
        } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
            // get_model returns a promise already
            return model.widget_manager.get_model(value.slice(10, value.length));
        } else {
            return Promise.resolve(value);
        }
    };

    var WidgetModel = Backbone.Model.extend({
        constructor: function (widget_manager, model_id, comm) {
            /**
             * Constructor
             *
             * Creates a WidgetModel instance.
             *
             * Parameters
             * ----------
             * widget_manager : WidgetManager instance
             * model_id : string
             *      An ID unique to this model.
             * comm : Comm instance (optional)
             */
            WidgetModel.__super__.constructor.apply(this);
            this.widget_manager = widget_manager;
            this.state_change = Promise.resolve();
            this._buffered_state_diff = {};
            this.pending_msgs = 0;
            this.msg_buffer = null;
            this.state_lock = null;
            this.id = model_id;

            // Force backbone to think that the model has already been
            // synced with the server.  As of backbone 1.1, backbone
            // ignores `patch` if it thinks the model has never been
            // pushed.
            this.isNew = function() { return false; };

            this.views = {};
            this._resolve_received_state = {};

            if (comm !== undefined) {
                // Remember comm associated with the model.
                this.comm = comm;
                comm.model = this;

                // Hook comm messages up to model.
                comm.on_close(_.bind(this._handle_comm_closed, this));
                comm.on_msg(_.bind(this._handle_comm_msg, this));

                // Assume the comm is alive.
                this.set_comm_live(true);
            } else {
                this.set_comm_live(false);
            }

            // Listen for the events that lead to the websocket being terminated.
            var that = this;
            var died = function() {
                that.set_comm_live(false);
            };
            widget_manager.notebook.events.on('kernel_disconnected.Kernel', died);
            widget_manager.notebook.events.on('kernel_killed.Kernel', died);
            widget_manager.notebook.events.on('kernel_restarting.Kernel', died);
            widget_manager.notebook.events.on('kernel_dead.Kernel', died);

            return Backbone.Model.apply(this);
        },

        send: function (content, callbacks, buffers) {
            /**
             * Send a custom msg over the comm.
             */
            if (this.comm !== undefined) {
                var data = {method: 'custom', content: content};
                this.comm.send(data, callbacks, {}, buffers);
                this.pending_msgs++;
            }
        },

        request_state: function(callbacks) {
            /** 
             * Request a state push from the back-end.
             */
            if (!this.comm) {
                console.error("Could not request_state because comm doesn't exist!");
                return;
            }

            var msg_id = this.comm.send({method: 'request_state'}, callbacks || this.widget_manager.callbacks());

            // Promise that is resolved when a state is received
            // from the back-end.
            var that = this;
            var received_state = new Promise(function(resolve) {
                that._resolve_received_state[msg_id] = resolve;
            });
            return received_state;
        },

        set_comm_live: function(live) {
            /** 
             * Change the comm_live state of the model.
             */
            if (this.comm_live === undefined || this.comm_live != live) {
                this.comm_live = live;
                this.trigger(live ? 'comm:live' : 'comm:dead', {model: this});
            }
        },

        close: function(comm_closed) {
            /**
             * Close model
             */
            if (this.comm && !comm_closed) {
                this.comm.close();
            }
            this.stopListening();
            this.trigger('destroy', this);
            delete this.comm.model; // Delete ref so GC will collect widget model.
            delete this.comm;
            delete this.model_id; // Delete id from model so widget manager cleans up.
            _.each(this.views, function(v, id, views) {
                v.then(function(view) {
                    view.remove();
                    delete views[id];
                });
            });
        },

        _handle_comm_closed: function (msg) {
            /** 
             * Handle when a widget is closed.
             */
            this.trigger('comm:close');
            this.close(true);
        },
        _deserialize_state: function(state) {
            /** 
             * Deserialize fields that have a custom serializer.
             */
            var serializers = this.constructor.serializers;
            if (serializers) {
                var deserialized = {};
                for (var k in state) {
                    if (serializers[k] && serializers[k].deserialize) {
                         deserialized[k] = (serializers[k].deserialize)(state[k], this);
                    } else {
                         deserialized[k] = state[k];
                    }
                }
                return deserialized;
            } else {
                return state;
            }
        },
        _handle_comm_msg: function (msg) {
            /**
             * Handle incoming comm msg.
             */
            var method = msg.content.data.method;
            
            var that = this;
            switch (method) {
                case 'update':
                    this.state_change = this.state_change
                        .then(function() {
                            var state = msg.content.data.state || {};
                            var buffer_keys = msg.content.data.buffers || [];
                            var buffers = msg.buffers || [];
                            for (var i=0; i<buffer_keys.length; i++) {
                                state[buffer_keys[i]] = buffers[i];
                            }
                            state = that._deserialize_state(state); 
                            return utils.resolve_promises_dict(state);
                        }).then(function(state) {
                            return that.set_state(state);
                        }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true))
                        .then(function() {
                            var parent_id = msg.parent_header.msg_id;
                            if (that._resolve_received_state[parent_id] !== undefined) {
                                that._resolve_received_state[parent_id].call();
                                delete that._resolve_received_state[parent_id];
                            }
                        }).catch(utils.reject("Couldn't resolve state request promise", true));
                    break;
                case 'custom':
                    this.trigger('msg:custom', msg.content.data.content, msg.buffers);
                    break;
                case 'display':
                    this.state_change = this.state_change.then(function() {
                        that.widget_manager.display_view(msg, that);
                    }).catch(utils.reject('Could not process display view msg', true));
                    break;
            }
        },

        set_state: function (state) {
            var that = this;
            // Handle when a widget is updated via the python side.
            return new Promise(function(resolve, reject) {
                that.state_lock = state;
                try {
                    WidgetModel.__super__.set.call(that, state);
                } finally {
                    that.state_lock = null;
                }
                resolve();
            }).catch(utils.reject("Couldn't set model state", true));
        },

        get_state: function() {
            // Get the serializable state of the model.
            // Equivalent to Backbone.Model.toJSON()
            return _.clone(this.attributes);
        },
        
        _handle_status: function (msg, callbacks) {
            /**
             * Handle status msgs.
             *
             * execution_state : ('busy', 'idle', 'starting')
             */
            if (this.comm !== undefined) {
                if (msg.content.execution_state ==='idle') {
                    // Send buffer if this message caused another message to be
                    // throttled.
                    if (this.msg_buffer !== null &&
                        (this.get('msg_throttle') || 3) === this.pending_msgs) {
                        var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
                        this.comm.send(data, callbacks);
                        this.msg_buffer = null;
                    } else {
                        --this.pending_msgs;
                    }
                }
            }
        },

        callbacks: function(view) {
            /**
             * Create msg callbacks for a comm msg.
             */
            var callbacks = this.widget_manager.callbacks(view);

            if (callbacks.iopub === undefined) {
                callbacks.iopub = {};
            }

            var that = this;
            callbacks.iopub.status = function (msg) {
                that._handle_status(msg, callbacks);
            };
            return callbacks;
        },

        set: function(key, val, options) {
            /**
             * Set a value.
             */
            var return_value = WidgetModel.__super__.set.apply(this, arguments);

            // Backbone only remembers the diff of the most recent set()
            // operation.  Calling set multiple times in a row results in a
            // loss of diff information.  Here we keep our own running diff.
            this._buffered_state_diff = _.extend(this._buffered_state_diff, this.changedAttributes() || {});
            return return_value;
        },

        sync: function (method, model, options) {
            /**
             * Handle sync to the back-end.  Called when a model.save() is called.
             *
             * Make sure a comm exists.

             * Parameters
             * ----------
             * method : create, update, patch, delete, read
             *   create/update always send the full attribute set
             *   patch - only send attributes listed in options.attrs, and if we are queuing
             *     up messages, combine with previous messages that have not been sent yet
             * model : the model we are syncing
             *   will normally be the same as `this`
             * options : dict
             *   the `attrs` key, if it exists, gives an {attr: value} dict that should be synced,
             *   otherwise, sync all attributes
             * 
             */
            var error = options.error || function() {
                console.error('Backbone sync error:', arguments);
            };
            if (this.comm === undefined) {
                error();
                return false;
            }

            var attrs = (method === 'patch') ? options.attrs : model.get_state(options);

            // the state_lock lists attributes that are currently be changed right now from a kernel message
            // we don't want to send these non-changes back to the kernel, so we delete them out of attrs
            // (but we only delete them if the value hasn't changed from the value stored in the state_lock
            if (this.state_lock !== null) {
                var keys = Object.keys(this.state_lock);
                for (var i=0; i<keys.length; i++) {
                    var key = keys[i];
                    if (attrs[key] === this.state_lock[key]) {
                        delete attrs[key];
                    }
                }
            }
            
            if (_.size(attrs) > 0) {

                // If this message was sent via backbone itself, it will not
                // have any callbacks.  It's important that we create callbacks
                // so we can listen for status messages, etc...
                var callbacks = options.callbacks || this.callbacks();

                // Check throttle.
                if (this.pending_msgs >= (this.get('msg_throttle') || 3)) {
                    // The throttle has been exceeded, buffer the current msg so
                    // it can be sent once the kernel has finished processing
                    // some of the existing messages.
                    // Combine updates if it is a 'patch' sync, otherwise replace updates
                    switch (method) {
                        case 'patch':
                            this.msg_buffer = _.extend(this.msg_buffer || {}, attrs);
                            break;
                        case 'update':
                        case 'create':
                            this.msg_buffer = attrs;
                            break;
                        default:
                            error();
                            return false;
                    }
                    this.msg_buffer_callbacks = callbacks;

                } else {
                    // We haven't exceeded the throttle, send the message like
                    // normal.
                    this.send_sync_message(attrs, callbacks);
                    this.pending_msgs++;
                }
            }
            // Since the comm is a one-way communication, assume the message
            // arrived.  Don't call success since we don't have a model back from the server
            // this means we miss out on the 'sync' event.
            this._buffered_state_diff = {};
        },


        send_sync_message: function(attrs, callbacks) {
            // prepare and send a comm message syncing attrs
            var that = this;
            // first, build a state dictionary with key=the attribute and the value
            // being the value or the promise of the serialized value
            var serializers = this.constructor.serializers;
            if (serializers) {
                for (var k in attrs) {
                    if (serializers[k] && serializers[k].serialize) {
                        attrs[k] = (serializers[k].serialize)(attrs[k], this);
                    }
                }
            }
            utils.resolve_promises_dict(attrs).then(function(state) {
                // get binary values, then send
                var keys = Object.keys(state);
                var buffers = [];
                var buffer_keys = [];
                for (var i=0; i<keys.length; i++) {
                    var key = keys[i];
                    var value = state[key];
                    if (value) {
                        if (value.buffer instanceof ArrayBuffer
                            || value instanceof ArrayBuffer) {
                            buffers.push(value);
                            buffer_keys.push(key);
                            delete state[key];
                        }
                    }
                }
                that.comm.send({method: 'backbone', sync_data: state, buffer_keys: buffer_keys}, callbacks, {}, buffers);
            }).catch(function(error) {
                that.pending_msgs--;
                return (utils.reject("Couldn't send widget sync message", true))(error);
            });
        },

        save_changes: function(callbacks) {
            /**
             * Push this model's state to the back-end
             *
             * This invokes a Backbone.Sync.
             */
            this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
        },

        on_some_change: function(keys, callback, context) {
            /**
             * on_some_change(["key1", "key2"], foo, context) differs from
             * on("change:key1 change:key2", foo, context).
             * If the widget attributes key1 and key2 are both modified,
             * the second form will result in foo being called twice
             * while the first will call foo only once.
             */
            this.on('change', function() {
                if (keys.some(this.hasChanged, this)) {
                    callback.apply(context, arguments);
                }
            }, this);

        },

        toJSON: function(options) {
            /**
             * Serialize the model.  See the types.js deserialization function
             * and the kernel-side serializer/deserializer
             */
            return "IPY_MODEL_" + this.id;
        }
    });
    widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);


    var WidgetViewMixin = {
        initialize: function(parameters) {
            /**
             * Public constructor.
             */
            this.model.on('change',this.update,this);

            // Bubble the comm live events.
            this.model.on('comm:live', function() {
                this.trigger('comm:live', this);
            }, this);
            this.model.on('comm:dead', function() {
                this.trigger('comm:dead', this);
            }, this);

            this.options = parameters.options;
            this.on('displayed', function() { 
                this.is_displayed = true; 
            }, this);
        },

        update: function(){
            /**
             * Triggered on model change.
             *
             * Update view to be consistent with this.model
             */
        },

        create_child_view: function(child_model, options) {
            /**
             * Create and promise that resolves to a child view of a given model
             */
            var that = this;
            options = _.extend({ parent: this }, options || {});
            return this.model.widget_manager.create_view(child_model, options).catch(utils.reject("Couldn't create child view", true));
        },

        callbacks: function(){
            /**
             * Create msg callbacks for a comm msg.
             */
            return this.model.callbacks(this);
        },

        render: function(){
            /**
             * Render the view.
             *
             * By default, this is only called the first time the view is created
             */
        },

        send: function (content, buffers) {
            /**
             * Send a custom msg associated with this view.
             */
            this.model.send(content, this.callbacks(), buffers);
        },

        touch: function () {
            this.model.save_changes(this.callbacks());
        },

        after_displayed: function (callback, context) {
            /**
             * Calls the callback right away is the view is already displayed
             * otherwise, register the callback to the 'displayed' event.
             */
            if (this.is_displayed) {
                callback.apply(context);
            } else {
                this.on('displayed', callback, context);
            }
        },

        remove: function () {
            // Raise a remove event when the view is removed.
            WidgetView.__super__.remove.apply(this, arguments);
            this.trigger('remove');
        }
    };


    var DOMWidgetViewMixin = {
        initialize: function (parameters) {
            /**
             * Public constructor
             */
            WidgetViewMixin.initialize.apply(this, [parameters]);
            this.model.on('change:visible', this.update_visible, this);
            this.model.on('change:_css', this.update_css, this);

            this.model.on('change:_dom_classes', function(model, new_classes) {
                var old_classes = model.previous('_dom_classes');
                this.update_classes(old_classes, new_classes);
            }, this);

            this.model.on('change:color', function (model, value) { 
                this.update_attr('color', value); }, this);

            this.model.on('change:background_color', function (model, value) { 
                this.update_attr('background', value); }, this);

            this.model.on('change:width', function (model, value) { 
                this.update_attr('width', value); }, this);

            this.model.on('change:height', function (model, value) { 
                this.update_attr('height', value); }, this);

            this.model.on('change:border_color', function (model, value) { 
                this.update_attr('border-color', value); }, this);

            this.model.on('change:border_width', function (model, value) { 
                this.update_attr('border-width', value); }, this);

            this.model.on('change:border_style', function (model, value) { 
                this.update_attr('border-style', value); }, this);

            this.model.on('change:font_style', function (model, value) { 
                this.update_attr('font-style', value); }, this);

            this.model.on('change:font_weight', function (model, value) { 
                this.update_attr('font-weight', value); }, this);

            this.model.on('change:font_size', function (model, value) { 
                this.update_attr('font-size', this._default_px(value)); }, this);

            this.model.on('change:font_family', function (model, value) { 
                this.update_attr('font-family', value); }, this);

            this.model.on('change:padding', function (model, value) { 
                this.update_attr('padding', value); }, this);

            this.model.on('change:margin', function (model, value) { 
                this.update_attr('margin', this._default_px(value)); }, this);

            this.model.on('change:border_radius', function (model, value) { 
                this.update_attr('border-radius', this._default_px(value)); }, this);

            this.after_displayed(function() {
                this.update_visible(this.model, this.model.get("visible"));
                this.update_classes([], this.model.get('_dom_classes'));
                
                this.update_attr('color', this.model.get('color'));
                this.update_attr('background', this.model.get('background_color'));
                this.update_attr('width', this.model.get('width'));
                this.update_attr('height', this.model.get('height'));
                this.update_attr('border-color', this.model.get('border_color'));
                this.update_attr('border-width', this.model.get('border_width'));
                this.update_attr('border-style', this.model.get('border_style'));
                this.update_attr('font-style', this.model.get('font_style'));
                this.update_attr('font-weight', this.model.get('font_weight'));
                this.update_attr('font-size', this._default_px(this.model.get('font_size')));
                this.update_attr('font-family', this.model.get('font_family'));
                this.update_attr('padding', this.model.get('padding'));
                this.update_attr('margin', this._default_px(this.model.get('margin')));
                this.update_attr('border-radius', this._default_px(this.model.get('border_radius')));

                this.update_css(this.model, this.model.get("_css"));
            }, this);
        },

        _default_px: function(value) {
            /**
             * Makes browser interpret a numerical string as a pixel value.
             */
            if (value && /^\d+\.?(\d+)?$/.test(value.trim())) {
                return value.trim() + 'px';
            }
            return value;
        },

        update_attr: function(name, value) {
            /**
             * Set a css attr of the widget view.
             */
            this.el.style[name] = value;
        },

        update_visible: function(model, value) {
            /**
             * Update visibility
             */
            switch(value) {
                case null: // python None
                    this.el.style.display = '';
                    this.el.style.visibility = 'hidden'; break;
                case false:
                    this.el.style.display = 'none'; break;
                case true:
                    this.el.style.display = '';
                    this.el.style.visibility = ''; break;
            }
        },

        update_css: function (model, css) {
            /**
             * Update the css styling of this view.
             */
            if (css === undefined) {return;}
            for (var i = 0; i < css.length; i++) {
                // Apply the css traits to all elements that match the selector.
                var selector = css[i][0];
                var elements = this.el.querySelectorAll(selector) || [this.el];
                if (elements.length > 0) {
                    var trait_key = css[i][1];
                    var trait_value = css[i][2];
                    _.each(elements, function(e) {
                        e.style[trait_key] = trait_value;
                    });
                }
            }
        },

        update_classes: function (old_classes, new_classes, el) {
            /**
             * Update the DOM classes applied to an element, default to this.el.
             */
            if (el===undefined) {
                el = this.el;
            }
            _.difference(old_classes, new_classes).map(function(c) { el.classList.remove(c); });
            _.difference(new_classes, old_classes).map(function(c) { el.classList.add(c); });
        },

        update_mapped_classes: function(class_map, trait_name, previous_trait_value, el) {
            /**
             * Update the DOM classes applied to the widget based on a single<commit_msg>Save changes should only save if comm is live<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(["nbextensions/widgets/widgets/js/manager",
        "underscore",
        "backbone",
        "base/js/utils",
        "base/js/namespace",
], function(widgetmanager, _, Backbone, utils, IPython){
    "use strict";

    var unpack_models = function unpack_models(value, model) {
        /**
         * Replace model ids with models recursively.
         */
        var unpacked;
        if (_.isArray(value)) {
            unpacked = [];
            _.each(value, function(sub_value, key) {
                unpacked.push(unpack_models(sub_value, model));
            });
            return Promise.all(unpacked);
        } else if (value instanceof Object) {
            unpacked = {};
            _.each(value, function(sub_value, key) {
                unpacked[key] = unpack_models(sub_value, model);
            });
            return utils.resolve_promises_dict(unpacked);
        } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
            // get_model returns a promise already
            return model.widget_manager.get_model(value.slice(10, value.length));
        } else {
            return Promise.resolve(value);
        }
    };

    var WidgetModel = Backbone.Model.extend({
        constructor: function (widget_manager, model_id, comm) {
            /**
             * Constructor
             *
             * Creates a WidgetModel instance.
             *
             * Parameters
             * ----------
             * widget_manager : WidgetManager instance
             * model_id : string
             *      An ID unique to this model.
             * comm : Comm instance (optional)
             */
            WidgetModel.__super__.constructor.apply(this);
            this.widget_manager = widget_manager;
            this.state_change = Promise.resolve();
            this._buffered_state_diff = {};
            this.pending_msgs = 0;
            this.msg_buffer = null;
            this.state_lock = null;
            this.id = model_id;

            // Force backbone to think that the model has already been
            // synced with the server.  As of backbone 1.1, backbone
            // ignores `patch` if it thinks the model has never been
            // pushed.
            this.isNew = function() { return false; };

            this.views = {};
            this._resolve_received_state = {};

            if (comm !== undefined) {
                // Remember comm associated with the model.
                this.comm = comm;
                comm.model = this;

                // Hook comm messages up to model.
                comm.on_close(_.bind(this._handle_comm_closed, this));
                comm.on_msg(_.bind(this._handle_comm_msg, this));

                // Assume the comm is alive.
                this.set_comm_live(true);
            } else {
                this.set_comm_live(false);
            }

            // Listen for the events that lead to the websocket being terminated.
            var that = this;
            var died = function() {
                that.set_comm_live(false);
            };
            widget_manager.notebook.events.on('kernel_disconnected.Kernel', died);
            widget_manager.notebook.events.on('kernel_killed.Kernel', died);
            widget_manager.notebook.events.on('kernel_restarting.Kernel', died);
            widget_manager.notebook.events.on('kernel_dead.Kernel', died);

            return Backbone.Model.apply(this);
        },

        send: function (content, callbacks, buffers) {
            /**
             * Send a custom msg over the comm.
             */
            if (this.comm !== undefined) {
                var data = {method: 'custom', content: content};
                this.comm.send(data, callbacks, {}, buffers);
                this.pending_msgs++;
            }
        },

        request_state: function(callbacks) {
            /** 
             * Request a state push from the back-end.
             */
            if (!this.comm) {
                console.error("Could not request_state because comm doesn't exist!");
                return;
            }

            var msg_id = this.comm.send({
                method: 'request_state'
            }, callbacks || this.widget_manager.callbacks());

            // Promise that is resolved when a state is received
            // from the back-end.
            var that = this;
            var received_state = new Promise(function(resolve) {
                that._resolve_received_state[msg_id] = resolve;
            });
            return received_state;
        },

        set_comm_live: function(live) {
            /** 
             * Change the comm_live state of the model.
             */
            if (this.comm_live === undefined || this.comm_live != live) {
                this.comm_live = live;
                this.trigger(live ? 'comm:live' : 'comm:dead', {model: this});
            }
        },

        close: function(comm_closed) {
            /**
             * Close model
             */
            if (this.comm && !comm_closed) {
                this.comm.close();
            }
            this.stopListening();
            this.trigger('destroy', this);
            delete this.comm.model; // Delete ref so GC will collect widget model.
            delete this.comm;
            delete this.model_id; // Delete id from model so widget manager cleans up.
            _.each(this.views, function(v, id, views) {
                v.then(function(view) {
                    view.remove();
                    delete views[id];
                });
            });
        },

        _handle_comm_closed: function (msg) {
            /** 
             * Handle when a widget is closed.
             */
            this.trigger('comm:close');
            this.close(true);
        },
        _deserialize_state: function(state) {
            /** 
             * Deserialize fields that have a custom serializer.
             */
            var serializers = this.constructor.serializers;
            if (serializers) {
                var deserialized = {};
                for (var k in state) {
                    if (serializers[k] && serializers[k].deserialize) {
                         deserialized[k] = (serializers[k].deserialize)(state[k], this);
                    } else {
                         deserialized[k] = state[k];
                    }
                }
                return deserialized;
            } else {
                return state;
            }
        },
        _handle_comm_msg: function (msg) {
            /**
             * Handle incoming comm msg.
             */
            var method = msg.content.data.method;
            
            var that = this;
            switch (method) {
                case 'update':
                    this.state_change = this.state_change
                        .then(function() {
                            var state = msg.content.data.state || {};
                            var buffer_keys = msg.content.data.buffers || [];
                            var buffers = msg.buffers || [];
                            for (var i=0; i<buffer_keys.length; i++) {
                                state[buffer_keys[i]] = buffers[i];
                            }
                            state = that._deserialize_state(state); 
                            return utils.resolve_promises_dict(state);
                        }).then(function(state) {
                            return that.set_state(state);
                        }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true))
                        .then(function() {
                            var parent_id = msg.parent_header.msg_id;
                            if (that._resolve_received_state[parent_id] !== undefined) {
                                that._resolve_received_state[parent_id].call();
                                delete that._resolve_received_state[parent_id];
                            }
                        }).catch(utils.reject("Couldn't resolve state request promise", true));
                    break;
                case 'custom':
                    this.trigger('msg:custom', msg.content.data.content, msg.buffers);
                    break;
                case 'display':
                    this.state_change = this.state_change.then(function() {
                        that.widget_manager.display_view(msg, that);
                    }).catch(utils.reject('Could not process display view msg', true));
                    break;
            }
        },

        set_state: function (state) {
            var that = this;
            // Handle when a widget is updated via the python side.
            return new Promise(function(resolve, reject) {
                that.state_lock = state;
                try {
                    WidgetModel.__super__.set.call(that, state);
                } finally {
                    that.state_lock = null;
                }
                resolve();
            }).catch(utils.reject("Couldn't set model state", true));
        },

        get_state: function() {
            // Get the serializable state of the model.
            // Equivalent to Backbone.Model.toJSON()
            return _.clone(this.attributes);
        },
        
        _handle_status: function (msg, callbacks) {
            /**
             * Handle status msgs.
             *
             * execution_state : ('busy', 'idle', 'starting')
             */
            if (this.comm !== undefined) {
                if (msg.content.execution_state ==='idle') {
                    // Send buffer if this message caused another message to be
                    // throttled.
                    if (this.msg_buffer !== null &&
                        (this.get('msg_throttle') || 3) === this.pending_msgs) {
                        var data = {
                            method: 'backbone',
                            sync_method: 'update',
                            sync_data: this.msg_buffer
                        };
                        this.comm.send(data, callbacks);
                        this.msg_buffer = null;
                    } else {
                        --this.pending_msgs;
                    }
                }
            }
        },

        callbacks: function(view) {
            /**
             * Create msg callbacks for a comm msg.
             */
            var callbacks = this.widget_manager.callbacks(view);

            if (callbacks.iopub === undefined) {
                callbacks.iopub = {};
            }

            var that = this;
            callbacks.iopub.status = function (msg) {
                that._handle_status(msg, callbacks);
            };
            return callbacks;
        },

        set: function(key, val, options) {
            /**
             * Set a value.
             */
            var return_value = WidgetModel.__super__.set.apply(this, arguments);

            // Backbone only remembers the diff of the most recent set()
            // operation.  Calling set multiple times in a row results in a
            // loss of diff information.  Here we keep our own running diff.
            this._buffered_state_diff = _.extend(this._buffered_state_diff, this.changedAttributes() || {});
            return return_value;
        },

        sync: function (method, model, options) {
            /**
             * Handle sync to the back-end.  Called when a model.save() is called.
             *
             * Make sure a comm exists.

             * Parameters
             * ----------
             * method : create, update, patch, delete, read
             *   create/update always send the full attribute set
             *   patch - only send attributes listed in options.attrs, and if we are queuing
             *     up messages, combine with previous messages that have not been sent yet
             * model : the model we are syncing
             *   will normally be the same as `this`
             * options : dict
             *   the `attrs` key, if it exists, gives an {attr: value} dict that should be synced,
             *   otherwise, sync all attributes
             * 
             */
            var error = options.error || function() {
                console.error('Backbone sync error:', arguments);
            };
            if (this.comm === undefined) {
                error();
                return false;
            }

            var attrs = (method === 'patch') ? options.attrs : model.get_state(options);

            // the state_lock lists attributes that are currently be changed right now from a kernel message
            // we don't want to send these non-changes back to the kernel, so we delete them out of attrs
            // (but we only delete them if the value hasn't changed from the value stored in the state_lock
            if (this.state_lock !== null) {
                var keys = Object.keys(this.state_lock);
                for (var i=0; i<keys.length; i++) {
                    var key = keys[i];
                    if (attrs[key] === this.state_lock[key]) {
                        delete attrs[key];
                    }
                }
            }
            
            if (_.size(attrs) > 0) {

                // If this message was sent via backbone itself, it will not
                // have any callbacks.  It's important that we create callbacks
                // so we can listen for status messages, etc...
                var callbacks = options.callbacks || this.callbacks();

                // Check throttle.
                if (this.pending_msgs >= (this.get('msg_throttle') || 3)) {
                    // The throttle has been exceeded, buffer the current msg so
                    // it can be sent once the kernel has finished processing
                    // some of the existing messages.
                    // Combine updates if it is a 'patch' sync, otherwise replace updates
                    switch (method) {
                        case 'patch':
                            this.msg_buffer = _.extend(this.msg_buffer || {}, attrs);
                            break;
                        case 'update':
                        case 'create':
                            this.msg_buffer = attrs;
                            break;
                        default:
                            error();
                            return false;
                    }
                    this.msg_buffer_callbacks = callbacks;

                } else {
                    // We haven't exceeded the throttle, send the message like
                    // normal.
                    this.send_sync_message(attrs, callbacks);
                    this.pending_msgs++;
                }
            }
            // Since the comm is a one-way communication, assume the message
            // arrived.  Don't call success since we don't have a model back from the server
            // this means we miss out on the 'sync' event.
            this._buffered_state_diff = {};
        },


        send_sync_message: function(attrs, callbacks) {
            // prepare and send a comm message syncing attrs
            var that = this;
            // first, build a state dictionary with key=the attribute and the value
            // being the value or the promise of the serialized value
            var serializers = this.constructor.serializers;
            if (serializers) {
                for (var k in attrs) {
                    if (serializers[k] && serializers[k].serialize) {
                        attrs[k] = (serializers[k].serialize)(attrs[k], this);
                    }
                }
            }
            utils.resolve_promises_dict(attrs).then(function(state) {
                // get binary values, then send
                var keys = Object.keys(state);
                var buffers = [];
                var buffer_keys = [];
                for (var i=0; i<keys.length; i++) {
                    var key = keys[i];
                    var value = state[key];
                    if (value) {
                        if (value.buffer instanceof ArrayBuffer
                            || value instanceof ArrayBuffer) {
                            buffers.push(value);
                            buffer_keys.push(key);
                            delete state[key];
                        }
                    }
                }
                that.comm.send({
                    method: 'backbone',
                    sync_data: state,
                    buffer_keys: buffer_keys
                }, callbacks, {}, buffers);
            }).catch(function(error) {
                that.pending_msgs--;
                return (utils.reject("Couldn't send widget sync message", true))(error);
            });
        },

        save_changes: function(callbacks) {
            /**
             * Push this model's state to the back-end
             *
             * This invokes a Backbone.Sync.
             */
            if (this.comm_live) {
                this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
            }
        },

        on_some_change: function(keys, callback, context) {
            /**
             * on_some_change(["key1", "key2"], foo, context) differs from
             * on("change:key1 change:key2", foo, context).
             * If the widget attributes key1 and key2 are both modified,
             * the second form will result in foo being called twice
             * while the first will call foo only once.
             */
            this.on('change', function() {
                if (keys.some(this.hasChanged, this)) {
                    callback.apply(context, arguments);
                }
            }, this);

        },

        toJSON: function(options) {
            /**
             * Serialize the model.  See the types.js deserialization function
             * and the kernel-side serializer/deserializer
             */
            return "IPY_MODEL_" + this.id;
        }
    });
    widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);


    var WidgetViewMixin = {
        initialize: function(parameters) {
            /**
             * Public constructor.
             */
            this.model.on('change',this.update,this);

            // Bubble the comm live events.
            this.model.on('comm:live', function() {
                this.trigger('comm:live', this);
            }, this);
            this.model.on('comm:dead', function() {
                this.trigger('comm:dead', this);
            }, this);

            this.options = parameters.options;
            this.on('displayed', function() { 
                this.is_displayed = true; 
            }, this);
        },

        update: function(){
            /**
             * Triggered on model change.
             *
             * Update view to be consistent with this.model
             */
        },

        create_child_view: function(child_model, options) {
            /**
             * Create and promise that resolves to a child view of a given model
             */
            var that = this;
            options = _.extend({ parent: this }, options || {});
            return this.model.widget_manager.create_view(child_model, options).catch(utils.reject("Couldn't create child view", true));
        },

        callbacks: function(){
            /**
             * Create msg callbacks for a comm msg.
             */
            return this.model.callbacks(this);
        },

        render: function(){
            /**
             * Render the view.
             *
             * By default, this is only called the first time the view is created
             */
        },

        send: function (content, buffers) {
            /**
             * Send a custom msg associated with this view.
             */
            this.model.send(content, this.callbacks(), buffers);
        },

        touch: function () {
            this.model.save_changes(this.callbacks());
        },

        after_displayed: function (callback, context) {
            /**
             * Calls the callback right away is the view is already displayed
             * otherwise, register the callback to the 'displayed' event.
             */
            if (this.is_displayed) {
                callback.apply(context);
            } else {
                this.on('displayed', callback, context);
            }
        },

        remove: function () {
            // Raise a remove event when the view is removed.
            WidgetView.__super__.remove.apply(this, arguments);
            this.trigger('remove');
        }
    };


    var DOMWidgetViewMixin = {
        initialize: function (parameters) {
            /**
             * Public constructor
             */
            WidgetViewMixin.initialize.apply(this, [parameters]);
            this.model.on('change:visible', this.update_visible, this);
            this.model.on('change:_css', this.update_css, this);

            this.model.on('change:_dom_classes', function(model, new_classes) {
                var old_classes = model.previous('_dom_classes');
                this.update_classes(old_classes, new_classes);
            }, this);

            this.model.on('change:color', function (model, value) { 
                this.update_attr('color', value); }, this);

            this.model.on('change:background_color', function (model, value) { 
                this.update_attr('background', value); }, this);

            this.model.on('change:width', function (model, value) { 
                this.update_attr('width', value); }, this);

            this.model.on('change:height', function (model, value) { 
                this.update_attr('height', value); }, this);

            this.model.on('change:border_color', function (model, value) { 
                this.update_attr('border-color', value); }, this);

            this.model.on('change:border_width', function (model, value) { 
                this.update_attr('border-width', value); }, this);

            this.model.on('change:border_style', function (model, value) { 
                this.update_attr('border-style', value); }, this);

            this.model.on('change:font_style', function (model, value) { 
                this.update_attr('font-style', value); }, this);

            this.model.on('change:font_weight', function (model, value) { 
                this.update_attr('font-weight', value); }, this);

            this.model.on('change:font_size', function (model, value) { 
                this.update_attr('font-size', this._default_px(value)); }, this);

            this.model.on('change:font_family', function (model, value) { 
                this.update_attr('font-family', value); }, this);

            this.model.on('change:padding', function (model, value) { 
                this.update_attr('padding', value); }, this);

            this.model.on('change:margin', function (model, value) { 
                this.update_attr('margin', this._default_px(value)); }, this);

            this.model.on('change:border_radius', function (model, value) { 
                this.update_attr('border-radius', this._default_px(value)); }, this);

            this.after_displayed(function() {
                this.update_visible(this.model, this.model.get("visible"));
                this.update_classes([], this.model.get('_dom_classes'));
                
                this.update_attr('color', this.model.get('color'));
                this.update_attr('background', this.model.get('background_color'));
                this.update_attr('width', this.model.get('width'));
                this.update_attr('height', this.model.get('height'));
                this.update_attr('border-color', this.model.get('border_color'));
                this.update_attr('border-width', this.model.get('border_width'));
                this.update_attr('border-style', this.model.get('border_style'));
                this.update_attr('font-style', this.model.get('font_style'));
                this.update_attr('font-weight', this.model.get('font_weight'));
                this.update_attr('font-size', this._default_px(this.model.get('font_size')));
                this.update_attr('font-family', this.model.get('font_family'));
                this.update_attr('padding', this.model.get('padding'));
                this.update_attr('margin', this._default_px(this.model.get('margin')));
                this.update_attr('border-radius', this._default_px(this.model.get('border_radius')));

                this.update_css(this.model, this.model.get("_css"));
            }, this);
        },

        _default_px: function(value) {
            /**
             * Makes browser interpret a numerical string as a pixel value.
             */
            if (value && /^\d+\.?(\d+)?$/.test(value.trim())) {
                return value.trim() + 'px';
            }
            return value;
        },

        update_attr: function(name, value) {
            /**
             * Set a css attr of the widget view.
             */
            this.el.style[name] = value;
        },

        update_visible: function(model, value) {
            /**
             * Update visibility
             */
            switch(value) {
                case null: // python None
                    this.el.style.display = '';
                    this.el.style.visibility = 'hidden'; break;
                case false:
                    this.el.style.display = 'none'; break;
                case true:
                    this.el.style.display = '';
                    this.el.style.visibility = ''; break;
            }
        },

        update_css: function (model, css) {
            /**
             * Update the css styling of this view.
             */
            if (css === undefined) {return;}
            for (var i = 0; i < css.length; i++) {
                // Apply the css traits to all elements that match the selector.
                var selector = css[i][0];
                var elements = this.el.querySelectorAll(selector) || [this.el];
                if (elements.length > 0) {
                    var trait_key = css[i][1];
                    var trait_value = css[i][2];
                    _.each(elements, function(e) {
                        e.style[trait_key] = trait_value;
                    });
                }
            }
        },

        update_classes: function (old_classes, new_classes, el) {
            /**
             * Update the DOM classes applied to an element, default to this.el.
             */
            if (el===undefined) {
                el = this.el;
            }
            _.difference(old_classes, new_classes).map(function(c) {
                el.classList.remove(c);
            });
            _.difference(new_classes, old_classes).map(function(c) {
                el.classList.add(c);
            });
        },

        update_mapped_classes: function(class_map, trait_name, previous_trait_value, el) {
            /**
             * Update the DOM classes applied to the widget based on a single
{'fraction_non_alphanumeric': 0.1094955923771901, 'fraction_numerical': 0.0009144445663703866, 'mean_word_length': 1.3455301990391215, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611186', 'n_tokens_mistral': 13752, 'n_tokens_neox': 13092, 'n_words': 3321}