define(["widgets/js/manager",
"underscore",
"backbone",
"jquery",
"base/js/utils",
"base/js/namespace",
], function(widgetmanager, _, Backbone, $, utils, IPython){
"use strict";
var WidgetModel = Backbone.Model.extend({
constructor: function (widget_manager, model_id, comm) {
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;
this.views = {};
this._resolve_received_state = {};
if (comm !== undefined) {
this.comm = comm;
comm.model = this;
comm.on_close($.proxy(this._handle_comm_closed, this));
comm.on_msg($.proxy(this._handle_comm_msg, this));
this.set_comm_live(true);
} else {
this.set_comm_live(false);
}
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) {
if (this.comm !== undefined) {
var data = {method: 'custom', content: content};
this.comm.send(data, callbacks);
this.pending_msgs++;
}
},
request_state: function(callbacks) {
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());
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) {
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) {
if (this.comm && !comm_closed) {
this.comm.close();
}
this.stopListening();
this.trigger('destroy', this);
delete this.comm.model;
delete this.comm;
delete this.model_id;
_.each(this.views, function(v, id, views) {
v.then(function(view) {
view.remove();
delete views[id];
});
});
},
_handle_comm_closed: function (msg) {
this.trigger('comm:close');
this.close(true);
},
_handle_comm_msg: function (msg) {
var method = msg.content.data.method;
var that = this;
switch (method) {
case 'update':
this.state_change = this.state_change
.then(function() {
return that.set_state(msg.content.data.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);
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;
return this._unpack_models(state).then(function(state) {
that.state_lock = state;
try {
WidgetModel.__super__.set.call(that, state);
} finally {
that.state_lock = null;
}
}).catch(utils.reject("Couldn't set model state", true));
},
get_state: function() {
var state = this.toJSON();
for (var key in state) {
if (state.hasOwnProperty(key)) {
state[key] = this._pack_models(state[key]);
}
}
return state;
},
_handle_status: function (msg, callbacks) {
if (this.comm !== undefined) {
if (msg.content.execution_state ==='idle') {
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) {
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) {
var return_value = WidgetModel.__super__.set.apply(this, arguments);
this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {});
return return_value;
},
sync: function (method, model, options) {
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.toJSON(options);
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];
}
}
}
attrs = this._pack_models(attrs);
if (_.size(attrs) > 0) {
var callbacks = options.callbacks || this.callbacks();
if (this.pending_msgs >= (this.get('msg_throttle') || 3)) {
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 {
var data = {method: 'backbone', sync_data: attrs};
this.comm.send(data, callbacks);
this.pending_msgs++;
}
}
this._buffered_state_diff = {};
},
save_changes: function(callbacks) {
this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
},
_pack_models: function(value) {
var that = this;
var packed;
if (value instanceof Backbone.Model) {
return "IPY_MODEL_" + value.id;
} else if ($.isArray(value)) {
packed = [];
_.each(value, function(sub_value, key) {
packed.push(that._pack_models(sub_value));
});
return packed;
} else if (value instanceof Date || value instanceof String) {
return value;
} else if (value instanceof Object) {
packed = {};
_.each(value, function(sub_value, key) {
packed[key] = that._pack_models(sub_value);
});
return packed;
} else {
return value;
}
},
_unpack_models: function(value) {
var that = this;
var unpacked;
if ($.isArray(value)) {
unpacked = [];
_.each(value, function(sub_value, key) {
unpacked.push(that._unpack_models(sub_value));
});
return Promise.all(unpacked);
} else if (value instanceof Object) {
unpacked = {};
_.each(value, function(sub_value, key) {
unpacked[key] = that._unpack_models(sub_value);
});
return utils.resolve_promises_dict(unpacked);
} else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
return this.widget_manager.get_model(value.slice(10, value.length));
} else {
return Promise.resolve(value);
}
},
on_some_change: function(keys, callback, context) {
this.on('change', function() {
if (keys.some(this.hasChanged, this)) {
callback.apply(context);
}
}, this);
},
});
widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);
var WidgetView = Backbone.View.extend({
initialize: function(parameters) {
this.model.on('change',this.update,this);
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(){
},
create_child_view: function(child_model, options) {
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(){
return this.model.callbacks(this);
},
render: function(){
},
send: function (content) {
this.model.send(content, this.callbacks());
},
touch: function () {
this.model.save_changes(this.callbacks());
},
after_displayed: function (callback, context) {
if (this.is_displayed) {
callback.apply(context);
} else {
this.on('displayed', callback, context);
}
},
remove: function () {
WidgetView.__super__.remove.apply(this, arguments);
this.trigger('remove');
}
});
var DOMWidgetView = WidgetView.extend({
initialize: function (parameters) {
DOMWidgetView.__super__.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) {
if (/^\d+\.?(\d+)?$/.test(value.trim())) {
return value.trim() + 'px';
}
return value;
},
update_attr: function(name, value) {
this.$el.css(name, value);
},
update_visible: function(model, value) {
switch(value) {
case null:
this.$el.show().css('visibility', 'hidden'); break;
case false:
this.$el.hide(); break;
case true:
this.$el.show().css('visibility', ''); break;
}
},
update_css: function (model, css) {
if (css === undefined) {return;}
for (var i = 0; i < css.length; i++) {
var selector = css[i][0];
var elements = this._get_selector_element(selector);
if (elements.length > 0) {
var trait_key = css[i][1];
var trait_value = css[i][2];
elements.css(trait_key ,trait_value);
}
}
},
update_classes: function (old_classes, new_classes, $el) {
if ($el===undefined) {
$el = this.$el;
}
_.difference(old_classes, new_classes).map(function(c) {$el.removeClass(c);})
_.difference(new_classes, old_classes).map(function(c) {$el.addClass(c);})
},
update_mapped_classes: function(class_map, trait_name, previous_trait_value, $el) {
var key = previous_trait_value;
if (key === undefined) {
key = this.model.previous(trait_name);
}
var old_classes = class_map[key] ? class_map[key] : [];
key = this.model.get(trait_name);
var new_classes = class_map[key] ? class_map[key] : [];
this.update_classes(old_classes, new_classes, $el || this.$el);
},
_get_selector_element: function (selector) {
var elements;
if (!selector) {
elements = this.$el;
} else {
elements = this.$el.find(selector).addBack(selector);
}
return elements;
},
typeset: function(element, text){
utils.typeset.apply(null, arguments);
},
});
var ViewList = function(create_view, remove_view, context) {
this.initialize.apply(this, arguments);
};
_.extend(ViewList.prototype, {
initialize: function(create_view, remove_view, context) {
this._handler_context = context || this;
this._models = [];
this.views = [];
this._create_view = create_view;
this._remove_view = remove_view || function(view) {view.remove();};
},
update: function(new_models, create_view, remove_view, context) {
var remove = remove_view || this._remove_view;
var create = create_view || this._create_view;
context = context || this._handler_context;
var i = 0;
for (; i < new_models.length; i++) {
if (i >= this._models.length || new_models[i] !== this._models[i]) {
break;
}
}
var first_removed = i;
var removed = this.views.splice(first_removed, this.views.length-first_removed);
for (var j = 0; j < removed.length; j++) {
removed[j].then(function(view) {
remove.call(context, view)
});
}
for (; i < new_models.length; i++) {
this.views.push(Promise.resolve(create.call(context, new_models[i])));
}
this._models = new_models.slice();
},
remove: function() {
var that = this;
return Promise.all(this.views).then(function(views) {
for (var i = 0; i < that.views.length; i++) {
that._remove_view.call(that._handler_context, views[i]);
}
that.views = [];
that._models = [];
});
},
});
var widget = {
'WidgetModel': WidgetModel,
'WidgetView': WidgetView,
'DOMWidgetView': DOMWidgetView,
'ViewList': ViewList,
};
$.extend(IPython, widget);
return widget;
});