Show More
@@ -413,31 +413,13 b' define(["widgets/js/manager",' | |||||
413 | this.update_visible(this.model, this.model.get("visible")); |
|
413 | this.update_visible(this.model, this.model.get("visible")); | |
414 | this.update_css(this.model, this.model.get("_css")); |
|
414 | this.update_css(this.model, this.model.get("_css")); | |
415 | }, this); |
|
415 | }, this); | |
416 | this.model.on('msg:custom', this.on_msg, this); |
|
|||
417 | this.model.on('change:visible', this.update_visible, this); |
|
416 | this.model.on('change:visible', this.update_visible, this); | |
418 | this.model.on('change:_css', this.update_css, this); |
|
417 | this.model.on('change:_css', this.update_css, this); | |
419 | }, |
|
418 | this.model.on('change:_dom_classes', function(model, new_classes) { | |
420 |
|
419 | var old_classes = model.previous('children'); | ||
421 | on_msg: function(msg) { |
|
420 | this.update_classes(old_classes, new_classes); | |
422 | // Handle DOM specific msgs. |
|
421 | }, this); | |
423 | switch(msg.msg_type) { |
|
422 | this.update_classes([], this.model.get('_dom_classes')); | |
424 | case 'add_class': |
|
|||
425 | this.add_class(msg.selector, msg.class_list); |
|
|||
426 | break; |
|
|||
427 | case 'remove_class': |
|
|||
428 | this.remove_class(msg.selector, msg.class_list); |
|
|||
429 | break; |
|
|||
430 | } |
|
|||
431 | }, |
|
|||
432 |
|
||||
433 | add_class: function (selector, class_list) { |
|
|||
434 | // Add a DOM class to an element. |
|
|||
435 | this._get_selector_element(selector).addClass(class_list); |
|
|||
436 | }, |
|
|||
437 |
|
||||
438 | remove_class: function (selector, class_list) { |
|
|||
439 | // Remove a DOM class from an element. |
|
|||
440 | this._get_selector_element(selector).removeClass(class_list); |
|
|||
441 | }, |
|
423 | }, | |
442 |
|
424 | |||
443 | update_visible: function(model, value) { |
|
425 | update_visible: function(model, value) { | |
@@ -461,6 +443,15 b' define(["widgets/js/manager",' | |||||
461 | } |
|
443 | } | |
462 | }, |
|
444 | }, | |
463 |
|
445 | |||
|
446 | update_classes: function (old_classes, new_classes) { | |||
|
447 | // Update the DOM classes applied to the topmost element. | |||
|
448 | this.do_diff(old_classes, new_classes, function(removed) { | |||
|
449 | this.$el.removeClass(removed); | |||
|
450 | }, function(added) { | |||
|
451 | this.$el.addClass(added); | |||
|
452 | }); | |||
|
453 | }, | |||
|
454 | ||||
464 | _get_selector_element: function (selector) { |
|
455 | _get_selector_element: function (selector) { | |
465 | // Get the elements via the css selector. |
|
456 | // Get the elements via the css selector. | |
466 | var elements; |
|
457 | var elements; |
@@ -379,100 +379,5 b' class Widget(LoggingConfigurable):' | |||||
379 |
|
379 | |||
380 | class DOMWidget(Widget): |
|
380 | class DOMWidget(Widget): | |
381 | visible = Bool(True, help="Whether the widget is visible.", sync=True) |
|
381 | visible = Bool(True, help="Whether the widget is visible.", sync=True) | |
382 |
_css = |
|
382 | _css = CTuple(sync=True, help="CSS property list: (selector, key, value)") | |
383 |
|
383 | _dom_classes = CTuple(sync=True, help="DOM classes applied to widget.$el.") | ||
384 | def get_css(self, key, selector=""): |
|
|||
385 | """Get a CSS property of the widget. |
|
|||
386 |
|
||||
387 | Note: This function does not actually request the CSS from the |
|
|||
388 | front-end; Only properties that have been set with set_css can be read. |
|
|||
389 |
|
||||
390 | Parameters |
|
|||
391 | ---------- |
|
|||
392 | key: unicode |
|
|||
393 | CSS key |
|
|||
394 | selector: unicode (optional) |
|
|||
395 | JQuery selector used when the CSS key/value was set. |
|
|||
396 | """ |
|
|||
397 | if selector in self._css and key in self._css[selector]: |
|
|||
398 | return self._css[selector][key] |
|
|||
399 | else: |
|
|||
400 | return None |
|
|||
401 |
|
||||
402 | def set_css(self, dict_or_key, value=None, selector=''): |
|
|||
403 | """Set one or more CSS properties of the widget. |
|
|||
404 |
|
||||
405 | This function has two signatures: |
|
|||
406 | - set_css(css_dict, selector='') |
|
|||
407 | - set_css(key, value, selector='') |
|
|||
408 |
|
||||
409 | Parameters |
|
|||
410 | ---------- |
|
|||
411 | css_dict : dict |
|
|||
412 | CSS key/value pairs to apply |
|
|||
413 | key: unicode |
|
|||
414 | CSS key |
|
|||
415 | value: |
|
|||
416 | CSS value |
|
|||
417 | selector: unicode (optional, kwarg only) |
|
|||
418 | JQuery selector to use to apply the CSS key/value. If no selector |
|
|||
419 | is provided, an empty selector is used. An empty selector makes the |
|
|||
420 | front-end try to apply the css to a default element. The default |
|
|||
421 | element is an attribute unique to each view, which is a DOM element |
|
|||
422 | of the view that should be styled with common CSS (see |
|
|||
423 | `$el_to_style` in the Javascript code). |
|
|||
424 | """ |
|
|||
425 | if value is None: |
|
|||
426 | css_dict = dict_or_key |
|
|||
427 | else: |
|
|||
428 | css_dict = {dict_or_key: value} |
|
|||
429 |
|
||||
430 | for (key, value) in css_dict.items(): |
|
|||
431 | # First remove the selector/key pair from the css list if it exists. |
|
|||
432 | # Then add the selector/key pair and new value to the bottom of the |
|
|||
433 | # list. |
|
|||
434 | self._css = [x for x in self._css if not (x[0]==selector and x[1]==key)] |
|
|||
435 | self._css += [(selector, key, value)] |
|
|||
436 | self.send_state('_css') |
|
|||
437 |
|
||||
438 | def add_class(self, class_names, selector=""): |
|
|||
439 | """Add class[es] to a DOM element. |
|
|||
440 |
|
||||
441 | Parameters |
|
|||
442 | ---------- |
|
|||
443 | class_names: unicode or list |
|
|||
444 | Class name(s) to add to the DOM element(s). |
|
|||
445 | selector: unicode (optional) |
|
|||
446 | JQuery selector to select the DOM element(s) that the class(es) will |
|
|||
447 | be added to. |
|
|||
448 | """ |
|
|||
449 | class_list = class_names |
|
|||
450 | if isinstance(class_list, (list, tuple)): |
|
|||
451 | class_list = ' '.join(class_list) |
|
|||
452 |
|
||||
453 | self.send({ |
|
|||
454 | "msg_type" : "add_class", |
|
|||
455 | "class_list" : class_list, |
|
|||
456 | "selector" : selector |
|
|||
457 | }) |
|
|||
458 |
|
||||
459 | def remove_class(self, class_names, selector=""): |
|
|||
460 | """Remove class[es] from a DOM element. |
|
|||
461 |
|
||||
462 | Parameters |
|
|||
463 | ---------- |
|
|||
464 | class_names: unicode or list |
|
|||
465 | Class name(s) to remove from the DOM element(s). |
|
|||
466 | selector: unicode (optional) |
|
|||
467 | JQuery selector to select the DOM element(s) that the class(es) will |
|
|||
468 | be removed from. |
|
|||
469 | """ |
|
|||
470 | class_list = class_names |
|
|||
471 | if isinstance(class_list, (list, tuple)): |
|
|||
472 | class_list = ' '.join(class_list) |
|
|||
473 |
|
||||
474 | self.send({ |
|
|||
475 | "msg_type" : "remove_class", |
|
|||
476 | "class_list" : class_list, |
|
|||
477 | "selector" : selector, |
|
|||
478 | }) |
|
General Comments 0
You need to be logged in to leave comments.
Login now