Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving editable select upstream to jdaviz #36

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 0 additions & 93 deletions lcviz/components/plugin_editable_select.vue

This file was deleted.

4 changes: 2 additions & 2 deletions lcviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

__all__ = ['LCviz']

custom_components = {}

_default_time_viewer_reference_name = 'flux-vs-time'

custom_components = {'lcviz-editable-select': 'components/plugin_editable_select.vue',
'plugin-ephemeris-select': 'components/plugin_ephemeris_select.vue'}
custom_components = {'plugin-ephemeris-select': 'components/plugin_ephemeris_select.vue'}

# Register pure vue component. This allows us to do recursive component instantiation only in the
# vue component file
Expand Down
13 changes: 7 additions & 6 deletions lcviz/plugins/ephemeris/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
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, FoldedLightCurve

from lcviz.events import EphemerisComponentChangedMessage, EphemerisChangedMessage
from lcviz.template_mixin import EditableSelectPluginComponent
from lcviz.viewers import PhaseScatterView

__all__ = ['Ephemeris']
Expand All @@ -33,7 +32,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 @@ -93,14 +92,16 @@ def __init__(self, *args, **kwargs):
self._prev_wrap_at = _default_wrap_at

self.component = EditableSelectPluginComponent(self,
name='ephemeris',
mode='component_mode',
edit_value='component_edit_value',
items='component_items',
selected='component_selected',
manual_options=['default'],
on_add=self._on_component_add,
on_rename=self._on_component_rename,
on_remove=self._on_component_remove)
on_rename_after_selection=self._on_component_rename, # noqa
on_remove_after_selection=self._on_component_remove) # noqa

# force the original entry in ephemerides with defaults
self._change_component()

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
92 changes: 2 additions & 90 deletions lcviz/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
from glue.core import HubListener

import jdaviz
from jdaviz.core.events import SnackbarMessage
from jdaviz.core.template_mixin import SelectPluginComponent, DatasetSelect
from jdaviz.core.template_mixin import ViewerSelect
from jdaviz.core.template_mixin import ViewerSelect, DatasetSelect, SelectPluginComponent
from lcviz.events import ViewerRenamedMessage, EphemerisComponentChangedMessage

__all__ = ['EditableSelectPluginComponent',
'EphemerisSelect', 'EphemerisSelectMixin']
__all__ = ['EphemerisSelect', 'EphemerisSelectMixin']


# TODO: remove this if/when jdaviz supports renaming viewers natively
Expand All @@ -24,91 +21,6 @@ def __init__(self, *args, **kwargs):
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)


class EphemerisSelect(SelectPluginComponent):
"""
Plugin select for ephemeris components defined by the Ephemeris plugin.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
]
dependencies = [
"astropy>=5.2",
"jdaviz>=3.6",
"jdaviz>=3.7",
"lightkurve@git+https://github.com/lightkurve/lightkurve", # until https://github.com/lightkurve/lightkurve/pull/1342 is in a release (anything after 2.4.0)
]
dynamic = [
Expand Down