From a0b95dc2dfb84c682bb8f677e5d471f84e5028fe Mon Sep 17 00:00:00 2001 From: luxagraf Date: Wed, 14 Nov 2018 13:17:42 -0600 Subject: wrote out basic notes skeleton --- .gitignore | 11 ++ README.md | 40 +++++ apps/accounts/__init__.py | 1 + apps/accounts/admin.py | 13 ++ apps/accounts/apps.py | 8 + apps/accounts/forms.py | 19 ++ apps/accounts/migrations/0001_initial.py | 55 ++++++ apps/accounts/migrations/__init__.py | 0 apps/accounts/models.py | 20 +++ apps/accounts/signals.py | 12 ++ apps/accounts/tests/__init__.py | 0 apps/accounts/tests/test_models.py | 13 ++ apps/accounts/tests/test_views.py | 23 +++ apps/accounts/urls.py | 12 ++ apps/accounts/views.py | 32 ++++ apps/notes/__init__.py | 0 apps/notes/forms.py | 17 ++ apps/notes/migrations/0001_initial.py | 38 ++++ apps/notes/migrations/0002_auto_20181111_1825.py | 31 ++++ apps/notes/migrations/0003_note_folder.py | 19 ++ apps/notes/migrations/__init__.py | 0 apps/notes/models.py | 38 ++++ apps/notes/serializers.py | 18 ++ apps/notes/tests/__init__.py | 1 + apps/notes/tests/test_models.py | 30 ++++ apps/notes/tests/test_views.py | 57 ++++++ apps/notes/urls.py | 18 ++ apps/notes/views.py | 41 +++++ apps/pages/__init__.py | 0 apps/pages/admin.py | 32 ++++ apps/pages/migrations/0001_initial.py | 26 +++ apps/pages/migrations/__init__.py | 0 apps/pages/models.py | 41 +++++ apps/pages/tests/__init__.py | 0 apps/pages/tests/test_models.py | 20 +++ apps/pages/tests/test_views.py | 29 +++ apps/pages/views.py | 11 ++ apps/utils/__init__.py | 0 apps/utils/next_prev.py | 80 +++++++++ apps/utils/static/autocomplete.js | 10 ++ apps/utils/static/choices.css | 2 + apps/utils/static/choices.min.js | 5 + apps/utils/static/image-loader.js | 47 +++++ apps/utils/static/next-prev-links.js | 60 +++++++ apps/utils/urls.py | 12 ++ apps/utils/util.py | 84 +++++++++ apps/utils/views.py | 69 ++++++++ apps/utils/widgets.py | 144 +++++++++++++++ config/__init__.py | 0 config/base_urls.py | 34 ++++ config/djadmin.sh | 10 ++ config/requirements.txt | 12 ++ config/settings.py | 167 ++++++++++++++++++ config/wsgi.py | 16 ++ design/config.rb | 12 ++ design/sass/_fonts.scss | 10 ++ design/sass/_footer.scss | 24 +++ design/sass/_forms.scss | 85 +++++++++ design/sass/_global.scss | 196 +++++++++++++++++++++ design/sass/_header.scss | 55 ++++++ design/sass/_mixins.scss | 136 ++++++++++++++ design/sass/_queries.scss | 29 +++ design/sass/screenv1.scss | 7 + design/templates/accounts/profile.html | 54 ++++++ design/templates/base.html | 56 ++++++ .../django_registration/activation_complete.html | 8 + .../django_registration/activation_email_body.txt | 5 + .../activation_email_subject.txt | 1 + .../django_registration/registration_complete.html | 7 + .../django_registration/registration_form.html | 17 ++ design/templates/notes/create.html | 18 ++ design/templates/notes/note_list.html | 9 + design/templates/pages/page.html | 7 + design/templates/registration/login.html | 35 ++++ functional_tests.py | 12 ++ manage.py | 17 ++ 76 files changed, 2278 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 apps/accounts/__init__.py create mode 100644 apps/accounts/admin.py create mode 100644 apps/accounts/apps.py create mode 100644 apps/accounts/forms.py create mode 100644 apps/accounts/migrations/0001_initial.py create mode 100644 apps/accounts/migrations/__init__.py create mode 100644 apps/accounts/models.py create mode 100644 apps/accounts/signals.py create mode 100644 apps/accounts/tests/__init__.py create mode 100644 apps/accounts/tests/test_models.py create mode 100644 apps/accounts/tests/test_views.py create mode 100644 apps/accounts/urls.py create mode 100644 apps/accounts/views.py create mode 100644 apps/notes/__init__.py create mode 100644 apps/notes/forms.py create mode 100644 apps/notes/migrations/0001_initial.py create mode 100644 apps/notes/migrations/0002_auto_20181111_1825.py create mode 100644 apps/notes/migrations/0003_note_folder.py create mode 100644 apps/notes/migrations/__init__.py create mode 100644 apps/notes/models.py create mode 100644 apps/notes/serializers.py create mode 100644 apps/notes/tests/__init__.py create mode 100644 apps/notes/tests/test_models.py create mode 100644 apps/notes/tests/test_views.py create mode 100644 apps/notes/urls.py create mode 100644 apps/notes/views.py create mode 100644 apps/pages/__init__.py create mode 100644 apps/pages/admin.py create mode 100644 apps/pages/migrations/0001_initial.py create mode 100644 apps/pages/migrations/__init__.py create mode 100644 apps/pages/models.py create mode 100644 apps/pages/tests/__init__.py create mode 100644 apps/pages/tests/test_models.py create mode 100644 apps/pages/tests/test_views.py create mode 100644 apps/pages/views.py create mode 100644 apps/utils/__init__.py create mode 100644 apps/utils/next_prev.py create mode 100644 apps/utils/static/autocomplete.js create mode 100644 apps/utils/static/choices.css create mode 100644 apps/utils/static/choices.min.js create mode 100644 apps/utils/static/image-loader.js create mode 100644 apps/utils/static/next-prev-links.js create mode 100644 apps/utils/urls.py create mode 100644 apps/utils/util.py create mode 100644 apps/utils/views.py create mode 100644 apps/utils/widgets.py create mode 100644 config/__init__.py create mode 100644 config/base_urls.py create mode 100755 config/djadmin.sh create mode 100644 config/requirements.txt create mode 100644 config/settings.py create mode 100644 config/wsgi.py create mode 100644 design/config.rb create mode 100644 design/sass/_fonts.scss create mode 100644 design/sass/_footer.scss create mode 100644 design/sass/_forms.scss create mode 100644 design/sass/_global.scss create mode 100644 design/sass/_header.scss create mode 100644 design/sass/_mixins.scss create mode 100644 design/sass/_queries.scss create mode 100644 design/sass/screenv1.scss create mode 100644 design/templates/accounts/profile.html create mode 100644 design/templates/base.html create mode 100644 design/templates/django_registration/activation_complete.html create mode 100644 design/templates/django_registration/activation_email_body.txt create mode 100644 design/templates/django_registration/activation_email_subject.txt create mode 100644 design/templates/django_registration/registration_complete.html create mode 100644 design/templates/django_registration/registration_form.html create mode 100644 design/templates/notes/create.html create mode 100644 design/templates/notes/note_list.html create mode 100644 design/templates/pages/page.html create mode 100644 design/templates/registration/login.html create mode 100644 functional_tests.py create mode 100755 manage.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69f3431 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.log +Vagrantfile +.vagrant +.env +venv/ +__pycache__ +.coverage +htmlcov/ +.sass-cache/ +media/ +static/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..872c983 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Project purpose + +Create, edit and view notes + +## Internal Apps +### Accounts +* [User](https://git.luxagraf.net/luxagraf/writer/src/branch/master/apps/accounts/models.py) + * custom User model subclassing AbstractUser as per [Django best practices](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project) +* [UserProfile](https://git.luxagraf.net/luxagraf/writer/src/branch/master/apps/accounts/models.py) + * adds profile to the user class with: + * profile photo + * website + * bio + * location + + +## External dependencies + +#### [Coverage](https://github.com/nedbat/coveragepy) +* Because I hate keeping track of what needs tests + +#### [Python Decouple](https://pypi.org/project/python-decouple/) +* Decouple helps you to organize your settings so that you can change parameters without having to redeploy your app. +* Allows committing settings.py by keeping not-committed settings in one place (.env) + +#### [Django Taggit](https://github.com/alex/django-taggit) +* Simplest way to get robust tagging capabilities + +#### [Django Storages](https://github.com/jschneier/django-storages) +* Simplest way to make all static assets go to Amazon S3 +* Simplifies migrating server setups by keep static assets off the server + +#### [Django Registration](https://github.com/ubernostrum/django-registration/) +* Simplest way to handle all aspects of registration/confirmation (written by James Bennett) + +#### [Django Pwned Passwords](https://github.com/ubernostrum/pwned-passwords-django) +* Because we should inform people when their passwords are compromised (written by James Bennett) + +#### [Django-Extensions](https://github.com/django-extensions/django-extensions) +* Because I'd go crazy without it. shell_plus is a godsend diff --git a/apps/accounts/__init__.py b/apps/accounts/__init__.py new file mode 100644 index 0000000..9332741 --- /dev/null +++ b/apps/accounts/__init__.py @@ -0,0 +1 @@ +default_app_config = 'accounts.apps.AccountAppConfig' diff --git a/apps/accounts/admin.py b/apps/accounts/admin.py new file mode 100644 index 0000000..25f873b --- /dev/null +++ b/apps/accounts/admin.py @@ -0,0 +1,13 @@ +from django.contrib import admin + +from .models import User, UserProfile + + +@admin.register(User) +class UserAdmin(admin.ModelAdmin): + pass + + +@admin.register(UserProfile) +class UserProfileAdmin(admin.ModelAdmin): + pass diff --git a/apps/accounts/apps.py b/apps/accounts/apps.py new file mode 100644 index 0000000..9a7d5ba --- /dev/null +++ b/apps/accounts/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class AccountAppConfig(AppConfig): + name = 'accounts' + + def ready(self): + import accounts.signals diff --git a/apps/accounts/forms.py b/apps/accounts/forms.py new file mode 100644 index 0000000..d53e754 --- /dev/null +++ b/apps/accounts/forms.py @@ -0,0 +1,19 @@ +from django import forms +from django_registration.forms import RegistrationForm + +from .models import User, UserProfile + + +class UserForm(RegistrationForm): + class Meta(RegistrationForm.Meta): + model = User + + +class ProfileForm(forms.ModelForm): + class Meta: + model = UserProfile + fields = '__all__' + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop("user", None) + super(ProfileForm, self).__init__(*args, **kwargs) diff --git a/apps/accounts/migrations/0001_initial.py b/apps/accounts/migrations/0001_initial.py new file mode 100644 index 0000000..0e2775e --- /dev/null +++ b/apps/accounts/migrations/0001_initial.py @@ -0,0 +1,55 @@ +# Generated by Django 2.1.2 on 2018-11-02 19:01 + +from django.conf import settings +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), + ('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')), + ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), + ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), + ], + options={ + 'ordering': ['-date_joined'], + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + migrations.CreateModel( + name='UserProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('photo', models.ImageField(blank=True, null=True, upload_to='profile')), + ('website', models.CharField(blank=True, default='', max_length=300, null=True)), + ('location', models.CharField(blank=True, default='', max_length=300, null=True)), + ('bio', models.TextField(blank=True, default='', null=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/apps/accounts/migrations/__init__.py b/apps/accounts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounts/models.py b/apps/accounts/models.py new file mode 100644 index 0000000..a930081 --- /dev/null +++ b/apps/accounts/models.py @@ -0,0 +1,20 @@ +from django.db import models +from django.contrib.auth.models import AbstractUser + + +class User(AbstractUser): + pass + + class Meta: + ordering = ['-date_joined'] + + +class UserProfile(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE) + photo = models.ImageField(upload_to='profile', null=True, blank=True) + website = models.CharField(max_length=300, null=True, blank=True, default='') + location = models.CharField(max_length=300, null=True, blank=True, default='') + bio = models.TextField(null=True, blank=True, default='') + + def __str__(self): + return self.user.username diff --git a/apps/accounts/signals.py b/apps/accounts/signals.py new file mode 100644 index 0000000..837a7ed --- /dev/null +++ b/apps/accounts/signals.py @@ -0,0 +1,12 @@ +from django.db.models.signals import post_save +from django.dispatch import receiver + +from .models import User, UserProfile + + +@receiver(post_save, sender=User) +def create_profile(sender, update_fields, created, instance, **kwargs): + """ creates a blank profile when a new user signs up """ + if created: + user_profile = UserProfile.objects.create(user=instance) + user_profile.save() diff --git a/apps/accounts/tests/__init__.py b/apps/accounts/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounts/tests/test_models.py b/apps/accounts/tests/test_models.py new file mode 100644 index 0000000..f9b33f6 --- /dev/null +++ b/apps/accounts/tests/test_models.py @@ -0,0 +1,13 @@ +from django.test import TestCase +from mixer.backend.django import mixer + +from accounts.models import User, UserProfile + + +class UserProfileModelTest(TestCase): + + def test_string_representation(self): + user = mixer.blend(User, username='test') + user.save() + profile = UserProfile.objects.get(user=user) + self.assertEqual(str(profile), str(user.username)) diff --git a/apps/accounts/tests/test_views.py b/apps/accounts/tests/test_views.py new file mode 100644 index 0000000..39dcb31 --- /dev/null +++ b/apps/accounts/tests/test_views.py @@ -0,0 +1,23 @@ +from django.test import Client +from django.test import RequestFactory, TestCase +from mixer.backend.django import mixer + +from accounts.models import User +from accounts.views import ProfileView + + +class ProfileViewTest(TestCase): + def setUp(self): + # Every test needs access to the request factory. + self.factory = RequestFactory() + self.user = mixer.blend(User, username='tpynchon', password="gravity") + + def test_profile_view(self): + request = self.factory.get('/settings/') + request.user = self.user + response = ProfileView.as_view()(request) + self.assertEqual(response.status_code, 200) + response.render() + html = response.content.decode('utf8') + self.assertTrue(html.startswith('')) + self.assertIn('

Account Settings

', html) diff --git a/apps/accounts/urls.py b/apps/accounts/urls.py new file mode 100644 index 0000000..d9ee327 --- /dev/null +++ b/apps/accounts/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +from . import views + + +urlpatterns = [ + path( + r'', + views.SettingsListView.as_view(), + name="settings" + ), +] diff --git a/apps/accounts/views.py b/apps/accounts/views.py new file mode 100644 index 0000000..e51c60e --- /dev/null +++ b/apps/accounts/views.py @@ -0,0 +1,32 @@ +from django.views.generic import UpdateView, DetailView +from django.utils.decorators import method_decorator +from django.contrib.auth.decorators import login_required + +from .models import UserProfile +from .forms import ProfileForm + + +@method_decorator(login_required, name='dispatch') +class UpdateViewWithUser(UpdateView): + + def get_form_kwargs(self, **kwargs): + kwargs = super().get_form_kwargs(**kwargs) + kwargs.update({'user': self.request.user}) + return kwargs + + +class ProfileView(UpdateViewWithUser): + model = UserProfile + form_class = ProfileForm + template_name = "accounts/profile.html" + + def get_object(self): + return self.request.user.userprofile + + +class SettingsListView(DetailView): + model = UserProfile + template_name = "accounts/profile.html" + + def get_object(self): + return self.request.user.userprofile diff --git a/apps/notes/__init__.py b/apps/notes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/notes/forms.py b/apps/notes/forms.py new file mode 100644 index 0000000..6a27ec9 --- /dev/null +++ b/apps/notes/forms.py @@ -0,0 +1,17 @@ +from django import forms +from django.utils.translation import ugettext_lazy as _ + +from .models import Note + + +class NoteForm(forms.ModelForm): + class Meta: + model = Note + fields = ['title', 'body_markdown', 'url', 'tags'] + labels = { + "body_markdown": _("Note"), + } + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop("user", None) + super(NoteForm, self).__init__(*args, **kwargs) diff --git a/apps/notes/migrations/0001_initial.py b/apps/notes/migrations/0001_initial.py new file mode 100644 index 0000000..3f04eb7 --- /dev/null +++ b/apps/notes/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 2.1.2 on 2018-11-11 18:01 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import taggit.managers + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('taggit', '0002_auto_20150616_2121'), + ] + + operations = [ + migrations.CreateModel( + name='Folder', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=250)), + ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=250)), + ('body_markdown', models.TextField(null=True)), + ('url', models.CharField(max_length=250)), + ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('tags', taggit.managers.TaggableManager(blank=True, help_text='Tags', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags')), + ], + ), + ] diff --git a/apps/notes/migrations/0002_auto_20181111_1825.py b/apps/notes/migrations/0002_auto_20181111_1825.py new file mode 100644 index 0000000..a7bc1c7 --- /dev/null +++ b/apps/notes/migrations/0002_auto_20181111_1825.py @@ -0,0 +1,31 @@ +# Generated by Django 2.1.2 on 2018-11-11 18:25 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='date_created', + field=models.DateTimeField(blank=True, default=datetime.datetime(2018, 11, 11, 18, 25, 29, 645561), editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='note', + name='date_updated', + field=models.DateTimeField(blank=True, default=datetime.datetime(2018, 11, 11, 18, 25, 42, 867666), editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='note', + name='slug', + field=models.SlugField(blank=True, unique=True), + ), + ] diff --git a/apps/notes/migrations/0003_note_folder.py b/apps/notes/migrations/0003_note_folder.py new file mode 100644 index 0000000..d548429 --- /dev/null +++ b/apps/notes/migrations/0003_note_folder.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.2 on 2018-11-14 15:41 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0002_auto_20181111_1825'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='folder', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='notes.Folder'), + ), + ] diff --git a/apps/notes/migrations/__init__.py b/apps/notes/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/notes/models.py b/apps/notes/models.py new file mode 100644 index 0000000..aeb3bb3 --- /dev/null +++ b/apps/notes/models.py @@ -0,0 +1,38 @@ +from django.db import models +from django.utils import timezone +from django.template.defaultfilters import slugify + +from taggit.managers import TaggableManager + +from django.conf import settings + + +class Folder(models.Model): + created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + name = models.CharField(max_length=250) + + def __str__(self): + return self.name + + +class Note(models.Model): + created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + date_created = models.DateTimeField(blank=True, editable=False) + date_updated = models.DateTimeField(blank=True, editable=False) + title = models.CharField(max_length=250) + body_markdown = models.TextField(null=True) + url = models.CharField(max_length=250) + slug = models.SlugField(unique=True, blank=True) + folder = models.ForeignKey(Folder, null=True, on_delete=models.SET_NULL) + tags = TaggableManager(blank=True, help_text='Tags') + + def __str__(self): + return self.title + + def save(self, **kwargs): + # On save, update timestamps (users updated through admin.py) + if not self.id: + self.date_created = timezone.now() + self.slug = slugify(self.title)[:50] + self.date_updated = timezone.now() + super(Note, self).save() diff --git a/apps/notes/serializers.py b/apps/notes/serializers.py new file mode 100644 index 0000000..d6c7392 --- /dev/null +++ b/apps/notes/serializers.py @@ -0,0 +1,18 @@ +from rest_framework import serializers +from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer + +from .models import Note, Folder + + +class NoteSerializer(TaggitSerializer, serializers.HyperlinkedModelSerializer): + tags = TagListSerializerField() + + class Meta: + model = Note + fields = ('title', 'body_markdown', 'url', 'folder', 'tags', 'date_created') + + +class FolderSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Folder + fields = ('name') diff --git a/apps/notes/tests/__init__.py b/apps/notes/tests/__init__.py new file mode 100644 index 0000000..1d7ccb7 --- /dev/null +++ b/apps/notes/tests/__init__.py @@ -0,0 +1 @@ +from notes.tests import test_models, test_views diff --git a/apps/notes/tests/test_models.py b/apps/notes/tests/test_models.py new file mode 100644 index 0000000..e6cdb6a --- /dev/null +++ b/apps/notes/tests/test_models.py @@ -0,0 +1,30 @@ +from django.test import TestCase +from mixer.backend.django import mixer + +from notes.models import Note, Folder +from accounts.models import User + + +class FolderModelTest(TestCase): + + def test_string_representation(self): + user = mixer.blend(User, username='tpynchon') + folder = Folder(created_by=user, name="My Folder") + self.assertEqual(str(folder), "My Folder") + + +class NoteModelTest(TestCase): + + def test_string_representation(self): + user = mixer.blend(User, username='test') + note = Note( + created_by=user, + title="test note", + body_markdown="the body of the note", + url="https://luxagraf.net/", + tags="mine,cool site" + ) + self.assertEqual(str(note), "test note") + self.assertEqual(str(note.body_markdown), "the body of the note") + self.assertEqual(str(note.url), "https://luxagraf.net/") + self.assertEqual(str(note.tags), "mine,cool site") diff --git a/apps/notes/tests/test_views.py b/apps/notes/tests/test_views.py new file mode 100644 index 0000000..3f21b0b --- /dev/null +++ b/apps/notes/tests/test_views.py @@ -0,0 +1,57 @@ +import json +from django.test import Client +from django.test import RequestFactory, TestCase + +from rest_framework.test import force_authenticate +from rest_framework.test import APIRequestFactory +from mixer.backend.django import mixer + +from accounts.models import User +from notes.models import Note +from notes.views import NoteListView, NoteViewSet + + +class StoriesViewTest(TestCase): + def setUp(self): + # Every test needs access to the request factory. + self.factory = RequestFactory() + # test API with rest framework request factory. + self.apifactory = APIRequestFactory() + self.user = mixer.blend(User, username='tpynchon', password="gravity") + self.note = Note.objects.create( + created_by=self.user, + title="test note", + body_markdown="the body of the note", + url="https://luxagraf.net/", + ) + self.note.tags.add("mine,cool site") + self.note.save() + + def test_list_view(self): + request = self.factory.get('/%s/notes/' % (self.user.username)) + request.user = self.user + response = NoteListView.as_view()(request) + self.assertEqual(response.status_code, 200) + response.render() + + def test_api_list(self): + # Make an authenticated request to the view... + request = self.factory.get('/api/notes/') + force_authenticate(request, user=self.user) + response = NoteViewSet.as_view({'get': 'list'})(request) + self.assertEqual(response.status_code, 200) + response.render() + api_data = json.loads(response.content.decode('utf8'))[0] + self.assertEqual(api_data['title'], 'test note') + self.assertEqual(api_data['tags'], ['mine,cool site']) + + def test_api_(self): + # Make an authenticated request to the view... + request = self.factory.get('/api/notes/') + force_authenticate(request, user=self.user) + response = NoteViewSet.as_view({'get': 'list'})(request) + self.assertEqual(response.status_code, 200) + response.render() + api_data = json.loads(response.content.decode('utf8'))[0] + self.assertEqual(api_data['title'], 'test note') + self.assertEqual(api_data['tags'], ['mine,cool site']) diff --git a/apps/notes/urls.py b/apps/notes/urls.py new file mode 100644 index 0000000..76bdeb1 --- /dev/null +++ b/apps/notes/urls.py @@ -0,0 +1,18 @@ +from django.urls import path + +from . import views + +app_name = "notes" + +urlpatterns = [ + path( + r'create/', + views.NoteCreateView.as_view(), + name="note-create" + ), + path( + r'', + views.NoteListView.as_view(), + name="note" + ), +] diff --git a/apps/notes/views.py b/apps/notes/views.py new file mode 100644 index 0000000..ddb72ed --- /dev/null +++ b/apps/notes/views.py @@ -0,0 +1,41 @@ +from django.views.generic import CreateView, ListView, UpdateView, DeleteView +from django.views.generic.detail import DetailView +from django.utils.decorators import method_decorator +from django.contrib.auth.decorators import login_required + +from rest_framework import viewsets +from .serializers import NoteSerializer, FolderSerializer +from .models import Note +from .forms import NoteForm + + +@method_decorator(login_required, name='dispatch') +class LoggedInCreateViewWithUser(CreateView): + + def get_form_kwargs(self, **kwargs): + kwargs = super().get_form_kwargs(**kwargs) + kwargs.update({'user': self.request.user}) + return kwargs + + +class NoteListView(ListView): + model = Note + + def get_queryset(self): + return Note.objects.filter(created_by=self.request.user) + + +class NoteCreateView(LoggedInCreateViewWithUser): + model = Note + form_class = NoteForm + template_name = "notes/create.html" + + +class NoteViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to be viewed or edited. + """ + serializer_class = NoteSerializer + + def get_queryset(self): + return Note.objects.filter(created_by=self.request.user).order_by('-date_created') diff --git a/apps/pages/__init__.py b/apps/pages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/pages/admin.py b/apps/pages/admin.py new file mode 100644 index 0000000..706d690 --- /dev/null +++ b/apps/pages/admin.py @@ -0,0 +1,32 @@ +from django.contrib import admin + +from django import forms + +from pages.models import Page + + +class PageEntryForm(forms.ModelForm): + class Meta: + model = Page + fields = '__all__' + widgets = { + 'body_markdown': forms.Textarea(attrs={'rows': 50, 'cols': 100}), + } + + +@admin.register(Page) +class PageAdmin(admin.ModelAdmin): + form = PageEntryForm + list_display = ('title', 'slug', 'path') + search_fields = ['title', 'body_markdown'] + prepopulated_fields = {"slug": ('title',)} + fieldsets = ( + ('Page', { + 'fields': ('title', 'body_markdown', ('slug', 'path', )), + 'classes': ('show', 'extrapretty', 'wide') + }), + ('Metadata', { + 'classes': ('collapse closed',), + 'fields': ('meta_description',), + }) + ) diff --git a/apps/pages/migrations/0001_initial.py b/apps/pages/migrations/0001_initial.py new file mode 100644 index 0000000..49e763f --- /dev/null +++ b/apps/pages/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 2.1.2 on 2018-11-11 21:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Page', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('slug', models.SlugField()), + ('body_html', models.TextField(blank=True)), + ('body_markdown', models.TextField()), + ('meta_description', models.CharField(blank=True, max_length=256, null=True)), + ('path', models.CharField(blank=True, max_length=200, null=True)), + ], + ), + ] diff --git a/apps/pages/migrations/__init__.py b/apps/pages/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/pages/models.py b/apps/pages/models.py new file mode 100644 index 0000000..0c663cb --- /dev/null +++ b/apps/pages/models.py @@ -0,0 +1,41 @@ +from django.db import models +from django.template.defaultfilters import slugify +from django.contrib.sitemaps import Sitemap + +from utils.util import markdown_to_html, render_images + + +class Page(models.Model): + title = models.CharField(max_length=200) + slug = models.SlugField() + body_html = models.TextField(blank=True) + body_markdown = models.TextField() + meta_description = models.CharField(max_length=256, null=True, blank=True) + path = models.CharField(max_length=200, null=True, blank=True) + + def __str__(self): + return self.title + + def get_absolute_url(self): + if self.path: + return "/%s/%s" % (self.path, self.slug) + else: + return "/%s" % (self.slug) + + def save(self): + # run markdown + md = render_images(self.body_markdown) + self.body_html = markdown_to_html(md) + if not self.id: + # self.date_created = timezone.now() + self.slug = slugify(self.title)[:50] + super(Page, self).save() + + +class PageSitemap(Sitemap): + changefreq = "never" + priority = 1.0 + protocol = "https" + + def items(self): + return Page.objects.all() diff --git a/apps/pages/tests/__init__.py b/apps/pages/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/pages/tests/test_models.py b/apps/pages/tests/test_models.py new file mode 100644 index 0000000..f8de5c7 --- /dev/null +++ b/apps/pages/tests/test_models.py @@ -0,0 +1,20 @@ +from django.test import TestCase + +from pages.models import Page + + +class PageModelTest(TestCase): + + def test_string_representation(self): + page = Page( + title="Test Page", + meta_description="The meta desc", + body_markdown="the body of the page", + ) + page.save() + self.assertEqual(str(page), "Test Page") + self.assertEqual(str(page.slug), "test-page") + self.assertEqual(str(page.body_markdown), "the body of the page") + self.assertEqual(str(page.body_html), "

the body of the page

") + self.assertEqual(str(page.meta_description), "The meta desc") + self.assertEqual(page.path, None) diff --git a/apps/pages/tests/test_views.py b/apps/pages/tests/test_views.py new file mode 100644 index 0000000..42a91e4 --- /dev/null +++ b/apps/pages/tests/test_views.py @@ -0,0 +1,29 @@ +from django.test import RequestFactory, TestCase +from mixer.backend.django import mixer + +from accounts.models import User +from pages.models import Page +from pages.views import PageDetailView + + +class PageViewTest(TestCase): + def setUp(self): + # Every test needs access to the request factory. + self.factory = RequestFactory() + self.page = Page( + title="Test Page", + meta_description="The meta desc", + body_markdown="the body of the page", + ) + self.page.save() + + def test_non_existent_page(self): + """A non-existent staticflatpage raises a 404.""" + response = self.client.get('/no_such_page/') + self.assertEqual(response.status_code, 404) + + def test_detail_view(self): + response = self.client.get(self.page.get_absolute_url()) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 200) + print(response.content) diff --git a/apps/pages/views.py b/apps/pages/views.py new file mode 100644 index 0000000..617ce04 --- /dev/null +++ b/apps/pages/views.py @@ -0,0 +1,11 @@ +from django.views.generic.detail import DetailView +from pages.models import Page + + +class PageDetailView(DetailView): + model = Page + slug_field = "slug" + + def get_template_names(self): + obj = self.get_object() + return ["pages/%s.html" % obj.slug, 'pages/page.html'] diff --git a/apps/utils/__init__.py b/apps/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/utils/next_prev.py b/apps/utils/next_prev.py new file mode 100644 index 0000000..766add1 --- /dev/null +++ b/apps/utils/next_prev.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# from https://github.com/gregplaysguitar/django-next-prev/blob/master/next_prev.py + +from functools import partial + +from django.db import models + +if not locals().get('reduce'): + from functools import reduce + +__version__ = '1.0.1' +VERSION = tuple(map(int, __version__.split('.'))) + + +def get_model_attr(instance, attr): + """Example usage: get_model_attr(instance, 'category__slug')""" + for field in attr.split('__'): + instance = getattr(instance, field) + return instance + + +def next_or_prev_in_order(instance, qs=None, prev=False, loop=False): + """Get the next (or previous with prev=True) item for instance, from the + given queryset (which is assumed to contain instance) respecting + queryset ordering. If loop is True, return the first/last item when the + end/start is reached. """ + + if not qs: + qs = instance.__class__.objects.all() + + if prev: + qs = qs.reverse() + lookup = 'lt' + else: + lookup = 'gt' + + q_list = [] + prev_fields = [] + + if qs.query.extra_order_by: + ordering = qs.query.extra_order_by + elif qs.query.order_by: + ordering = qs.query.order_by + elif qs.query.get_meta().ordering: + ordering = qs.query.get_meta().ordering + else: + ordering = [] + + ordering = list(ordering) + + # if the ordering doesn't contain pk, append it and reorder the queryset + # to ensure consistency + if 'pk' not in ordering and '-pk' not in ordering: + ordering.append('pk') + qs = qs.order_by(*ordering) + + for field in ordering: + if field[0] == '-': + this_lookup = (lookup == 'gt' and 'lt' or 'gt') + field = field[1:] + else: + this_lookup = lookup + q_kwargs = dict([(f, get_model_attr(instance, f)) + for f in prev_fields]) + key = "%s__%s" % (field, this_lookup) + q_kwargs[key] = get_model_attr(instance, field) + q_list.append(models.Q(**q_kwargs)) + prev_fields.append(field) + try: + return qs.filter(reduce(models.Q.__or__, q_list))[0] + except IndexError: + length = qs.count() + if loop and length > 1: + # queryset is reversed above if prev + return qs[0] + return None + + +next_in_order = partial(next_or_prev_in_order, prev=False) +prev_in_order = partial(next_or_prev_in_order, prev=True) diff --git a/apps/utils/static/autocomplete.js b/apps/utils/static/autocomplete.js new file mode 100644 index 0000000..ad0c70d --- /dev/null +++ b/apps/utils/static/autocomplete.js @@ -0,0 +1,10 @@ +function autoCompleteItems() { +var item = document.getElementById('id_ap'); +var singlePresetOpts = new Choices(item, { + searchPlaceholderValue: 'Search for Animal', + placeholder: true, +}); +} +document.addEventListener("DOMContentLoaded", function(event) { + autoCompleteItems(); +}); diff --git a/apps/utils/static/choices.css b/apps/utils/static/choices.css new file mode 100644 index 0000000..2c1e7f1 --- /dev/null +++ b/apps/utils/static/choices.css @@ -0,0 +1,2 @@ +.choices{position:relative;margin-bottom:24px;font-size:16px; min-width:400px;} +.choices:focus{outline:none}.choices:last-child{margin-bottom:0}.choices.is-disabled .choices__inner,.choices.is-disabled .choices__input{background-color:#eaeaea;cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.choices.is-disabled .choices__item{cursor:not-allowed}.choices[data-type*=select-one]{cursor:pointer}.choices[data-type*=select-one] .choices__inner{padding-bottom:7.5px}.choices[data-type*=select-one] .choices__input{display:block;width:100%;padding:10px;border-bottom:1px solid #ddd;background-color:#fff;margin:0}.choices[data-type*=select-one] .choices__button{background-image:url(../../icons/cross-inverse.svg);padding:0;background-size:8px;position:absolute;top:50%;right:0;margin-top:-10px;margin-right:25px;height:20px;width:20px;border-radius:10em;opacity:.5}.choices[data-type*=select-one] .choices__button:focus,.choices[data-type*=select-one] .choices__button:hover{opacity:1}.choices[data-type*=select-one] .choices__button:focus{box-shadow:0 0 0 2px #00bcd4}.choices[data-type*=select-one]:after{content:"";height:0;width:0;border-style:solid;border-color:#333 transparent transparent transparent;border-width:5px;position:absolute;right:11.5px;top:50%;margin-top:-2.5px;pointer-events:none}.choices[data-type*=select-one].is-open:after{border-color:transparent transparent #333 transparent;margin-top:-7.5px}.choices[data-type*=select-one][dir=rtl]:after{left:11.5px;right:auto}.choices[data-type*=select-one][dir=rtl] .choices__button{right:auto;left:0;margin-left:25px;margin-right:0}.choices[data-type*=select-multiple] .choices__inner,.choices[data-type*=text] .choices__inner{cursor:text}.choices[data-type*=select-multiple] .choices__button,.choices[data-type*=text] .choices__button{position:relative;display:inline-block;margin:0 -4px 0 8px;padding-left:16px;border-left:1px solid #008fa1;background-image:url(../../icons/cross.svg);background-size:8px;width:8px;line-height:1;opacity:.75}.choices[data-type*=select-multiple] .choices__button:focus,.choices[data-type*=select-multiple] .choices__button:hover,.choices[data-type*=text] .choices__button:focus,.choices[data-type*=text] .choices__button:hover{opacity:1}.choices__inner{display:inline-block;vertical-align:top;width:100%;background-color:#f9f9f9;padding:7.5px 7.5px 3.75px;border:1px solid #ddd;border-radius:2.5px;font-size:14px;min-height:44px;overflow:hidden}.is-focused .choices__inner,.is-open .choices__inner{border-color:#b7b7b7}.is-open .choices__inner{border-radius:2.5px 2.5px 0 0}.is-flipped.is-open .choices__inner{border-radius:0 0 2.5px 2.5px}.choices__list{margin:0;padding-left:0;list-style:none}.choices__list--single{display:inline-block;padding:4px 16px 4px 4px;width:100%}[dir=rtl] .choices__list--single{padding-right:4px;padding-left:16px}.choices__list--single .choices__item{width:100%}.choices__list--multiple{display:inline}.choices__list--multiple .choices__item{display:inline-block;vertical-align:middle;border-radius:20px;padding:4px 10px;font-size:12px;font-weight:500;margin-right:3.75px;margin-bottom:3.75px;background-color:#00bcd4;border:1px solid #00a5bb;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:5px}[dir=rtl] .choices__list--multiple .choices__item{margin-right:0;margin-left:3.75px}.choices__list--multiple .choices__item.is-highlighted{background-color:#00a5bb;border:1px solid #008fa1}.is-disabled .choices__list--multiple .choices__item{background-color:#aaa;border:1px solid #919191}.choices__list--dropdown{display:none;z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;border-bottom-left-radius:2.5px;border-bottom-right-radius:2.5px;overflow:hidden;word-break:break-all}.choices__list--dropdown.is-active{display:block}.is-open .choices__list--dropdown{border-color:#b7b7b7}.is-flipped .choices__list--dropdown{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-radius:.25rem .25rem 0 0}.choices__list--dropdown .choices__list{position:relative;max-height:300px;overflow:auto;-webkit-overflow-scrolling:touch;will-change:scroll-position}.choices__list--dropdown .choices__item{position:relative;padding:10px;font-size:14px}[dir=rtl] .choices__list--dropdown .choices__item{text-align:right}@media (min-width:640px){.choices__list--dropdown .choices__item--selectable{padding-right:100px}.choices__list--dropdown .choices__item--selectable:after{content:attr(data-select-text);font-size:12px;opacity:0;position:absolute;right:10px;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%)}[dir=rtl] .choices__list--dropdown .choices__item--selectable{text-align:right;padding-left:100px;padding-right:10px}[dir=rtl] .choices__list--dropdown .choices__item--selectable:after{right:auto;left:10px}}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.choices__list--dropdown .choices__item--selectable.is-highlighted:after{opacity:.5}.choices__item{cursor:default}.choices__item--selectable{cursor:pointer}.choices__item--disabled{cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:.5}.choices__heading{font-weight:600;font-size:12px;padding:10px;border-bottom:1px solid #f7f7f7;color:gray}.choices__button{text-indent:-9999px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;background-color:transparent;background-repeat:no-repeat;background-position:center;cursor:pointer}.choices__button:focus{outline:none}.choices__input{display:inline-block;vertical-align:baseline;background-color:#f9f9f9;font-size:14px;margin-bottom:5px;border:0;border-radius:0;max-width:100%;padding:4px 0 4px 2px}.choices__input:focus{outline:0}[dir=rtl] .choices__input{padding-right:2px;padding-left:0}.choices__placeholder{opacity:.5} diff --git a/apps/utils/static/choices.min.js b/apps/utils/static/choices.min.js new file mode 100644 index 0000000..197f18e --- /dev/null +++ b/apps/utils/static/choices.min.js @@ -0,0 +1,5 @@ +/*! choices.js v3.0.3 | (c) 2018 Josh Johnson | https://github.com/jshjohnson/Choices#readme */ +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Choices=t():e.Choices=t()}(this,function(){return function(e){function t(n){if(i[n])return i[n].exports;var s=i[n]={exports:{},id:n,loaded:!1};return e[n].call(s.exports,s,s.exports,t),s.loaded=!0,s.exports}var i={};return t.m=e,t.c=i,t.p="/assets/scripts/dist/",t(0)}([function(e,t,i){e.exports=i(1)},function(e,t,i){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function s(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function o(e){if(Array.isArray(e)){for(var t=0,i=Array(e.length);t0&&void 0!==arguments[0]?arguments[0]:"[data-choice]",i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(r(this,e),(0,v.isType)("String",t)){var n=document.querySelectorAll(t);if(n.length>1)for(var s=1;s"'+e+'"'},maxItemText:function(e){return"Only "+e+" values can be added."},uniqueItemText:"Only unique values can be added.",classNames:{containerOuter:"choices",containerInner:"choices__inner",input:"choices__input",inputCloned:"choices__input--cloned",list:"choices__list",listItems:"choices__list--multiple",listSingle:"choices__list--single",listDropdown:"choices__list--dropdown",item:"choices__item",itemSelectable:"choices__item--selectable",itemDisabled:"choices__item--disabled",itemChoice:"choices__item--choice",placeholder:"choices__placeholder",group:"choices__group",groupHeading:"choices__heading",button:"choices__button",activeState:"is-active",focusState:"is-focused",openState:"is-open",disabledState:"is-disabled",highlightedState:"is-highlighted",hiddenState:"is-hidden",flippedState:"is-flipped",loadingState:"is-loading",noResults:"has-no-results",noChoices:"has-no-choices"},fuseOptions:{include:"score"},callbackOnInit:null,callbackOnCreateTemplates:null};if(this.idNames={itemChoice:"item-choice"},this.config=(0,v.extend)(a,i),"auto"!==this.config.renderSelectedChoices&&"always"!==this.config.renderSelectedChoices&&(this.config.silent||console.warn("renderSelectedChoices: Possible values are 'auto' and 'always'. Falling back to 'auto'."),this.config.renderSelectedChoices="auto"),this.store=new f.default(this.render),this.initialised=!1,this.currentState={},this.prevState={},this.currentValue="",this.element=t,this.passedElement=(0,v.isType)("String",t)?document.querySelector(t):t,!this.passedElement)return void(this.config.silent||console.error("Passed element not found"));this.isTextElement="text"===this.passedElement.type,this.isSelectOneElement="select-one"===this.passedElement.type,this.isSelectMultipleElement="select-multiple"===this.passedElement.type,this.isSelectElement=this.isSelectOneElement||this.isSelectMultipleElement,this.isValidElementType=this.isTextElement||this.isSelectElement,this.isIe11=!(!navigator.userAgent.match(/Trident/)||!navigator.userAgent.match(/rv[ :]11/)),this.isScrollingOnIe=!1,this.config.shouldSortItems===!0&&this.isSelectOneElement&&(this.config.silent||console.warn("shouldSortElements: Type of passed element is 'select-one', falling back to false.")),this.highlightPosition=0,this.canSearch=this.config.searchEnabled,this.placeholder=!1,this.isSelectOneElement||(this.placeholder=!!this.config.placeholder&&(this.config.placeholderValue||this.passedElement.getAttribute("placeholder"))),this.presetChoices=this.config.choices,this.presetItems=this.config.items,this.passedElement.value&&(this.presetItems=this.presetItems.concat(this.passedElement.value.split(this.config.delimiter))),this.baseId=(0,v.generateId)(this.passedElement,"choices-"),this.render=this.render.bind(this),this._onFocus=this._onFocus.bind(this),this._onBlur=this._onBlur.bind(this),this._onKeyUp=this._onKeyUp.bind(this),this._onKeyDown=this._onKeyDown.bind(this),this._onClick=this._onClick.bind(this),this._onTouchMove=this._onTouchMove.bind(this),this._onTouchEnd=this._onTouchEnd.bind(this),this._onMouseDown=this._onMouseDown.bind(this),this._onMouseOver=this._onMouseOver.bind(this),this._onPaste=this._onPaste.bind(this),this._onInput=this._onInput.bind(this),this.wasTap=!0;var c="classList"in document.documentElement;c||this.config.silent||console.error("Choices: Your browser doesn't support Choices");var l=(0,v.isElement)(this.passedElement)&&this.isValidElementType;if(l){if("active"===this.passedElement.getAttribute("data-choice"))return;this.init()}else this.config.silent||console.error("Incompatible input passed")}return a(e,[{key:"init",value:function(){if(this.initialised!==!0){var e=this.config.callbackOnInit;this.initialised=!0,this._createTemplates(),this._createInput(),this.store.subscribe(this.render),this.render(),this._addEventListeners(),e&&(0,v.isType)("Function",e)&&e.call(this)}}},{key:"destroy",value:function(){if(this.initialised!==!1){this._removeEventListeners(),this.passedElement.classList.remove(this.config.classNames.input,this.config.classNames.hiddenState),this.passedElement.removeAttribute("tabindex");var e=this.passedElement.getAttribute("data-choice-orig-style");Boolean(e)?(this.passedElement.removeAttribute("data-choice-orig-style"),this.passedElement.setAttribute("style",e)):this.passedElement.removeAttribute("style"),this.passedElement.removeAttribute("aria-hidden"),this.passedElement.removeAttribute("data-choice"),this.passedElement.value=this.passedElement.value,this.containerOuter.parentNode.insertBefore(this.passedElement,this.containerOuter),this.containerOuter.parentNode.removeChild(this.containerOuter),this.clearStore(),this.config.templates=null,this.initialised=!1}}},{key:"renderGroups",value:function(e,t,i){var n=this,s=i||document.createDocumentFragment(),o=this.config.sortFilter;return this.config.shouldSort&&e.sort(o),e.forEach(function(e){var i=t.filter(function(t){return n.isSelectOneElement?t.groupId===e.id:t.groupId===e.id&&!t.selected});if(i.length>=1){var o=n._getTemplate("choiceGroup",e);s.appendChild(o),n.renderChoices(i,s,!0)}}),s}},{key:"renderChoices",value:function(e,t){var i=this,n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],s=t||document.createDocumentFragment(),r=this.config,a=r.renderSelectedChoices,c=r.searchResultLimit,l=r.renderChoiceLimit,h=this.isSearching?v.sortByScore:this.config.sortFilter,u=function(e){var t="auto"!==a||(i.isSelectOneElement||!e.selected);if(t){var n=i._getTemplate("choice",e);s.appendChild(n)}},d=e;"auto"!==a||this.isSelectOneElement||(d=e.filter(function(e){return!e.selected}));var f=d.reduce(function(e,t){return t.placeholder?e.placeholderChoices.push(t):e.normalChoices.push(t),e},{placeholderChoices:[],normalChoices:[]}),p=f.placeholderChoices,m=f.normalChoices;(this.config.shouldSort||this.isSearching)&&m.sort(h);var g=d.length,y=[].concat(o(p),o(m));this.isSearching?g=c:l>0&&!n&&(g=l);for(var b=0;b1&&void 0!==arguments[1]?arguments[1]:null,n=i||document.createDocumentFragment();if(this.config.shouldSortItems&&!this.isSelectOneElement&&e.sort(this.config.sortFilter),this.isTextElement){var s=this.store.getItemsReducedToValues(e),o=s.join(this.config.delimiter);this.passedElement.setAttribute("value",o),this.passedElement.value=o}else{var r=document.createDocumentFragment();e.forEach(function(e){var i=t._getTemplate("option",e);r.appendChild(i)}),this.passedElement.innerHTML="",this.passedElement.appendChild(r)}return e.forEach(function(e){var i=t._getTemplate("item",e);n.appendChild(i)}),n}},{key:"render",value:function(){if(!this.store.isLoading()&&(this.currentState=this.store.getState(),this.currentState!==this.prevState)){if((this.currentState.choices!==this.prevState.choices||this.currentState.groups!==this.prevState.groups||this.currentState.items!==this.prevState.items)&&this.isSelectElement){var e=this.store.getGroupsFilteredByActive(),t=this.store.getChoicesFilteredByActive(),i=document.createDocumentFragment();this.choiceList.innerHTML="",this.config.resetScrollPosition&&(this.choiceList.scrollTop=0),e.length>=1&&this.isSearching!==!0?i=this.renderGroups(e,t,i):t.length>=1&&(i=this.renderChoices(t,i));var n=this.store.getItemsFilteredByActive(),s=this._canAddItem(n,this.input.value);if(i.childNodes&&i.childNodes.length>0)s.response?(this.choiceList.appendChild(i),this._highlightChoice()):this.choiceList.appendChild(this._getTemplate("notice",s.notice));else{var o=void 0,r=void 0;this.isSearching?(r=(0,v.isType)("Function",this.config.noResultsText)?this.config.noResultsText():this.config.noResultsText,o=this._getTemplate("notice",r,"no-results")):(r=(0,v.isType)("Function",this.config.noChoicesText)?this.config.noChoicesText():this.config.noChoicesText,o=this._getTemplate("notice",r,"no-choices")),this.choiceList.appendChild(o)}}if(this.currentState.items!==this.prevState.items){var a=this.store.getItemsFilteredByActive();if(this.itemList.innerHTML="",a&&a){var c=this.renderItems(a);c.childNodes&&this.itemList.appendChild(c)}}this.prevState=this.currentState}}},{key:"highlightItem",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(!e)return this;var i=e.id,n=e.groupId,s=n>=0?this.store.getGroupById(n):null;return this.store.dispatch((0,p.highlightItem)(i,!0)),t&&(s&&s.value?(0,v.triggerEvent)(this.passedElement,"highlightItem",{id:i,value:e.value,label:e.label,groupValue:s.value}):(0,v.triggerEvent)(this.passedElement,"highlightItem",{id:i,value:e.value,label:e.label})),this}},{key:"unhighlightItem",value:function(e){if(!e)return this;var t=e.id,i=e.groupId,n=i>=0?this.store.getGroupById(i):null;return this.store.dispatch((0,p.highlightItem)(t,!1)),n&&n.value?(0,v.triggerEvent)(this.passedElement,"unhighlightItem",{id:t,value:e.value,label:e.label,groupValue:n.value}):(0,v.triggerEvent)(this.passedElement,"unhighlightItem",{id:t,value:e.value,label:e.label}),this}},{key:"highlightAll",value:function(){var e=this,t=this.store.getItems();return t.forEach(function(t){e.highlightItem(t)}),this}},{key:"unhighlightAll",value:function(){var e=this,t=this.store.getItems();return t.forEach(function(t){e.unhighlightItem(t)}),this}},{key:"removeItemsByValue",value:function(e){var t=this;if(!e||!(0,v.isType)("String",e))return this;var i=this.store.getItemsFilteredByActive();return i.forEach(function(i){i.value===e&&t._removeItem(i)}),this}},{key:"removeActiveItems",value:function(e){var t=this,i=this.store.getItemsFilteredByActive();return i.forEach(function(i){i.active&&e!==i.id&&t._removeItem(i)}),this}},{key:"removeHighlightedItems",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=this.store.getItemsFilteredByActive();return i.forEach(function(i){i.highlighted&&i.active&&(e._removeItem(i),t&&e._triggerChange(i.value))}),this}},{key:"showDropdown",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t=document.body,i=document.documentElement,n=Math.max(t.scrollHeight,t.offsetHeight,i.clientHeight,i.scrollHeight,i.offsetHeight);this.containerOuter.classList.add(this.config.classNames.openState),this.containerOuter.setAttribute("aria-expanded","true"),this.dropdown.classList.add(this.config.classNames.activeState),this.dropdown.setAttribute("aria-expanded","true");var s=this.dropdown.getBoundingClientRect(),o=Math.ceil(s.top+window.scrollY+this.dropdown.offsetHeight),r=!1;return"auto"===this.config.position?r=o>=n:"top"===this.config.position&&(r=!0),r&&this.containerOuter.classList.add(this.config.classNames.flippedState),e&&this.canSearch&&document.activeElement!==this.input&&this.input.focus(),(0,v.triggerEvent)(this.passedElement,"showDropdown",{}),this}},{key:"hideDropdown",value:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t=this.containerOuter.classList.contains(this.config.classNames.flippedState);return this.containerOuter.classList.remove(this.config.classNames.openState),this.containerOuter.setAttribute("aria-expanded","false"),this.dropdown.classList.remove(this.config.classNames.activeState),this.dropdown.setAttribute("aria-expanded","false"),t&&this.containerOuter.classList.remove(this.config.classNames.flippedState),e&&this.canSearch&&document.activeElement===this.input&&this.input.blur(),(0,v.triggerEvent)(this.passedElement,"hideDropdown",{}),this}},{key:"toggleDropdown",value:function(){var e=this.dropdown.classList.contains(this.config.classNames.activeState);return e?this.hideDropdown():this.showDropdown(!0),this}},{key:"getValue",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=this.store.getItemsFilteredByActive(),n=[];return i.forEach(function(i){e.isTextElement?n.push(t?i.value:i):i.active&&n.push(t?i.value:i)}),this.isSelectOneElement?n[0]:n}},{key:"setValue",value:function(e){var t=this;if(this.initialised===!0){var i=[].concat(o(e)),n=function(e){var i=(0,v.getType)(e);if("Object"===i){if(!e.value)return;t.isTextElement?t._addItem(e.value,e.label,e.id,void 0,e.customProperties,e.placeholder):t._addChoice(e.value,e.label,!0,!1,-1,e.customProperties,e.placeholder)}else"String"===i&&(t.isTextElement?t._addItem(e):t._addChoice(e,e,!0,!1,-1,null))};i.length>1?i.forEach(function(e){n(e)}):n(i[0])}return this}},{key:"setValueByChoice",value:function(e){var t=this;if(!this.isTextElement){var i=this.store.getChoices(),n=(0,v.isType)("Array",e)?e:[e];n.forEach(function(e){var n=i.find(function(t){return t.value===e});n?n.selected?t.config.silent||console.warn("Attempting to select choice already selected"):t._addItem(n.value,n.label,n.id,n.groupId,n.customProperties,n.placeholder,n.keyCode):t.config.silent||console.warn("Attempting to select choice that does not exist")})}return this}},{key:"setChoices",value:function(e,t,i){var n=this,s=arguments.length>3&&void 0!==arguments[3]&&arguments[3];if(this.initialised===!0&&this.isSelectElement){if(!(0,v.isType)("Array",e)||!t)return this;s&&this._clearChoices(),this._setLoading(!0),e&&e.length&&(this.containerOuter.classList.remove(this.config.classNames.loadingState),e.forEach(function(e){e.choices?n._addGroup(e,e.id||null,t,i):n._addChoice(e[t],e[i],e.selected,e.disabled,void 0,e.customProperties,e.placeholder)})),this._setLoading(!1)}return this}},{key:"clearStore",value:function(){return this.store.dispatch((0,p.clearAll)()),this}},{key:"clearInput",value:function(){return this.input.value&&(this.input.value=""),this.isSelectOneElement||this._setInputWidth(),!this.isTextElement&&this.config.searchEnabled&&(this.isSearching=!1,this.store.dispatch((0,p.activateChoices)(!0))),this}},{key:"enable",value:function(){if(this.initialised){this.passedElement.disabled=!1;var e=this.containerOuter.classList.contains(this.config.classNames.disabledState);e&&(this._addEventListeners(),this.passedElement.removeAttribute("disabled"),this.input.removeAttribute("disabled"),this.containerOuter.classList.remove(this.config.classNames.disabledState),this.containerOuter.removeAttribute("aria-disabled"),this.isSelectOneElement&&this.containerOuter.setAttribute("tabindex","0"))}return this}},{key:"disable",value:function(){if(this.initialised){this.passedElement.disabled=!0;var e=!this.containerOuter.classList.contains(this.config.classNames.disabledState);e&&(this._removeEventListeners(),this.passedElement.setAttribute("disabled",""),this.input.setAttribute("disabled",""),this.containerOuter.classList.add(this.config.classNames.disabledState),this.containerOuter.setAttribute("aria-disabled","true"),this.isSelectOneElement&&this.containerOuter.setAttribute("tabindex","-1"))}return this}},{key:"ajax",value:function(e){var t=this;return this.initialised===!0&&this.isSelectElement&&(requestAnimationFrame(function(){t._handleLoadingState(!0)}),e(this._ajaxCallback())),this}},{key:"_triggerChange",value:function(e){e&&(0,v.triggerEvent)(this.passedElement,"change",{value:e})}},{key:"_handleButtonAction",value:function(e,t){if(e&&t&&this.config.removeItems&&this.config.removeItemButton){var i=t.parentNode.getAttribute("data-id"),n=e.find(function(e){return e.id===parseInt(i,10)});this._removeItem(n),this._triggerChange(n.value),this.isSelectOneElement&&this._selectPlaceholderChoice()}}},{key:"_selectPlaceholderChoice",value:function(){var e=this.store.getPlaceholderChoice();e&&(this._addItem(e.value,e.label,e.id,e.groupId,null,e.placeholder),this._triggerChange(e.value))}},{key:"_handleItemAction",value:function(e,t){var i=this,n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(e&&t&&this.config.removeItems&&!this.isSelectOneElement){var s=t.getAttribute("data-id");e.forEach(function(e){e.id!==parseInt(s,10)||e.highlighted?n||e.highlighted&&i.unhighlightItem(e):i.highlightItem(e)}),document.activeElement!==this.input&&this.input.focus()}}},{key:"_handleChoiceAction",value:function(e,t){if(e&&t){var i=t.getAttribute("data-id"),n=this.store.getChoiceById(i),s=e[0]&&e[0].keyCode?e[0].keyCode:null,o=this.dropdown.classList.contains(this.config.classNames.activeState);if(n.keyCode=s,(0,v.triggerEvent)(this.passedElement,"choice",{choice:n}),n&&!n.selected&&!n.disabled){var r=this._canAddItem(e,n.value);r.response&&(this._addItem(n.value,n.label,n.id,n.groupId,n.customProperties,n.placeholder,n.keyCode),this._triggerChange(n.value))}this.clearInput(),o&&this.isSelectOneElement&&(this.hideDropdown(),this.containerOuter.focus())}}},{key:"_handleBackspace",value:function(e){if(this.config.removeItems&&e){var t=e[e.length-1],i=e.some(function(e){return e.highlighted});this.config.editItems&&!i&&t?(this.input.value=t.value,this._setInputWidth(),this._removeItem(t),this._triggerChange(t.value)):(i||this.highlightItem(t,!1),this.removeHighlightedItems(!0))}}},{key:"_canAddItem",value:function(e,t){var i=!0,n=(0,v.isType)("Function",this.config.addItemText)?this.config.addItemText(t):this.config.addItemText;(this.isSelectMultipleElement||this.isTextElement)&&this.config.maxItemCount>0&&this.config.maxItemCount<=e.length&&(i=!1,n=(0,v.isType)("Function",this.config.maxItemText)?this.config.maxItemText(this.config.maxItemCount):this.config.maxItemText),this.isTextElement&&this.config.addItems&&i&&this.config.regexFilter&&(i=this._regexFilter(t));var s=!e.some(function(e){return(0,v.isType)("String",t)?e.value===t.trim():e.value===t});return s||this.config.duplicateItems||this.isSelectOneElement||!i||(i=!1,n=(0,v.isType)("Function",this.config.uniqueItemText)?this.config.uniqueItemText(t):this.config.uniqueItemText),{response:i,notice:n}}},{key:"_handleLoadingState",value:function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],t=this.itemList.querySelector("."+this.config.classNames.placeholder);e?(this.containerOuter.classList.add(this.config.classNames.loadingState),this.containerOuter.setAttribute("aria-busy","true"),this.isSelectOneElement?t?t.innerHTML=this.config.loadingText:(t=this._getTemplate("placeholder",this.config.loadingText),this.itemList.appendChild(t)):this.input.placeholder=this.config.loadingText):(this.containerOuter.classList.remove(this.config.classNames.loadingState),this.isSelectOneElement?t.innerHTML=this.placeholder||"":this.input.placeholder=this.placeholder||"")}},{key:"_ajaxCallback",value:function(){var e=this;return function(t,i,n){if(t&&i){var s=(0,v.isType)("Object",t)?[t]:t;s&&(0,v.isType)("Array",s)&&s.length?(e._handleLoadingState(!1),e._setLoading(!0),s.forEach(function(t){if(t.choices){var s=t.id||null;e._addGroup(t,s,i,n)}else e._addChoice(t[i],t[n],t.selected,t.disabled,void 0,t.customProperties,t.placeholder)}),e._setLoading(!1),e.isSelectOneElement&&e._selectPlaceholderChoice()):e._handleLoadingState(!1),e.containerOuter.removeAttribute("aria-busy")}}}},{key:"_searchChoices",value:function(e){var t=(0,v.isType)("String",e)?e.trim():e,i=(0,v.isType)("String",this.currentValue)?this.currentValue.trim():this.currentValue;if(t.length>=1&&t!==i+" "){var n=this.store.getSearchableChoices(),s=t,o=(0,v.isType)("Array",this.config.searchFields)?this.config.searchFields:[this.config.searchFields],r=Object.assign(this.config.fuseOptions,{keys:o}),a=new l.default(n,r),c=a.search(s);return this.currentValue=t,this.highlightPosition=0,this.isSearching=!0,this.store.dispatch((0,p.filterChoices)(c)),c.length}return 0}},{key:"_handleSearch",value:function(e){if(e){var t=this.store.getChoices(),i=t.some(function(e){return!e.active});if(this.input===document.activeElement)if(e&&e.length>=this.config.searchFloor){var n=0;this.config.searchChoices&&(n=this._searchChoices(e)),(0,v.triggerEvent)(this.passedElement,"search",{value:e,resultCount:n})}else i&&(this.isSearching=!1,this.store.dispatch((0,p.activateChoices)(!0)))}}},{key:"_addEventListeners",value:function(){document.addEventListener("keyup",this._onKeyUp),document.addEventListener("keydown",this._onKeyDown),document.addEventListener("click",this._onClick),document.addEventListener("touchmove",this._onTouchMove),document.addEventListener("touchend",this._onTouchEnd),document.addEventListener("mousedown",this._onMouseDown),document.addEventListener("mouseover",this._onMouseOver),this.isSelectOneElement&&(this.containerOuter.addEventListener("focus",this._onFocus),this.containerOuter.addEventListener("blur",this._onBlur)),this.input.addEventListener("input",this._onInput),this.input.addEventListener("paste",this._onPaste),this.input.addEventListener("focus",this._onFocus),this.input.addEventListener("blur",this._onBlur)}},{key:"_removeEventListeners",value:function(){document.removeEventListener("keyup",this._onKeyUp),document.removeEventListener("keydown",this._onKeyDown),document.removeEventListener("click",this._onClick),document.removeEventListener("touchmove",this._onTouchMove),document.removeEventListener("touchend",this._onTouchEnd),document.removeEventListener("mousedown",this._onMouseDown),document.removeEventListener("mouseover",this._onMouseOver),this.isSelectOneElement&&(this.containerOuter.removeEventListener("focus",this._onFocus),this.containerOuter.removeEventListener("blur",this._onBlur)),this.input.removeEventListener("input",this._onInput),this.input.removeEventListener("paste",this._onPaste),this.input.removeEventListener("focus",this._onFocus),this.input.removeEventListener("blur",this._onBlur)}},{key:"_setInputWidth",value:function(){this.placeholder?this.input.value&&this.input.value.length>=this.placeholder.length/1.25&&(this.input.style.width=(0,v.getWidthOfInput)(this.input)):this.input.style.width=(0,v.getWidthOfInput)(this.input)}},{key:"_onKeyDown",value:function(e){var t,i=this;if(e.target===this.input||this.containerOuter.contains(e.target)){var n=e.target,o=this.store.getItemsFilteredByActive(),r=this.input===document.activeElement,a=this.dropdown.classList.contains(this.config.classNames.activeState),c=this.itemList&&this.itemList.children,l=String.fromCharCode(e.keyCode),h=46,u=8,d=13,f=65,p=27,m=38,g=40,y=33,b=34,E=e.ctrlKey||e.metaKey;this.isTextElement||!/[a-zA-Z0-9-_ ]/.test(l)||a||this.showDropdown(!0),this.canSearch=this.config.searchEnabled;var _=function(){E&&c&&(i.canSearch=!1,i.config.removeItems&&!i.input.value&&i.input===document.activeElement&&i.highlightAll())},S=function(){if(i.isTextElement&&n.value){var t=i.input.value,s=i._canAddItem(o,t);s.response&&(a&&i.hideDropdown(),i._addItem(t),i._triggerChange(t),i.clearInput())}if(n.hasAttribute("data-button")&&(i._handleButtonAction(o,n),e.preventDefault()),a){e.preventDefault();var r=i.dropdown.querySelector("."+i.config.classNames.highlightedState);r&&(o[0]&&(o[0].keyCode=d),i._handleChoiceAction(o,r))}else i.isSelectOneElement&&(a||(i.showDropdown(!0),e.preventDefault()))},I=function(){a&&(i.toggleDropdown(),i.containerOuter.focus())},w=function(){if(a||i.isSelectOneElement){a||i.showDropdown(!0),i.canSearch=!1;var t=e.keyCode===g||e.keyCode===b?1:-1,n=e.metaKey||e.keyCode===b||e.keyCode===y,s=void 0;if(n)s=t>0?Array.from(i.dropdown.querySelectorAll("[data-choice-selectable]")).pop():i.dropdown.querySelector("[data-choice-selectable]");else{var o=i.dropdown.querySelector("."+i.config.classNames.highlightedState);s=o?(0,v.getAdjacentEl)(o,"[data-choice-selectable]",t):i.dropdown.querySelector("[data-choice-selectable]")}s&&((0,v.isScrolledIntoView)(s,i.choiceList,t)||i._scrollToChoice(s,t),i._highlightChoice(s)),e.preventDefault()}},T=function(){!r||e.target.value||i.isSelectOneElement||(i._handleBackspace(o),e.preventDefault())},C=(t={},s(t,f,_),s(t,d,S),s(t,p,I),s(t,m,w),s(t,y,w),s(t,g,w),s(t,b,w),s(t,u,T),s(t,h,T),t);C[e.keyCode]&&C[e.keyCode]()}}},{key:"_onKeyUp",value:function(e){if(e.target===this.input){var t=this.input.value,i=this.store.getItemsFilteredByActive(),n=this._canAddItem(i,t);if(this.isTextElement){var s=this.dropdown.classList.contains(this.config.classNames.activeState);if(t){if(n.notice){var o=this._getTemplate("notice",n.notice);this.dropdown.innerHTML=o.outerHTML}n.response===!0?s||this.showDropdown():!n.notice&&s&&this.hideDropdown()}else s&&this.hideDropdown()}else{var r=46,a=8;e.keyCode!==r&&e.keyCode!==a||e.target.value?this.canSearch&&n.response&&this._handleSearch(this.input.value):!this.isTextElement&&this.isSearching&&(this.isSearching=!1,this.store.dispatch((0,p.activateChoices)(!0)))}this.canSearch=this.config.searchEnabled}}},{key:"_onInput",value:function(){this.isSelectOneElement||this._setInputWidth()}},{key:"_onTouchMove",value:function(){this.wasTap===!0&&(this.wasTap=!1)}},{key:"_onTouchEnd",value:function(e){var t=e.target||e.touches[0].target,i=this.dropdown.classList.contains(this.config.classNames.activeState);this.wasTap===!0&&this.containerOuter.contains(t)&&(t!==this.containerOuter&&t!==this.containerInner||this.isSelectOneElement||(this.isTextElement?document.activeElement!==this.input&&this.input.focus():i||this.showDropdown(!0)),e.stopPropagation()),this.wasTap=!0}},{key:"_onMouseDown",value:function(e){var t=e.target;if(t===this.choiceList&&this.isIe11&&(this.isScrollingOnIe=!0),this.containerOuter.contains(t)&&t!==this.input){var i=void 0,n=this.store.getItemsFilteredByActive(),s=e.shiftKey;(i=(0,v.findAncestorByAttrName)(t,"data-button"))?this._handleButtonAction(n,i):(i=(0,v.findAncestorByAttrName)(t,"data-item"))?this._handleItemAction(n,i,s):(i=(0,v.findAncestorByAttrName)(t,"data-choice"))&&this._handleChoiceAction(n,i),e.preventDefault()}}},{key:"_onClick",value:function(e){var t=e.target,i=this.dropdown.classList.contains(this.config.classNames.activeState),n=this.store.getItemsFilteredByActive();if(this.containerOuter.contains(t))t.hasAttribute("data-button")&&this._handleButtonAction(n,t),i?this.isSelectOneElement&&t!==this.input&&!this.dropdown.contains(t)&&this.hideDropdown(!0):this.isTextElement?document.activeElement!==this.input&&this.input.focus():this.canSearch?this.showDropdown(!0):(this.showDropdown(),this.containerOuter.focus());else{var s=n.some(function(e){return e.highlighted});s&&this.unhighlightAll(),this.containerOuter.classList.remove(this.config.classNames.focusState),i&&this.hideDropdown()}}},{key:"_onMouseOver",value:function(e){(e.target===this.dropdown||this.dropdown.contains(e.target))&&e.target.hasAttribute("data-choice")&&this._highlightChoice(e.target)}},{key:"_onPaste",value:function(e){e.target!==this.input||this.config.paste||e.preventDefault()}},{key:"_onFocus",value:function(e){var t=this,i=e.target;if(this.containerOuter.contains(i)){var n=this.dropdown.classList.contains(this.config.classNames.activeState),s={text:function(){i===t.input&&t.containerOuter.classList.add(t.config.classNames.focusState)},"select-one":function(){t.containerOuter.classList.add(t.config.classNames.focusState),i===t.input&&(n||t.showDropdown())},"select-multiple":function(){i===t.input&&(t.containerOuter.classList.add(t.config.classNames.focusState),n||t.showDropdown(!0))}};s[this.passedElement.type]()}}},{key:"_onBlur",value:function(e){var t=this,i=e.target;if(this.containerOuter.contains(i)&&!this.isScrollingOnIe){var n=this.store.getItemsFilteredByActive(),s=this.dropdown.classList.contains(this.config.classNames.activeState),o=n.some(function(e){return e.highlighted}),r={text:function(){i===t.input&&(t.containerOuter.classList.remove(t.config.classNames.focusState),o&&t.unhighlightAll(),s&&t.hideDropdown())},"select-one":function(){t.containerOuter.classList.remove(t.config.classNames.focusState),i===t.containerOuter&&s&&!t.canSearch&&t.hideDropdown(),i===t.input&&s&&t.hideDropdown()},"select-multiple":function(){i===t.input&&(t.containerOuter.classList.remove(t.config.classNames.focusState),s&&t.hideDropdown(),o&&t.unhighlightAll())}};r[this.passedElement.type]()}else this.isScrollingOnIe=!1,this.input.focus()}},{key:"_regexFilter",value:function(e){if(!e)return!1;var t=this.config.regexFilter,i=new RegExp(t.source,"i");return i.test(e)}},{key:"_scrollToChoice",value:function(e,t){var i=this;if(e){var n=this.choiceList.offsetHeight,s=e.offsetHeight,o=e.offsetTop+s,r=this.choiceList.scrollTop+n,a=t>0?this.choiceList.scrollTop+o-r:e.offsetTop,c=function e(){var n=4,s=i.choiceList.scrollTop,o=!1,r=void 0,c=void 0;t>0?(r=(a-s)/n,c=r>1?r:1,i.choiceList.scrollTop=s+c,s1?r:1,i.choiceList.scrollTop=s-c,s>a&&(o=!0)),o&&requestAnimationFrame(function(i){e(i,a,t)})};requestAnimationFrame(function(e){c(e,a,t)})}}},{key:"_highlightChoice",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,i=Array.from(this.dropdown.querySelectorAll("[data-choice-selectable]")),n=t;if(i&&i.length){var s=Array.from(this.dropdown.querySelectorAll("."+this.config.classNames.highlightedState));s.forEach(function(t){t.classList.remove(e.config.classNames.highlightedState),t.setAttribute("aria-selected","false")}),n?this.highlightPosition=i.indexOf(n):(n=i.length>this.highlightPosition?i[this.highlightPosition]:i[i.length-1],n||(n=i[0])),n.classList.add(this.config.classNames.highlightedState),n.setAttribute("aria-selected","true"),this.containerOuter.setAttribute("aria-activedescendant",n.id)}}},{key:"_addItem",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:-1,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:-1,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null,o=arguments.length>5&&void 0!==arguments[5]&&arguments[5],r=arguments.length>6&&void 0!==arguments[6]?arguments[6]:null,a=(0,v.isType)("String",e)?e.trim():e,c=r,l=this.store.getItems(),h=t||a,u=parseInt(i,10)||-1,d=n>=0?this.store.getGroupById(n):null,f=l?l.length+1:1;return this.config.prependValue&&(a=this.config.prependValue+a.toString()),this.config.appendValue&&(a+=this.config.appendValue.toString()),this.store.dispatch((0,p.addItem)(a,h,f,u,n,s,o,c)), +this.isSelectOneElement&&this.removeActiveItems(f),d&&d.value?(0,v.triggerEvent)(this.passedElement,"addItem",{id:f,value:a,label:h,groupValue:d.value,keyCode:c}):(0,v.triggerEvent)(this.passedElement,"addItem",{id:f,value:a,label:h,keyCode:c}),this}},{key:"_removeItem",value:function(e){if(!e||!(0,v.isType)("Object",e))return this;var t=e.id,i=e.value,n=e.label,s=e.choiceId,o=e.groupId,r=o>=0?this.store.getGroupById(o):null;return this.store.dispatch((0,p.removeItem)(t,s)),r&&r.value?(0,v.triggerEvent)(this.passedElement,"removeItem",{id:t,value:i,label:n,groupValue:r.value}):(0,v.triggerEvent)(this.passedElement,"removeItem",{id:t,value:i,label:n}),this}},{key:"_addChoice",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=arguments.length>3&&void 0!==arguments[3]&&arguments[3],s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:-1,o=arguments.length>5&&void 0!==arguments[5]?arguments[5]:null,r=arguments.length>6&&void 0!==arguments[6]&&arguments[6],a=arguments.length>7&&void 0!==arguments[7]?arguments[7]:null;if("undefined"!=typeof e&&null!==e){var c=this.store.getChoices(),l=t||e,h=c?c.length+1:1,u=this.baseId+"-"+this.idNames.itemChoice+"-"+h;this.store.dispatch((0,p.addChoice)(e,l,h,s,n,u,o,r,a)),i&&this._addItem(e,l,h,void 0,o,r,a)}}},{key:"_clearChoices",value:function(){this.store.dispatch((0,p.clearChoices)())}},{key:"_addGroup",value:function(e,t){var i=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"value",s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"label",o=(0,v.isType)("Object",e)?e.choices:Array.from(e.getElementsByTagName("OPTION")),r=t?t:Math.floor((new Date).valueOf()*Math.random()),a=!!e.disabled&&e.disabled;o?(this.store.dispatch((0,p.addGroup)(e.label,r,!0,a)),o.forEach(function(e){var t=e.disabled||e.parentNode&&e.parentNode.disabled;i._addChoice(e[n],(0,v.isType)("Object",e)?e[s]:e.innerHTML,e.selected,t,r,e.customProperties,e.placeholder)})):this.store.dispatch((0,p.addGroup)(e.label,e.id,!1,e.disabled))}},{key:"_getTemplate",value:function(e){if(!e)return null;for(var t=this.config.templates,i=arguments.length,n=Array(i>1?i-1:0),s=1;s