forked from rc/aircox
		
	- save & load
- key navigation - ui improvements
This commit is contained in:
		@ -9,7 +9,7 @@ from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
 | 
				
			|||||||
from ..models import Sound, Track
 | 
					from ..models import Sound, Track
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TrackInline(SortableInlineAdminMixin, admin.TabularInline):
 | 
					class TrackInline(admin.TabularInline):
 | 
				
			||||||
    template = 'admin/aircox/playlist_inline.html'
 | 
					    template = 'admin/aircox/playlist_inline.html'
 | 
				
			||||||
    model = Track
 | 
					    model = Track
 | 
				
			||||||
    extra = 0
 | 
					    extra = 0
 | 
				
			||||||
 | 
				
			|||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							@ -4,21 +4,18 @@
 | 
				
			|||||||
{# include "adminsortable2/edit_inline/tabular-django-4.1.html" #}
 | 
					{# include "adminsortable2/edit_inline/tabular-django-4.1.html" #}
 | 
				
			||||||
{% with inline_admin_formset as admin_formset %}
 | 
					{% with inline_admin_formset as admin_formset %}
 | 
				
			||||||
{% with admin_formset.formset as formset %}
 | 
					{% with admin_formset.formset as formset %}
 | 
				
			||||||
<div id="inline-tracks" class="box mb-5">
 | 
					
 | 
				
			||||||
    <h5 class="title is-4">{% trans "Playlist" %}</h5>
 | 
					<script id="{{ formset.prefix }}-init-data">
 | 
				
			||||||
    <script id="{{ formset.prefix }}-init-data">{
 | 
					{{ formset|inline_data|json }}
 | 
				
			||||||
        "items": [
 | 
					 | 
				
			||||||
            {% for form in formset.forms %}
 | 
					 | 
				
			||||||
            {{ form.initial|json }}
 | 
					 | 
				
			||||||
            {% if not forloop.last %},{% endif %}
 | 
					 | 
				
			||||||
            {% endfor %}
 | 
					 | 
				
			||||||
        ]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					<div id="inline-tracks" class="box mb-5">
 | 
				
			||||||
    {{ admin_formset.non_form_errors }}
 | 
					    {{ admin_formset.non_form_errors }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    <a-playlist-editor data-el="{{ formset.prefix }}-init-data"
 | 
				
			||||||
    <a-playlist-editor>
 | 
					            data-prefix="{{ formset.prefix }}-">
 | 
				
			||||||
 | 
					        <template #title>
 | 
				
			||||||
 | 
					            <h5 class="title is-4">{% trans "Playlist" %}</h5>
 | 
				
			||||||
 | 
					        </template>
 | 
				
			||||||
        <template v-slot:top="{items}">
 | 
					        <template v-slot:top="{items}">
 | 
				
			||||||
            <input type="hidden" name="{{ formset.prefix }}-TOTAL_FORMS"
 | 
					            <input type="hidden" name="{{ formset.prefix }}-TOTAL_FORMS"
 | 
				
			||||||
                :value="items.length || 0"/>
 | 
					                :value="items.length || 0"/>
 | 
				
			||||||
@ -43,7 +40,7 @@
 | 
				
			|||||||
                <input type="hidden"
 | 
					                <input type="hidden"
 | 
				
			||||||
                    :name="'{{ formset.prefix }}-' + row + '-position'"
 | 
					                    :name="'{{ formset.prefix }}-' + row + '-position'"
 | 
				
			||||||
                    :value="row"/>
 | 
					                    :value="row"/>
 | 
				
			||||||
                <input t-if="item.data.id" type="hidden"
 | 
					                <input t-if="item.id" type="hidden"
 | 
				
			||||||
                    :name="'{{ formset.prefix }}-' + row + '-id'"
 | 
					                    :name="'{{ formset.prefix }}-' + row + '-id'"
 | 
				
			||||||
                    :value="item.data.id"/>
 | 
					                    :value="item.data.id"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -59,12 +56,18 @@
 | 
				
			|||||||
        {% for field in admin_formset.fields %}
 | 
					        {% for field in admin_formset.fields %}
 | 
				
			||||||
        {% if not field.widget.is_hidden and not field.is_readonly %}
 | 
					        {% if not field.widget.is_hidden and not field.is_readonly %}
 | 
				
			||||||
        <template v-slot:row-{{ field.name }}="{item,col,row,value,attr,emit}">
 | 
					        <template v-slot:row-{{ field.name }}="{item,col,row,value,attr,emit}">
 | 
				
			||||||
 | 
					            <div class="field">
 | 
				
			||||||
                <div class="control">
 | 
					                <div class="control">
 | 
				
			||||||
                <input type="{{ widget.type }}" class="input half-field"
 | 
					                    <input type="{{ widget.type }}"
 | 
				
			||||||
 | 
					                        :class="['input', item.error(attr) ? 'is-danger' : 'half-field']"
 | 
				
			||||||
                        :name="'{{ formset.prefix }}-' + row + '-{{ field.name }}'"
 | 
					                        :name="'{{ formset.prefix }}-' + row + '-{{ field.name }}'"
 | 
				
			||||||
                        v-model="item.data[attr]"
 | 
					                        v-model="item.data[attr]"
 | 
				
			||||||
                        @change="emit('change', col)"/>
 | 
					                        @change="emit('change', col)"/>
 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
 | 
					                <p v-for="error in item.error(attr)" class="help is-danger">
 | 
				
			||||||
 | 
					                    [[ error ]] !
 | 
				
			||||||
 | 
					                </p>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
        </template>
 | 
					        </template>
 | 
				
			||||||
        {% endif %}
 | 
					        {% endif %}
 | 
				
			||||||
        {% endfor %}
 | 
					        {% endfor %}
 | 
				
			||||||
 | 
				
			|||||||
@ -18,7 +18,7 @@ The audio player
 | 
				
			|||||||
    </noscript>
 | 
					    </noscript>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <a-player ref="player"
 | 
					    <a-player ref="player"
 | 
				
			||||||
            :live-args="{url: '{% url "api:live" %}', timeout:10, src: {{ audio_streams|json }} || []}"
 | 
					            :live-args="{url: '{% url "api:live" %}', timeout:10, src: {{ audio_streams|json|force_escape }} || []}"
 | 
				
			||||||
            button-title="{% translate "Play or pause audio" %}">
 | 
					            button-title="{% translate "Play or pause audio" %}">
 | 
				
			||||||
        <template v-slot:content="{ loaded, live, current }">
 | 
					        <template v-slot:content="{ loaded, live, current }">
 | 
				
			||||||
            <h4 v-if="loaded" class="title is-4">
 | 
					            <h4 v-if="loaded" class="title is-4">
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ from django.contrib.admin.templatetags.admin_urls import admin_urlname
 | 
				
			|||||||
from django.urls import reverse
 | 
					from django.urls import reverse
 | 
				
			||||||
from django.utils.safestring import mark_safe
 | 
					from django.utils.safestring import mark_safe
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from aircox.models import Page, Diffusion, Log
 | 
					from aircox.models import Diffusion, Log
 | 
				
			||||||
 | 
					
 | 
				
			||||||
random.seed()
 | 
					random.seed()
 | 
				
			||||||
register = template.Library()
 | 
					register = template.Library()
 | 
				
			||||||
@ -18,6 +18,7 @@ def do_admin_url(obj, arg, pass_id=True):
 | 
				
			|||||||
    name = admin_urlname(obj._meta, arg)
 | 
					    name = admin_urlname(obj._meta, arg)
 | 
				
			||||||
    return reverse(name, args=(obj.id,)) if pass_id else reverse(name)
 | 
					    return reverse(name, args=(obj.id,)) if pass_id else reverse(name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.filter(name='get_tracks')
 | 
					@register.filter(name='get_tracks')
 | 
				
			||||||
def do_get_tracks(obj):
 | 
					def do_get_tracks(obj):
 | 
				
			||||||
    """ Get a list of track for the provided log, diffusion, or episode """
 | 
					    """ Get a list of track for the provided log, diffusion, or episode """
 | 
				
			||||||
@ -28,6 +29,7 @@ def do_get_tracks(obj):
 | 
				
			|||||||
        obj = obj.episode
 | 
					        obj = obj.episode
 | 
				
			||||||
    return obj.track_set.all()
 | 
					    return obj.track_set.all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.simple_tag(name='has_perm', takes_context=True)
 | 
					@register.simple_tag(name='has_perm', takes_context=True)
 | 
				
			||||||
def do_has_perm(context, obj, perm, user=None):
 | 
					def do_has_perm(context, obj, perm, user=None):
 | 
				
			||||||
    """ Return True if ``user.has_perm('[APP].[perm]_[MODEL]')`` """
 | 
					    """ Return True if ``user.has_perm('[APP].[perm]_[MODEL]')`` """
 | 
				
			||||||
@ -36,17 +38,21 @@ def do_has_perm(context, obj, perm, user=None):
 | 
				
			|||||||
    return user.has_perm('{}.{}_{}'.format(
 | 
					    return user.has_perm('{}.{}_{}'.format(
 | 
				
			||||||
        obj._meta.app_label, perm, obj._meta.model_name))
 | 
					        obj._meta.app_label, perm, obj._meta.model_name))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.filter(name='is_diffusion')
 | 
					@register.filter(name='is_diffusion')
 | 
				
			||||||
def do_is_diffusion(obj):
 | 
					def do_is_diffusion(obj):
 | 
				
			||||||
    """ Return True if object is a Diffusion. """
 | 
					    """ Return True if object is a Diffusion. """
 | 
				
			||||||
    return isinstance(obj, Diffusion)
 | 
					    return isinstance(obj, Diffusion)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.filter(name='json')
 | 
					@register.filter(name='json')
 | 
				
			||||||
def do_json(obj, fields=""):
 | 
					def do_json(obj, fields=""):
 | 
				
			||||||
    """ Return object as json """
 | 
					    """ Return object as json """
 | 
				
			||||||
    if fields:
 | 
					    if fields:
 | 
				
			||||||
        obj = { k: getattr(obj,k,None) for k in ','.split(fields) }
 | 
					        obj = {k: getattr(obj, k, None)
 | 
				
			||||||
    return json.dumps(obj)
 | 
					               for k in ','.split(fields)}
 | 
				
			||||||
 | 
					    return mark_safe(json.dumps(obj))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.simple_tag(name='nav_items', takes_context=True)
 | 
					@register.simple_tag(name='nav_items', takes_context=True)
 | 
				
			||||||
def do_nav_items(context, menu, **kwargs):
 | 
					def do_nav_items(context, menu, **kwargs):
 | 
				
			||||||
@ -55,6 +61,7 @@ def do_nav_items(context, menu, **kwargs):
 | 
				
			|||||||
    return [(item, item.render(request, **kwargs))
 | 
					    return [(item, item.render(request, **kwargs))
 | 
				
			||||||
            for item in station.navitem_set.filter(menu=menu)]
 | 
					            for item in station.navitem_set.filter(menu=menu)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.simple_tag(name='update_query')
 | 
					@register.simple_tag(name='update_query')
 | 
				
			||||||
def do_update_query(obj, **kwargs):
 | 
					def do_update_query(obj, **kwargs):
 | 
				
			||||||
    """ Replace provided querydict's values with **kwargs. """
 | 
					    """ Replace provided querydict's values with **kwargs. """
 | 
				
			||||||
@ -65,6 +72,7 @@ def do_update_query(obj, **kwargs):
 | 
				
			|||||||
            obj.pop(k)
 | 
					            obj.pop(k)
 | 
				
			||||||
    return obj
 | 
					    return obj
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.filter(name='verbose_name')
 | 
					@register.filter(name='verbose_name')
 | 
				
			||||||
def do_verbose_name(obj, plural=False):
 | 
					def do_verbose_name(obj, plural=False):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
@ -74,4 +82,3 @@ def do_verbose_name(obj, plural=False):
 | 
				
			|||||||
    return obj if isinstance(obj, str) else \
 | 
					    return obj if isinstance(obj, str) else \
 | 
				
			||||||
        obj._meta.verbose_name_plural if plural else \
 | 
					        obj._meta.verbose_name_plural if plural else \
 | 
				
			||||||
        obj._meta.verbose_name
 | 
					        obj._meta.verbose_name
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,16 +1,27 @@
 | 
				
			|||||||
from django import template
 | 
					from django import template
 | 
				
			||||||
from django.contrib import admin
 | 
					from django.contrib import admin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..serializers import AdminTrackSerializer
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
register = template.Library()
 | 
					register = template.Library()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.simple_tag(name='get_admin_tools')
 | 
					@register.simple_tag(name='get_admin_tools')
 | 
				
			||||||
def do_get_admin_tools():
 | 
					def do_get_admin_tools():
 | 
				
			||||||
    return admin.site.get_tools()
 | 
					    return admin.site.get_tools()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@register.filter(name='serialize_track')
 | 
					@register.filter(name='inline_data')
 | 
				
			||||||
def do_serialize_track(instance):
 | 
					def do_inline_data(formset):
 | 
				
			||||||
    ser = AdminTrackSerializer(instance=instance)
 | 
					    items = []
 | 
				
			||||||
    return ser.data
 | 
					    for form in formset.forms:
 | 
				
			||||||
 | 
					        item = {name: form[name].value()
 | 
				
			||||||
 | 
					                for name in form.fields.keys()}
 | 
				
			||||||
 | 
					        item['__errors__'] = form.errors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # hack for playlist editor
 | 
				
			||||||
 | 
					        tags = item.get('tags')
 | 
				
			||||||
 | 
					        if tags and not isinstance(tags, str):
 | 
				
			||||||
 | 
					            item['tags'] = ', '.join(tag.name for tag in tags)
 | 
				
			||||||
 | 
					        items.append(item)
 | 
				
			||||||
 | 
					    return {"items": items}
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
				
			|||||||
@ -1,30 +1,35 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div class="playlist-editor">
 | 
					    <div class="playlist-editor">
 | 
				
			||||||
        <slot name="top" :set="set" :columns="columns" :items="items"/>
 | 
					        <div class="columns">
 | 
				
			||||||
        <div class="tabs">
 | 
					            <div class="column">
 | 
				
			||||||
            <ul>
 | 
					                <slot name="title" />
 | 
				
			||||||
                <li :class="{'is-active': mode == Modes.Text}"
 | 
					            </div>
 | 
				
			||||||
 | 
					            <div class="column has-text-right">
 | 
				
			||||||
 | 
					                <div class="float-right field has-addons">
 | 
				
			||||||
 | 
					                    <p class="control">
 | 
				
			||||||
 | 
					                        <a :class="['button','p-2', mode == Modes.Text ? 'is-primary' : 'is-light']"
 | 
				
			||||||
                                @click="mode = Modes.Text">
 | 
					                                @click="mode = Modes.Text">
 | 
				
			||||||
                    <a>
 | 
					 | 
				
			||||||
                            <span class="icon is-small">
 | 
					                            <span class="icon is-small">
 | 
				
			||||||
                                <i class="fa fa-pencil"></i>
 | 
					                                <i class="fa fa-pencil"></i>
 | 
				
			||||||
                            </span>
 | 
					                            </span>
 | 
				
			||||||
                        Texte
 | 
					                            <span>Texte</span>
 | 
				
			||||||
                        </a>
 | 
					                        </a>
 | 
				
			||||||
                </li>
 | 
					                    </p>
 | 
				
			||||||
                <li :class="{'is-active': mode == Modes.List}"
 | 
					                    <p class="control">
 | 
				
			||||||
 | 
					                        <a :class="['button','p-2', mode == Modes.List ? 'is-primary' : 'is-light']"
 | 
				
			||||||
                                @click="mode = Modes.List">
 | 
					                                @click="mode = Modes.List">
 | 
				
			||||||
                    <a>
 | 
					 | 
				
			||||||
                            <span class="icon is-small">
 | 
					                            <span class="icon is-small">
 | 
				
			||||||
                                <i class="fa fa-list"></i>
 | 
					                                <i class="fa fa-list"></i>
 | 
				
			||||||
                            </span>
 | 
					                            </span>
 | 
				
			||||||
                        Liste
 | 
					                            <span>Liste</span>
 | 
				
			||||||
                        </a>
 | 
					                        </a>
 | 
				
			||||||
                </li>
 | 
					                    </p>
 | 
				
			||||||
            </ul>
 | 
					 | 
				
			||||||
                </div>
 | 
					                </div>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					        <slot name="top" :set="set" :columns="columns" :items="items"/>
 | 
				
			||||||
        <section class="page" v-show="mode == Modes.Text">
 | 
					        <section class="page" v-show="mode == Modes.Text">
 | 
				
			||||||
            <textarea ref="textarea" class="is-fullwidth" style="height: 10em;"
 | 
					            <textarea ref="textarea" class="is-fullwidth" rows="20"
 | 
				
			||||||
                @change="updateList"
 | 
					                @change="updateList"
 | 
				
			||||||
            />
 | 
					            />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -36,7 +41,7 @@
 | 
				
			|||||||
                    <table class="table is-bordered is-inline-block"
 | 
					                    <table class="table is-bordered is-inline-block"
 | 
				
			||||||
                            style="vertical-align: middle">
 | 
					                            style="vertical-align: middle">
 | 
				
			||||||
                        <tr>
 | 
					                        <tr>
 | 
				
			||||||
                            <a-row :columns="columns" :item="FormatLabels"
 | 
					                            <a-row :cell="{columns}" :item="FormatLabels"
 | 
				
			||||||
                                @move="formatMove" :orderable="true">
 | 
					                                @move="formatMove" :orderable="true">
 | 
				
			||||||
                            </a-row>
 | 
					                            </a-row>
 | 
				
			||||||
                        </tr>
 | 
					                        </tr>
 | 
				
			||||||
@ -93,22 +98,25 @@ const FormatLabels = {
 | 
				
			|||||||
export default {
 | 
					export default {
 | 
				
			||||||
    components: { ARow, ARows },
 | 
					    components: { ARow, ARows },
 | 
				
			||||||
    props: {
 | 
					    props: {
 | 
				
			||||||
 | 
					        dataEl: String,
 | 
				
			||||||
 | 
					        dataPrefix: String,
 | 
				
			||||||
        listClass: String,
 | 
					        listClass: String,
 | 
				
			||||||
        itemClass: String,
 | 
					        itemClass: String,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    data() {
 | 
					    data() {
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            dataEl: String,
 | 
					 | 
				
			||||||
            Modes: Modes,
 | 
					            Modes: Modes,
 | 
				
			||||||
            FormatLabels: FormatLabels,
 | 
					            FormatLabels: FormatLabels,
 | 
				
			||||||
            mode: Modes.Text,
 | 
					            mode: Modes.Text,
 | 
				
			||||||
            set: new Set(Track),
 | 
					            set: new Set(Track),
 | 
				
			||||||
            columns: ['artist', 'title', 'tags', 'album', 'year'],
 | 
					            columns: ['artist', 'title', 'tags', 'album', 'year'],
 | 
				
			||||||
 | 
					            extraData: {},
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    computed: {
 | 
					    computed: {
 | 
				
			||||||
 | 
					       
 | 
				
			||||||
        items() {
 | 
					        items() {
 | 
				
			||||||
            return this.set.items
 | 
					            return this.set.items
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
@ -194,12 +202,25 @@ export default {
 | 
				
			|||||||
            return lines.join('\n')
 | 
					            return lines.join('\n')
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        _data_key(key) {
 | 
				
			||||||
 | 
					            key = key.slice(this.dataPrefix.length)
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                var [index, attr] = key.split('-', 1)
 | 
				
			||||||
 | 
					                return [Number(index), attr]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            catch(err) {
 | 
				
			||||||
 | 
					                return [null, key]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * Load initial data
 | 
					         * Load initial data
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        loadData({items=[], errors, fieldErrors, ...data}) {
 | 
					        loadData({items=[]}) {
 | 
				
			||||||
            for(var item of items)
 | 
					            for(var index in items)
 | 
				
			||||||
                this.set.push(item)
 | 
					                this.set.push(items[index])
 | 
				
			||||||
 | 
					            this.updateInput()
 | 
				
			||||||
         },
 | 
					         },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -207,10 +228,11 @@ export default {
 | 
				
			|||||||
        if(this.dataEl) {
 | 
					        if(this.dataEl) {
 | 
				
			||||||
            const el = document.getElementById(this.dataEl)
 | 
					            const el = document.getElementById(this.dataEl)
 | 
				
			||||||
            if(el) {
 | 
					            if(el) {
 | 
				
			||||||
                const data = JSON.parse(el.textContext)
 | 
					                const data = JSON.parse(el.textContent)
 | 
				
			||||||
                loadData(data)
 | 
					                this.loadData(data)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        this.mode = (this.items) ? Modes.List : Modes.Text
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
				
			|||||||
@ -1,21 +1,22 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <tr>
 | 
					    <tr>
 | 
				
			||||||
        <slot name="head" :item="item" :row="index"/>
 | 
					        <slot name="head" :item="item" :row="row"/>
 | 
				
			||||||
        <template v-for="(attr,col) in columns" :key="col">
 | 
					        <template v-for="(attr,col) in columns" :key="col">
 | 
				
			||||||
            <td :class="['cell', 'cell-' + attr]" :data-index="col"
 | 
					            <td :class="['cell', 'cell-' + attr]" :data-col="col"
 | 
				
			||||||
                    :draggable="orderable"
 | 
					                    :draggable="orderable"
 | 
				
			||||||
                    @dragstart="onDragStart" @dragover="onDragOver" @drop="onDrop">
 | 
					                    @dragstart="onDragStart" @dragover="onDragOver" @drop="onDrop">
 | 
				
			||||||
                <slot :name="attr" :item="item" :row="index" :col="col"
 | 
					                <slot :name="attr" :item="item" :cell="cells[col]"
 | 
				
			||||||
                        :data="itemData" :attr="attr" :emit="cellEmit"
 | 
					                        :data="itemData" :attr="attr" :emit="cellEmit"
 | 
				
			||||||
                        :value="itemData && itemData[attr]">
 | 
					                        :value="itemData && itemData[attr]">
 | 
				
			||||||
                    {{ itemData && itemData[attr] }}
 | 
					                    {{ itemData && itemData[attr] }}
 | 
				
			||||||
                </slot>
 | 
					                </slot>
 | 
				
			||||||
            </td>
 | 
					            </td>
 | 
				
			||||||
        </template>
 | 
					        </template>
 | 
				
			||||||
        <slot name="tail" :item="item" :row="index"/>
 | 
					        <slot name="tail" :item="item" :row="cell.row"/>
 | 
				
			||||||
    </tr>
 | 
					    </tr>
 | 
				
			||||||
</template>
 | 
					</template>
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
 | 
					import {isReactive, toRefs} from 'vue'
 | 
				
			||||||
import Model from '../model'
 | 
					import Model from '../model'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
@ -23,35 +24,48 @@ export default {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    props: {
 | 
					    props: {
 | 
				
			||||||
        item: Object,
 | 
					        item: Object,
 | 
				
			||||||
        index: Number,
 | 
					        cell: Object,
 | 
				
			||||||
        columns: Array,
 | 
					 | 
				
			||||||
        orderable: {type: Boolean, default: false},
 | 
					        orderable: {type: Boolean, default: false},
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    computed: {
 | 
					    computed: {
 | 
				
			||||||
 | 
					        row() { return this.cell.row || 0 },
 | 
				
			||||||
 | 
					        columns() { return this.cell.columns },
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        itemData() {
 | 
					        itemData() {
 | 
				
			||||||
            return this.item instanceof Model ? this.item.data : this.item;
 | 
					            return this.item instanceof Model ? this.item.data : this.item;
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cells() {
 | 
				
			||||||
 | 
					            const cell = isReactive(this.cell) && toRefs(this.cell) || this.cell
 | 
				
			||||||
 | 
					            const cells = []
 | 
				
			||||||
 | 
					            for(var col in this.columns)
 | 
				
			||||||
 | 
					                cells.push({...cell, col: Number(col)})
 | 
				
			||||||
 | 
					            return cells
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cellEls() {
 | 
				
			||||||
 | 
					            return [...this.$el.querySelectorAll('td')].filter(x => x.dataset.col)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    methods: {
 | 
					    methods: {
 | 
				
			||||||
        /// Emit a 'cell' event.
 | 
					        /// Emit a 'cell' event.
 | 
				
			||||||
        /// Event data: `{index, name, data, item, attr}`
 | 
					        /// Event data: `{name, data, item, attr}`
 | 
				
			||||||
        ///
 | 
					        ///
 | 
				
			||||||
        /// @param {Number} col: cell column's index
 | 
					        /// @param {Number} col: cell column's index
 | 
				
			||||||
        /// @param {String} name: cell's event name
 | 
					        /// @param {String} name: cell's event name
 | 
				
			||||||
        /// @param {} data: cell's event data
 | 
					        /// @param {} data: cell's event data
 | 
				
			||||||
        cellEmit(name, col, data) {
 | 
					        cellEmit(name, cell, data) {
 | 
				
			||||||
            this.$emit('cell', {
 | 
					            this.$emit('cell', {
 | 
				
			||||||
                name, col, data,
 | 
					                name, cell, data,
 | 
				
			||||||
                item: this.item,
 | 
					                item: this.item,
 | 
				
			||||||
                attr: this.columns[col],
 | 
					 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onDragStart(ev) {
 | 
					        onDragStart(ev) {
 | 
				
			||||||
            const dataset = ev.target.dataset;
 | 
					            const dataset = ev.target.dataset;
 | 
				
			||||||
            const data = `cell:${dataset.index}`
 | 
					            const data = `cell:${dataset.col}`
 | 
				
			||||||
            ev.dataTransfer.setData("text/cell", data)
 | 
					            ev.dataTransfer.setData("text/cell", data)
 | 
				
			||||||
            ev.dataTransfer.dropEffect = 'move'
 | 
					            ev.dataTransfer.dropEffect = 'move'
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
@ -69,9 +83,27 @@ export default {
 | 
				
			|||||||
            ev.preventDefault()
 | 
					            ev.preventDefault()
 | 
				
			||||||
            this.$emit('move', {
 | 
					            this.$emit('move', {
 | 
				
			||||||
                from: Number(data.slice(5)),
 | 
					                from: Number(data.slice(5)),
 | 
				
			||||||
                to: Number(ev.target.dataset.index),
 | 
					                to: Number(ev.target.dataset.col),
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        focus(col, from) {
 | 
				
			||||||
 | 
					            if(from)
 | 
				
			||||||
 | 
					                col += from.col
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const target = this.cellEls[col]
 | 
				
			||||||
 | 
					            if(!target)
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            const control = target.querySelector('input') ||
 | 
				
			||||||
 | 
					                            target.querySelector('button') ||
 | 
				
			||||||
 | 
					                            target.querySelector('select') ||
 | 
				
			||||||
 | 
					                            target.querySelector('a');
 | 
				
			||||||
 | 
					            control && control.focus()
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    mounted() {
 | 
				
			||||||
 | 
					        this.$el.__row = this
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -10,18 +10,26 @@
 | 
				
			|||||||
        </thead>
 | 
					        </thead>
 | 
				
			||||||
        <tbody>
 | 
					        <tbody>
 | 
				
			||||||
            <slot name="head"/>
 | 
					            <slot name="head"/>
 | 
				
			||||||
            <template v-for="(item,index) in items" :key="index">
 | 
					            <template v-for="(item,row) in items" :key="row">
 | 
				
			||||||
                <a-row :item="item" :index="index" :columns="columns" :data-index="index"
 | 
					                <!-- data-index comes from AList component drag & drop -->
 | 
				
			||||||
 | 
					                <a-row :item="item" :cell="{row, columns}" :data-index="row"
 | 
				
			||||||
                        :draggable="orderable"
 | 
					                        :draggable="orderable"
 | 
				
			||||||
                        @dragstart="onDragStart" @dragover="onDragOver" @drop="onDrop"
 | 
					                        @dragstart="onDragStart" @dragover="onDragOver" @drop="onDrop"
 | 
				
			||||||
                        @cell="onCellEvent(index, $event)">
 | 
					                        @cell="onCellEvent(index, $event)">
 | 
				
			||||||
                    <template v-for="[name,slot] of rowSlots" :key="slot" v-slot:[slot]="data">
 | 
					                    <template v-for="[name,slot] of rowSlots" :key="slot" v-slot:[slot]="data">
 | 
				
			||||||
 | 
					                        <template v-if="slot == 'head' || slot == 'tail'">
 | 
				
			||||||
                            <slot :name="name" v-bind="data"/>
 | 
					                            <slot :name="name" v-bind="data"/>
 | 
				
			||||||
                        </template>
 | 
					                        </template>
 | 
				
			||||||
 | 
					                        <template v-else>
 | 
				
			||||||
 | 
					                            <div @keydown.capture.ctrl="onControlKey($event, data.cell)">
 | 
				
			||||||
 | 
					                                <slot :name="name" v-bind="data"/>
 | 
				
			||||||
 | 
					                            </div>
 | 
				
			||||||
 | 
					                        </template>
 | 
				
			||||||
 | 
					                    </template>
 | 
				
			||||||
                </a-row>
 | 
					                </a-row>
 | 
				
			||||||
            </template>
 | 
					            </template>
 | 
				
			||||||
            <template v-if="allowCreate">
 | 
					            <template v-if="allowCreate">
 | 
				
			||||||
                <a-row :item="extraItem" :index="items.length" :columns="columns"
 | 
					                <a-row :item="extraItem" :cell="{row:items.length, columns}" 
 | 
				
			||||||
                        @keypress.enter.stop.prevent="validateExtraCell">
 | 
					                        @keypress.enter.stop.prevent="validateExtraCell">
 | 
				
			||||||
                    <template v-for="[name,slot] of rowSlots" :key="slot" v-slot:[slot]="data">
 | 
					                    <template v-for="[name,slot] of rowSlots" :key="slot" v-slot:[slot]="data">
 | 
				
			||||||
                        <slot :name="name" v-bind="data"/>
 | 
					                        <slot :name="name" v-bind="data"/>
 | 
				
			||||||
@ -56,6 +64,17 @@ const Component = {
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    computed: {
 | 
					    computed: {
 | 
				
			||||||
 | 
					        rowCells() {
 | 
				
			||||||
 | 
					            const cells = []
 | 
				
			||||||
 | 
					            for(var row in this.items)
 | 
				
			||||||
 | 
					                cells.push({row, columns: this.columns,})
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        rows() {
 | 
				
			||||||
 | 
					            return [...this.$el.querySelectorAll('tr')].filter(x => x.__row)
 | 
				
			||||||
 | 
					                                                       .map(x => x.__row)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        rowSlots() {
 | 
					        rowSlots() {
 | 
				
			||||||
            return Object.keys(this.$slots).filter(x => x.startsWith('row-'))
 | 
					            return Object.keys(this.$slots).filter(x => x.startsWith('row-'))
 | 
				
			||||||
                                           .map(x => [x, x.slice(4)])
 | 
					                                           .map(x => [x, x.slice(4)])
 | 
				
			||||||
@ -70,18 +89,60 @@ const Component = {
 | 
				
			|||||||
            this.extraItem = new this.set.model()
 | 
					            this.extraItem = new this.set.model()
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// React on 'cell' event, re-emitting it with additional values:
 | 
					        onControlKey(event, cell) {
 | 
				
			||||||
        /// - `set`: data set
 | 
					            switch(event.key) {
 | 
				
			||||||
        /// - `row`: row index
 | 
					                case "ArrowUp": this.focus(-1, 0, cell)
 | 
				
			||||||
        ///
 | 
					                                event.stopPropagation()
 | 
				
			||||||
        /// @param {Number} row: row index
 | 
					                                event.preventDefault()
 | 
				
			||||||
        /// @param {} data: cell's event data
 | 
					                                break;
 | 
				
			||||||
 | 
					                case "ArrowDown": this.focus(1, 0, cell)
 | 
				
			||||||
 | 
					                                event.stopPropagation()
 | 
				
			||||||
 | 
					                                event.preventDefault()
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					                case "ArrowLeft": this.focus(0, -1, cell)
 | 
				
			||||||
 | 
					                                event.stopPropagation()
 | 
				
			||||||
 | 
					                                event.preventDefault()
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					                case "ArrowRight": this.focus(0, 1, cell)
 | 
				
			||||||
 | 
					                                event.stopPropagation()
 | 
				
			||||||
 | 
					                                event.preventDefault()
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * React on 'cell' event, re-emitting it with additional values:
 | 
				
			||||||
 | 
					         * - `set`: data set
 | 
				
			||||||
 | 
					         * - `row`: row index
 | 
				
			||||||
 | 
					         *
 | 
				
			||||||
 | 
					         * @param {Number} row: row index
 | 
				
			||||||
 | 
					         * @param {} data: cell's event data
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
        onCellEvent(row, event) {
 | 
					        onCellEvent(row, event) {
 | 
				
			||||||
 | 
					            if(event.name == 'focus')
 | 
				
			||||||
 | 
					                this.cellFocus(event.data, event.cell)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            this.$emit('cell', {
 | 
					            this.$emit('cell', {
 | 
				
			||||||
                ...event, row,
 | 
					                ...event, row,
 | 
				
			||||||
                set: this.set
 | 
					                set: this.set
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        getCellNode(row, col) {
 | 
				
			||||||
 | 
					            const el = this.$refs[row]
 | 
				
			||||||
 | 
					            return el && el.cellEls(col)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * Focus on a cell
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        focus(row, col, from=null) {
 | 
				
			||||||
 | 
					            if(from)
 | 
				
			||||||
 | 
					                row += from.row
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            row = this.rows[row]
 | 
				
			||||||
 | 
					            row && row.focus(col, from)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
Component.props.itemTag.default = 'tr'
 | 
					Component.props.itemTag.default = 'tr'
 | 
				
			||||||
 | 
				
			|||||||
@ -41,11 +41,15 @@ export default class Model {
 | 
				
			|||||||
        this.commit(data);
 | 
					        this.commit(data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get errors() {
 | 
				
			||||||
 | 
					        return this.data.__errors__
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Get instance id from its data
 | 
					     * Get instance id from its data
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    static getId(data) {
 | 
					    static getId(data) {
 | 
				
			||||||
        return data.id;
 | 
					        return 'id' in data ? data.id : data.pk;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -112,8 +116,16 @@ export default class Model {
 | 
				
			|||||||
     * Update instance's data with provided data. Return None
 | 
					     * Update instance's data with provided data. Return None
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    commit(data) {
 | 
					    commit(data) {
 | 
				
			||||||
        this.id = this.constructor.getId(data);
 | 
					 | 
				
			||||||
        this.data = data;
 | 
					        this.data = data;
 | 
				
			||||||
 | 
					        this.id = this.constructor.getId(this.data);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Update model data, without reset previous value
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    update(data) {
 | 
				
			||||||
 | 
					        this.data = {...this.data, ...data}
 | 
				
			||||||
 | 
					        this.id = this.constructor.getId(this.data)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -130,6 +142,13 @@ export default class Model {
 | 
				
			|||||||
        let item = window.localStorage.getItem(key);
 | 
					        let item = window.localStorage.getItem(key);
 | 
				
			||||||
        return item === null ? item : new this(JSON.parse(item));
 | 
					        return item === null ? item : new this(JSON.parse(item));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Return error for a specific attribute name if any 
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    error(attr=null) {
 | 
				
			||||||
 | 
					        return attr === null ? this.errors : this.errors && this.errors[attr]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user