##// END OF EJS Templates
Removed for () loops where necessary. Replaced with _.each
Jonathan Frederic -
Show More
@@ -34,14 +34,12 b''
34 34
35 35 // Attach a comm manager to the
36 36 this.comm_manager = comm_manager;
37 this._models = {}; /* Dictionary of model ids and model instances */
37 38
38 39 // Register already-registered widget model types with the comm manager.
39 for (var name in WidgetManager._model_types) {
40 if (WidgetManager._model_types.hasOwnProperty(name)) {
41 this.comm_manager.register_target(name, $.proxy(this._handle_comm_open, this));
42
43 }
44 }
40 _.each(WidgetManager._model_types, function(value, key) {
41 this.comm_manager.register_target(value, $.proxy(this._handle_comm_open, this));
42 });
45 43 };
46 44
47 45 //--------------------------------------------------------------------
@@ -49,7 +47,6 b''
49 47 //--------------------------------------------------------------------
50 48 WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
51 49 WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
52 WidgetManager._models = {}; /* Dictionary of model ids and model instances */
53 50 WidgetManager._managers = []; /* List of widget managers */
54 51
55 52 WidgetManager.register_widget_model = function (model_name, model_type) {
@@ -58,12 +55,11 b''
58 55
59 56 // Register the widget with the comm manager. Make sure to pass this object's context
60 57 // in so `this` works in the call back.
61 for (var i = 0; i < WidgetManager._managers.length; i++) {
62 var instance = WidgetManager._managers[i];
58 _.each(WidgetManager._managers, function(instance, i) {
63 59 if (instance.comm_manager !== null) {
64 60 instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
65 61 }
66 }
62 });
67 63 };
68 64
69 65 WidgetManager.register_widget_view = function (view_name, view_type) {
@@ -169,7 +165,7 b''
169 165 };
170 166
171 167 WidgetManager.prototype.get_model = function (model_id) {
172 var model = WidgetManager._models[model_id];
168 var model = this._models[model_id];
173 169 if (model !== undefined && model.id == model_id) {
174 170 return model;
175 171 }
@@ -180,7 +176,7 b''
180 176 var model_id = comm.comm_id;
181 177 var widget_type_name = msg.content.target_name;
182 178 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
183 WidgetManager._models[model_id] = widget_model;
179 this._models[model_id] = widget_model;
184 180 };
185 181
186 182 IPython.WidgetManager = WidgetManager;
@@ -86,17 +86,14 b' function(WidgetManager, Underscore, Backbone){'
86 86
87 87 apply_update: function (state) {
88 88 // Handle when a widget is updated via the python side.
89 for (var key in state) {
90 if (state.hasOwnProperty(key)) {
91 var value = state[key];
89 _.each(state, function(value, key) {
92 90 this.key_value_lock = [key, value];
93 91 try {
94 92 this.set(key, this._unpack_models(value));
95 93 } finally {
96 94 this.key_value_lock = null;
97 95 }
98 }
99 }
96 });
100 97 },
101 98
102 99 _handle_status: function (msg, callbacks) {
@@ -205,9 +202,9 b' function(WidgetManager, Underscore, Backbone){'
205 202 return value.id;
206 203 } else if (value instanceof Object) {
207 204 var packed = {};
208 for (var key in value) {
209 packed[key] = this._pack_models(value[key]);
210 }
205 _.each(value, function(sub_value, key) {
206 packed[key] = this._pack_models(sub_value);
207 });
211 208 return packed;
212 209 } else {
213 210 return value;
@@ -218,9 +215,9 b' function(WidgetManager, Underscore, Backbone){'
218 215 // Replace model ids with models recursively.
219 216 if (value instanceof Object) {
220 217 var unpacked = {};
221 for (var key in value) {
222 unpacked[key] = this._unpack_models(value[key]);
223 }
218 _.each(value, function(sub_value, key) {
219 unpacked[key] = this._unpack_models(sub_value);
220 });
224 221 return unpacked;
225 222 } else {
226 223 var model = this.widget_manager.get_model(value);
@@ -369,20 +366,16 b' function(WidgetManager, Underscore, Backbone){'
369 366
370 367 var css = this.model.get('_css');
371 368 if (css === undefined) {return;}
372 for (var selector in css) {
373 if (css.hasOwnProperty(selector)) {
369 _.each(css, function(css_traits, selector){
374 370 // Apply the css traits to all elements that match the selector.
375 371 var elements = this._get_selector_element(selector);
376 372 if (elements.length > 0) {
377 var css_traits = css[selector];
378 for (var css_key in css_traits) {
379 if (css_traits.hasOwnProperty(css_key)) {
380 elements.css(css_key, css_traits[css_key]);
381 }
382 }
383 }
384 }
373 _.each(css_traits, function(css_value, css_key){
374 elements.css(css_key, css_value);
375 });
385 376 }
377 });
378
386 379 },
387 380
388 381 _get_selector_element: function (selector) {
@@ -50,12 +50,12 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
50 50 // JQuery slider option keys. These keys happen to have a
51 51 // one-to-one mapping with the corrosponding keys of the model.
52 52 var jquery_slider_keys = ['step', 'max', 'min', 'disabled'];
53 for (var index in jquery_slider_keys) {
54 var key = jquery_slider_keys[index];
55 if (this.model.get(key) !== undefined) {
56 this.$slider.slider("option", key, this.model.get(key));
57 }
53 _.each(jquery_slider_keys, function(key, i) {
54 var model_value = this.model.get(key);
55 if (model_value !== undefined) {
56 this.$slider.slider("option", key, model_value);
58 57 }
58 });
59 59
60 60 // WORKAROUND FOR JQUERY SLIDER BUG.
61 61 // The horizontal position of the slider handle
@@ -49,12 +49,12 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
49 49 // JQuery slider option keys. These keys happen to have a
50 50 // one-to-one mapping with the corrosponding keys of the model.
51 51 var jquery_slider_keys = ['step', 'max', 'min', 'disabled'];
52 for (var index in jquery_slider_keys) {
53 var key = jquery_slider_keys[index];
54 if (this.model.get(key) !== undefined) {
55 this.$slider.slider("option", key, this.model.get(key));
56 }
52 _.each(jquery_slider_keys, function(key, i) {
53 var model_value = this.model.get(key);
54 if (model_value !== undefined) {
55 this.$slider.slider("option", key, model_value);
57 56 }
57 });
58 58
59 59 // WORKAROUND FOR JQUERY SLIDER BUG.
60 60 // The horizontal position of the slider handle
@@ -67,13 +67,13 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
67 67 var items = this.model.get('values');
68 68 var $replace_droplist = $('<ul />')
69 69 .addClass('dropdown-menu');
70 for (var index in items) {
71 var that = this;
70 _.each(items, function(item, i) {
72 71 var item_button = $('<a href="#"/>')
73 .text(items[index])
72 .text(item)
74 73 .on('click', $.proxy(this.handle_click, this));
75 74 $replace_droplist.append($('<li />').append(item_button));
76 }
75 });
76
77 77 this.$droplist.replaceWith($replace_droplist);
78 78 this.$droplist.remove();
79 79 this.$droplist = $replace_droplist;
@@ -140,41 +140,41 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
140 140 // Add missing items to the DOM.
141 141 var items = this.model.get('values');
142 142 var disabled = this.model.get('disabled');
143 for (var index in items) {
144 var item_query = ' :input[value="' + items[index] + '"]';
143 _.each(items, function(item, index) {
144 var item_query = ' :input[value="' + item + '"]';
145 145 if (this.$el.find(item_query).length === 0) {
146 146 var $label = $('<label />')
147 147 .addClass('radio')
148 .text(items[index])
148 .text(item)
149 149 .appendTo(this.$container);
150 150
151 151 $('<input />')
152 152 .attr('type', 'radio')
153 153 .addClass(this.model)
154 .val(items[index])
154 .val(item)
155 155 .prependTo($label)
156 156 .on('click', $.proxy(this.handle_click, this));
157 157 }
158 158
159 159 var $item_element = this.$container.find(item_query);
160 if (this.model.get('value') == items[index]) {
160 if (this.model.get('value') == item) {
161 161 $item_element.prop('checked', true);
162 162 } else {
163 163 $item_element.prop('checked', false);
164 164 }
165 165 $item_element.prop('disabled', disabled);
166 }
166 });
167 167
168 168 // Remove items that no longer exist.
169 169 this.$container.find('input').each(function(i, obj) {
170 170 var value = $(obj).val();
171 171 var found = false;
172 for (var index in items) {
173 if (items[index] == value) {
172 _.each(items, function(item, index) {
173 if (item == value) {
174 174 found = true;
175 175 break;
176 176 }
177 }
177 });
178 178
179 179 if (!found) {
180 180 $(obj).parent().remove();
@@ -230,36 +230,36 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
230 230 // Add missing items to the DOM.
231 231 var items = this.model.get('values');
232 232 var disabled = this.model.get('disabled');
233 for (var index in items) {
234 var item_query = ' :contains("' + items[index] + '")';
233 _.each(items, function(item, index) {
234 var item_query = ' :contains("' + item + '")';
235 235 if (this.$buttongroup.find(item_query).length === 0) {
236 236 $('<button />')
237 237 .attr('type', 'button')
238 238 .addClass('btn')
239 .text(items[index])
239 .text(item)
240 240 .appendTo(this.$buttongroup)
241 241 .on('click', $.proxy(this.handle_click, this));
242 242 }
243 243
244 244 var $item_element = this.$buttongroup.find(item_query);
245 if (this.model.get('value') == items[index]) {
245 if (this.model.get('value') == item) {
246 246 $item_element.addClass('active');
247 247 } else {
248 248 $item_element.removeClass('active');
249 249 }
250 250 $item_element.prop('disabled', disabled);
251 }
251 });
252 252
253 253 // Remove items that no longer exist.
254 254 this.$buttongroup.find('button').each(function(i, obj) {
255 255 var value = $(obj).text();
256 256 var found = false;
257 for (var index in items) {
258 if (items[index] == value) {
257 _.each(items, function(item, index) {
258 if (item == value) {
259 259 found = true;
260 260 break;
261 261 }
262 }
262 });
263 263
264 264 if (!found) {
265 265 $(obj).remove();
@@ -314,16 +314,16 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
314 314 if (options === undefined || options.updated_view != this) {
315 315 // Add missing items to the DOM.
316 316 var items = this.model.get('values');
317 for (var index in items) {
318 var item_query = ' :contains("' + items[index] + '")';
317 _.each(items, function(item, index) {
318 var item_query = ' :contains("' + item + '")';
319 319 if (this.$listbox.find(item_query).length === 0) {
320 320 $('<option />')
321 .text(items[index])
322 .attr('value', items[index])
321 .text(item)
322 .attr('value', item)
323 323 .appendTo(this.$listbox)
324 324 .on('click', $.proxy(this.handle_click, this));
325 325 }
326 }
326 });
327 327
328 328 // Select the correct element
329 329 this.$listbox.val(this.model.get('value'));
@@ -336,12 +336,12 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
336 336 this.$listbox.find('option').each(function(i, obj) {
337 337 var value = $(obj).text();
338 338 var found = false;
339 for (var index in items) {
340 if (items[index] == value) {
339 _.each(items, function(item, index) {
340 if (item == value) {
341 341 found = true;
342 342 break;
343 343 }
344 }
344 });
345 345
346 346 if (!found) {
347 347 $(obj).remove();
@@ -39,28 +39,26 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
39 39 if (options === undefined || options.updated_view != this) {
40 40 // Set tab titles
41 41 var titles = this.model.get('_titles');
42 for (var page_index in titles) {
43
42 _.each(titles, function(title, page_index) {
44 43 var accordian = this.containers[page_index];
45 44 if (accordian !== undefined) {
46 45 accordian
47 46 .find('.accordion-heading')
48 47 .find('.accordion-toggle')
49 .text(titles[page_index]);
50 }
48 .text(title);
51 49 }
50 });
52 51
53 52 // Set selected page
54 53 var selected_index = this.model.get("selected_index");
55 54 if (0 <= selected_index && selected_index < this.containers.length) {
56 for (var index in this.containers) {
55 _.each(this.containers, function(container, index) {
57 56 if (index==selected_index) {
58 this.containers[index].find('.accordion-body').collapse('show');
57 container.find('.accordion-body').collapse('show');
59 58 } else {
60 this.containers[index].find('.accordion-body').collapse('hide');
61 }
62
59 container.find('.accordion-body').collapse('hide');
63 60 }
61 });
64 62 }
65 63 }
66 64 return AccordionView.__super__.update.apply(this);
@@ -218,12 +216,12 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
218 216 if (options === undefined || options.updated_view != this) {
219 217 // Set tab titles
220 218 var titles = this.model.get('_titles');
221 for (var page_index in titles) {
219 _.each(titles, function(title, page_index) {
222 220 var tab_text = this.containers[page_index];
223 221 if (tab_text !== undefined) {
224 tab_text.text(titles[page_index]);
225 }
222 tab_text.text(title);
226 223 }
224 });
227 225
228 226 var selected_index = this.model.get('selected_index');
229 227 if (0 <= selected_index && selected_index < this.containers.length) {
General Comments 0
You need to be logged in to leave comments. Login now