Skip to content

Commit

Permalink
move editable select component upstream
Browse files Browse the repository at this point in the history
NOTE: requires changes in jdaviz and update to pin
  • Loading branch information
kecnry committed Sep 6, 2023
1 parent c9583d0 commit c1df172
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 188 deletions.
93 changes: 0 additions & 93 deletions lcviz/components/plugin_editable_select.vue

This file was deleted.

2 changes: 1 addition & 1 deletion lcviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

__all__ = ['LCviz']

custom_components = {'lcviz-editable-select': 'components/plugin_editable_select.vue'}
custom_components = {}

# Register pure vue component. This allows us to do recursive component instantiation only in the
# vue component file
Expand Down
8 changes: 4 additions & 4 deletions lcviz/plugins/ephemeris/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
from jdaviz.core.custom_traitlets import FloatHandleEmpty
from jdaviz.core.events import (NewViewerMessage, ViewerAddedMessage, ViewerRemovedMessage)
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, SelectPluginComponent,
DatasetSelectMixin)
from jdaviz.core.template_mixin import (PluginTemplateMixin, DatasetSelectMixin,
SelectPluginComponent, EditableSelectPluginComponent)
from jdaviz.core.user_api import PluginUserApi

from lightkurve import periodogram

from lcviz.template_mixin import EditableSelectPluginComponent
from lcviz.viewers import PhaseScatterView

__all__ = ['Ephemeris']
Expand All @@ -31,7 +30,7 @@ class Ephemeris(PluginTemplateMixin, DatasetSelectMixin):
Only the following attributes and methods are available through the
public plugin API.
* ``component`` (:class:`~lcviz.template_mixin.EditableSelectPluginComponent`):
* ``component`` (:class:`~jdaviz.template_mixin.EditableSelectPluginComponent`):
Label of the component corresponding to the active ephemeris.
* :attr:`t0`:
Zeropoint of the ephemeris.
Expand Down Expand Up @@ -85,6 +84,7 @@ def __init__(self, *args, **kwargs):
self._ephemerides = {}

self.component = EditableSelectPluginComponent(self,
name='ephemeris',
mode='component_mode',
edit_value='component_edit_value',
items='component_items',
Expand Down
4 changes: 2 additions & 2 deletions lcviz/plugins/ephemeris/ephemeris.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
:link="'https://lcviz.readthedocs.io/en/'+vdocs+'/plugins.html#ephemeris'"
:popout_button="popout_button">

<lcviz-editable-select
<plugin-editable-select
:mode.sync="component_mode"
:edit_value.sync="component_edit_value"
:items="component_items"
:selected.sync="component_selected"
label="Component"
hint="Select an ephemeris component."
>
</lcviz-editable-select>
</plugin-editable-select>

<v-row justify="end">
<v-btn color="primary" text @click="create_phase_viewer" :disabled="phase_viewer_exists || component_selected.length == 0">
Expand Down
89 changes: 1 addition & 88 deletions lcviz/template_mixin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import jdaviz
from jdaviz.core.events import SnackbarMessage
from jdaviz.core.template_mixin import SelectPluginComponent
from jdaviz.core.template_mixin import ViewerSelect
from lcviz.events import ViewerRenamedMessage

__all__ = ['EditableSelectPluginComponent']
__all__ = []


# TODO: remove this if/when jdaviz supports renaming viewers natively
Expand All @@ -16,88 +14,3 @@ def __init__(self, *args, **kwargs):

# monkey-patch upstream version so all plugins use the viewer-renamed logic
jdaviz.core.template_mixin.ViewerSelect = ViewerSelect


class EditableSelectPluginComponent(SelectPluginComponent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.is_multiselect:
self._multiselect_changed()
self.add_observe(kwargs.get('mode'), self._mode_changed)
self.mode = 'select' # select, rename, add
self._on_add = kwargs.get('on_add', lambda *args: None)
self._on_rename = kwargs.get('on_rename', lambda *args: None)
self._on_remove = kwargs.get('on_remove', lambda *args: None)

def _multiselect_changed(self):
# already subscribed to traitlet by SelectPluginComponent
if self.multiselect:
raise ValueError("EditableSelectPluginComponent does not support multiselect")

def _selected_changed(self, event):
super()._selected_changed(event)
self.edit_value = self.selected

def _mode_changed(self, event):
if self.mode == 'rename:accept':
try:
self.rename_choice(self.selected, self.edit_value)
except ValueError:
self.hub.broadcast(SnackbarMessage("Renaming ephemeris failed",
sender=self, color="error"))
else:
self.mode = 'select'
self.edit_value = self.selected
elif self.mode == 'add:accept':
try:
self.add_choice(self.edit_value)
except ValueError:
self.hub.broadcast(SnackbarMessage("Adding ephemeris failed",
sender=self, color="error"))
else:
self.mode = 'select'
self.edit_value = self.selected
elif self.mode == 'remove:accept':
self.remove_choice(self.edit_value)
if len(self.choices):
self.mode = 'select'
else:
self.mode = 'add'

def _update_items(self):
self.items = [{"label": opt} for opt in self._manual_options]

def _check_new_choice(self, label):
if not len(label):
raise ValueError("new choice must not be blank")
if label in self.choices:
raise ValueError(f"'{label}' is already a valid choice")

def add_choice(self, label, set_as_selected=True):
self._check_new_choice(label)
self._manual_options += [label]
self._update_items()
self._on_add(label)
if set_as_selected:
self.selected = label

def remove_choice(self, label=None):
if label is None:
label = self.selected
if label not in self.choices:
raise ValueError(f"'{label}' not one of available choices ({self.choices})")
self._manual_options.remove(label)
self._update_items()
self._apply_default_selection(skip_if_current_valid=True)
self._on_remove(label)

def rename_choice(self, old, new):
if old not in self.choices:
raise ValueError(f"'{old}' not one of available choices ({self.choices})")
self._check_new_choice(new)
was_selected = self.selected == old
self._manual_options[self._manual_options.index(old)] = new
self._update_items()
if was_selected:
self.selected = new
self._on_rename(old, new)

0 comments on commit c1df172

Please sign in to comment.