From 4f205192e9162daa6c110fa329ee12322af4301e 2014-07-08 15:57:56 From: Jonathan Frederic Date: 2014-07-08 15:57:56 Subject: [PATCH] Add order respecting method --- diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 6d71364..1c787f6 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -337,7 +337,7 @@ function(WidgetManager, _, Backbone){ return null; }, - do_diff: function(old_list, new_list, removed_callback, added_callback) { + do_diff: function(old_list, new_list, removed_callback, added_callback, respect_order) { // Difference a changed list and call remove and add callbacks for // each removed and added item in the new list. // @@ -349,17 +349,39 @@ function(WidgetManager, _, Backbone){ // Callback that is called for each item removed. // added_callback : Callback(item) // Callback that is called for each item added. + // [respect_order] : bool [True] + // Whether or not the order of the list matters. + + if (respect_order || respect_order===undefined) { + // Walk the lists until an unequal entry is found. + var i; + for (i = 0; i < new_list.length; i++) { + if (i < old_list.length || new_list[i] !== old_list[i]) { + break; + } + } + // Remove the non-matching items from the old list. + for (var j = i; j < old_list.length; j++) { + console.log(j, old_list.length, old_list[j]); + removed_callback(old_list[j]); + } - // removed items - _.each(this.difference(old_list, new_list), function(item, index, list) { - removed_callback(item); - }, this); - - // added items - _.each(this.difference(new_list, old_list), function(item, index, list) { - added_callback(item); - }, this); + // Add the rest of the new list items. + for (i; i < new_list.length; i++) { + added_callback(new_list[i]); + } + } else { + // removed items + _.each(this.difference(old_list, new_list), function(item, index, list) { + removed_callback(item); + }, this); + + // added items + _.each(this.difference(new_list, old_list), function(item, index, list) { + added_callback(item); + }, this); + } }, difference: function(a, b) {