From 3aa9af3fb50765b935e29ae796ef659b164d8d81 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 18 Jun 2018 17:58:44 -0300 Subject: [PATCH] los campos multiples pueden ser cerrados tuvimos que volver a select2 4.0.5 porque hay un bug que impide procesar los data-* --- app/models/post/template_field.rb | 21 +++++++++++++++++++++ app/views/posts/_form.haml | 6 +++++- config/locales/en.yml | 3 +++ config/locales/es.yml | 3 +++ doc/plantillas.md | 1 + package.json | 2 +- yarn.lock | 6 +++--- 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index 89b20cdb..b1708402 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -83,6 +83,27 @@ class Post array? end + # Detecta si el valor es una tabla de campos + def nested? + value.is_a? Hash + end + + # Un campo acepta valores abiertos si no es un array con múltiples + # elementos + def open? + # Todos los valores simples son abiertos + return true unless complex? + return false unless array? + # La cosa se complejiza cuando tenemos valores complejos + # + # Si tenemos una lista cerrada de valores, necesitamos saber si el + # campo es abierto o cerrado. Si la lista tiene varios elementos, + # es una lista cerrada, opcionalmente abierta. Si la lista tiene + # un elemento, quiere decir que estamos autocompletando desde otro + # lado. + contents.fetch('open', value.count < 2) + end + # Obtiene los valores posibles para el campo de la plantilla def values return '' if %w[string text].include? value diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 06d2d7d5..cbe0539d 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -95,7 +95,11 @@ - when 'select' = select_tag name, options_for_select(template.values, @post.get_front_matter(template)), { class: 'form-control select2', - multiple: template.multiple? } + multiple: template.multiple?, + data: { tags: template.open?, + placeholder: t('posts.select.placeholder')}} + - if template.open? + %small.text-muted.form-text= t('posts.open') .invalid-feedback= t('posts.invalid') .form-group = submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post' diff --git a/config/locales/en.yml b/config/locales/en.yml index 0b259f7e..92c1bab9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -203,3 +203,6 @@ en: title: 'Post needs a title' date: 'Post needs a valid date' invalid: 'This field is required!' + open: 'Tip: You can add new options by typing them' + select: + placeholder: 'Type to filter available options' diff --git a/config/locales/es.yml b/config/locales/es.yml index d05e8e8f..fa44325c 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -199,3 +199,6 @@ es: title: 'Necesita un título' date: 'Necesita una fecha' invalid: '¡Este campo es obligatorio!' + open: 'Nota: Puedes agregar más opciones a medida que las escribes' + select: + placeholder: 'Empieza a escribir para filtrar las opciones disponibles' diff --git a/doc/plantillas.md b/doc/plantillas.md index 073289ac..01308167 100644 --- a/doc/plantillas.md +++ b/doc/plantillas.md @@ -207,4 +207,5 @@ required: false min: nil max: nil multiple: true +open: true ``` diff --git a/package.json b/package.json index 8fffbf0f..6de28d74 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "markdown-it": "^8.4.0", "marked": "^0.3.12", "popper.js": "^1.12.9", - "select2": "^4.0.6-rc.1", + "select2": "4.0.5", "select2-bootstrap-theme": "https://github.com/angel-vladov/select2-bootstrap-theme", "table-dragger": "^1.0.2" } diff --git a/yarn.lock b/yarn.lock index 5b14f8c4..93ef77d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -137,9 +137,9 @@ rx@^4.1.0: version "0.1.0-beta.10" resolved "https://github.com/angel-vladov/select2-bootstrap-theme#4fb604838e79150161fc636dbeb3b3022c034cda" -select2@^4.0.6-rc.1: - version "4.0.6-rc.1" - resolved "https://registry.yarnpkg.com/select2/-/select2-4.0.6-rc.1.tgz#aa6c3038a7f0f2e91ffade38f0a21c15e1813276" +select2@4.0.5: + version "4.0.5" + resolved "https://registry.yarnpkg.com/select2/-/select2-4.0.5.tgz#7aac50692561985b34d3b82ec55e226f8960d40a" dependencies: almond "~0.3.1" jquery-mousewheel "~3.1.13"