From 81a6569fe797590bbcee4db67f8f10196ecee1e5 Mon Sep 17 00:00:00 2001 From: did Date: Thu, 22 Dec 2011 02:59:30 +0100 Subject: [PATCH] UI compoment to add options for the select type field (wip) --- Gemfile.lock | 2 +- .../locomotive/models/custom_field.js.coffee | 6 ++++++ .../models/custom_field_select_option.js.coffee | 13 +++++++++++++ .../stylesheets/locomotive/editable_elements.scss | 1 + app/helpers/locomotive/custom_fields_helper.rb | 10 +++++----- app/inputs/locomotive/empty_input.rb | 8 ++++++++ app/views/locomotive/custom_fields/_form.html.haml | 4 ++++ config/locales/formtastic.en.yml | 4 +++- doc/TODO | 4 ++-- lib/locomotive/mongoid/patches.rb | 2 +- 10 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 app/assets/javascripts/locomotive/models/custom_field_select_option.js.coffee diff --git a/Gemfile.lock b/Gemfile.lock index 76113e6f..7376dc2d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,7 @@ GIT GIT remote: git://github.com/locomotivecms/custom_fields.git - revision: 8bf202d6e0294ca2050ecd7ec44dcc4d570cb7fd + revision: 1a8f26a379193c5a60f926eacadc0a9eee7ab5de branch: experimental specs: custom_fields (2.0.0.rc1) diff --git a/app/assets/javascripts/locomotive/models/custom_field.js.coffee b/app/assets/javascripts/locomotive/models/custom_field.js.coffee index 6daf8990..0d5226bf 100644 --- a/app/assets/javascripts/locomotive/models/custom_field.js.coffee +++ b/app/assets/javascripts/locomotive/models/custom_field.js.coffee @@ -1,9 +1,15 @@ class Locomotive.Models.CustomField extends Backbone.Model initialize: -> + @_normalize() + unless @get('name')? @set name: @get('label').slugify() + _normalize: -> + @set + select_options: new Locomotive.Models.CustomFieldSelectOptionsCollection(@get('select_options')) + toJSONForSave: -> _.tap {}, (hash) => for key, value of @.toJSON() diff --git a/app/assets/javascripts/locomotive/models/custom_field_select_option.js.coffee b/app/assets/javascripts/locomotive/models/custom_field_select_option.js.coffee new file mode 100644 index 00000000..05695eb3 --- /dev/null +++ b/app/assets/javascripts/locomotive/models/custom_field_select_option.js.coffee @@ -0,0 +1,13 @@ +class Locomotive.Models.CustomFieldSelectOption extends Backbone.Model + + toJSONForSave: -> + _.tap {}, (hash) => + for key, value of @.toJSON() + hash[key] = value unless _.include(['created_at', 'updated_at'], key) + +class Locomotive.Models.CustomFieldSelectOptionsCollection extends Backbone.Collection + + model: Locomotive.Models.CustomFieldSelectOption + + toJSONForSave: -> + @map (model) => model.toJSONForSave() diff --git a/app/assets/stylesheets/locomotive/editable_elements.scss b/app/assets/stylesheets/locomotive/editable_elements.scss index c0be65a8..5d418207 100644 --- a/app/assets/stylesheets/locomotive/editable_elements.scss +++ b/app/assets/stylesheets/locomotive/editable_elements.scss @@ -64,6 +64,7 @@ clear: both; ol { + min-height: 400px; li.file { span.file { diff --git a/app/helpers/locomotive/custom_fields_helper.rb b/app/helpers/locomotive/custom_fields_helper.rb index 0239c976..f429c3eb 100644 --- a/app/helpers/locomotive/custom_fields_helper.rb +++ b/app/helpers/locomotive/custom_fields_helper.rb @@ -35,11 +35,11 @@ module Locomotive::CustomFieldsHelper end end - # def options_for_text_formatting - # options = %w(none html).map do |option| - # [t("locomotive.custom_fields.text_formatting.#{option}"), option] - # end - # end + def options_for_text_formatting + %w(none html).map do |option| + [t("locomotive.custom_fields.text_formatting.#{option}"), option] + end + end # def options_for_association_target # current_site.reload.content_types.collect { |c| [c.name, c.content_klass.to_s] } diff --git a/app/inputs/locomotive/empty_input.rb b/app/inputs/locomotive/empty_input.rb index 9bb12071..eb875f1c 100644 --- a/app/inputs/locomotive/empty_input.rb +++ b/app/inputs/locomotive/empty_input.rb @@ -9,6 +9,14 @@ module Locomotive end end + def association_primary_key + begin + super + rescue Exception => e + # does not work correctly with embedded collections + end + end + def wrapper_html_options super.tap do |opts| opts[:class] += ' no-label' unless render_label? diff --git a/app/views/locomotive/custom_fields/_form.html.haml b/app/views/locomotive/custom_fields/_form.html.haml index 09ce85dc..da34be16 100644 --- a/app/views/locomotive/custom_fields/_form.html.haml +++ b/app/views/locomotive/custom_fields/_form.html.haml @@ -40,6 +40,10 @@ = g.input :hint, :input_html => { :class => 'hint' } + = g.input :select_options, :as => :'Locomotive::Empty', :wrapper_html => { :style => 'display: none' } + + = g.input :text_formatting, :as => :select, :collection => options_for_text_formatting, :include_blank => false, :wrapper_html => { :style => 'display: none' } + %span.actions = link_to 'toggle', '#', :class => 'toggle' = link_to 'x', '#', :class => 'remove', :confirm => t('locomotive.messages.confirm') \ No newline at end of file diff --git a/config/locales/formtastic.en.yml b/config/locales/formtastic.en.yml index 160c955e..9f837ffb 100644 --- a/config/locales/formtastic.en.yml +++ b/config/locales/formtastic.en.yml @@ -41,7 +41,9 @@ en: default_site_template: "Use the default site template. Click here to upload a site template as a zip file instead." content_type: raw_item_template: Item template - api_accounts: Notified Accounts + public_form_accounts: Notified Accounts + "custom_fields/field": + select_options: "Options" content_instance: _slug: Permalink account: diff --git a/doc/TODO b/doc/TODO index 4a6b9460..591dd374 100644 --- a/doc/TODO +++ b/doc/TODO @@ -45,10 +45,10 @@ x edit my site x display errors x slugify x refactor highlighted_field (field id instead of name) - - other content type options + x other content type options - show / hide options of a field based on its type - - select: add/edit/remove options - text: formatting + - select: add/edit/remove options - change in main menu - manage contents - list diff --git a/lib/locomotive/mongoid/patches.rb b/lib/locomotive/mongoid/patches.rb index 0a4baf37..c7189539 100644 --- a/lib/locomotive/mongoid/patches.rb +++ b/lib/locomotive/mongoid/patches.rb @@ -4,7 +4,7 @@ require 'mongoid' module Mongoid module Document - def as_json(options={}) + def as_json(options = {}) attrs = super(options) attrs["id"] = attrs["_id"] attrs