##// END OF EJS Templates
Add order respecting method
Jonathan Frederic -
Show More
@@ -337,7 +337,7 b' function(WidgetManager, _, Backbone){'
337 return null;
337 return null;
338 },
338 },
339
339
340 do_diff: function(old_list, new_list, removed_callback, added_callback) {
340 do_diff: function(old_list, new_list, removed_callback, added_callback, respect_order) {
341 // Difference a changed list and call remove and add callbacks for
341 // Difference a changed list and call remove and add callbacks for
342 // each removed and added item in the new list.
342 // each removed and added item in the new list.
343 //
343 //
@@ -349,17 +349,39 b' function(WidgetManager, _, Backbone){'
349 // Callback that is called for each item removed.
349 // Callback that is called for each item removed.
350 // added_callback : Callback(item)
350 // added_callback : Callback(item)
351 // Callback that is called for each item added.
351 // Callback that is called for each item added.
352 // [respect_order] : bool [True]
353 // Whether or not the order of the list matters.
354
355 if (respect_order || respect_order===undefined) {
356 // Walk the lists until an unequal entry is found.
357 var i;
358 for (i = 0; i < new_list.length; i++) {
359 if (i < old_list.length || new_list[i] !== old_list[i]) {
360 break;
361 }
362 }
352
363
364 // Remove the non-matching items from the old list.
365 for (var j = i; j < old_list.length; j++) {
366 console.log(j, old_list.length, old_list[j]);
367 removed_callback(old_list[j]);
368 }
353
369
354 // removed items
370 // Add the rest of the new list items.
355 _.each(this.difference(old_list, new_list), function(item, index, list) {
371 for (i; i < new_list.length; i++) {
356 removed_callback(item);
372 added_callback(new_list[i]);
357 }, this);
373 }
358
374 } else {
359 // added items
375 // removed items
360 _.each(this.difference(new_list, old_list), function(item, index, list) {
376 _.each(this.difference(old_list, new_list), function(item, index, list) {
361 added_callback(item);
377 removed_callback(item);
362 }, this);
378 }, this);
379
380 // added items
381 _.each(this.difference(new_list, old_list), function(item, index, list) {
382 added_callback(item);
383 }, this);
384 }
363 },
385 },
364
386
365 difference: function(a, b) {
387 difference: function(a, b) {
General Comments 0
You need to be logged in to leave comments. Login now