From 0a7c4e83e823e2a8d74c7ac352ae69bfbecfe434 Mon Sep 17 00:00:00 2001 From: Martin Markech Date: Thu, 27 Sep 2012 18:22:25 +0200 Subject: add page tree + jquery tristate checkbox plugin --- app/assets/javascripts/banners/banners.js | 25 ++++ app/assets/javascripts/banners/jquery.tristate.js | 94 +++++++++++++ .../refinery/banners/admin/banners/_form.html.erb | 153 +++++++++++---------- .../refinery/banners/admin/banners/_page.html.erb | 19 +++ 4 files changed, 217 insertions(+), 74 deletions(-) create mode 100644 app/assets/javascripts/banners/banners.js create mode 100644 app/assets/javascripts/banners/jquery.tristate.js create mode 100644 app/views/refinery/banners/admin/banners/_page.html.erb diff --git a/app/assets/javascripts/banners/banners.js b/app/assets/javascripts/banners/banners.js new file mode 100644 index 0000000..9fcea06 --- /dev/null +++ b/app/assets/javascripts/banners/banners.js @@ -0,0 +1,25 @@ +$(function() { + $(document).ready(function() { + $(".tree ul:first > li:first").addClass("branch_start"); + $('.tree ul').find('li:last').addClass("branch_end"); + $('ul.tristate').tristate(); + + $('.tree').delegate('.toggle','click', function(e) { + e.preventDefault(); + + var $li = $(this).parents('li:first'); + var $icon = $li.find('.icon.toggle'); + var $nested = $li.find('.nested'); + + if ($icon.hasClass('expanded')) { + $icon.removeClass('expanded'); + $nested.slideUp(); + } + else { + $icon.addClass('expanded'); + $nested.slideDown(); + } + + }); + }); +}); diff --git a/app/assets/javascripts/banners/jquery.tristate.js b/app/assets/javascripts/banners/jquery.tristate.js new file mode 100644 index 0000000..2d2903d --- /dev/null +++ b/app/assets/javascripts/banners/jquery.tristate.js @@ -0,0 +1,94 @@ +/********************************************************************************** + ** + ** jQuery Tristate Checkbox Plugin + ** version: 1.0 + ** + ** Dual licensed under the MIT and GPL licenses: + ** http://www.opensource.org/licenses/mit-license.php + ** http://www.gnu.org/licenses/gpl.html + ** + ** author: Jeff Leombruno + ** creation date: 09.20.2011 + ** dependencies: jQuery v1.6 or higher + ** + ** This file contains the functionality for implementing 3 state checkboxes. + ** Inspired by: + ** http://code.google.com/p/jquery-tristate-checkbox/ + ** http://css-tricks.com/13467-indeterminate-checkboxes/ + ** + **********************************************************************************/ + +(function($){ + $.fn.tristate = function(options){ + + var config = { + selector: $(this).selector, + checked: null, + container: null, + siblings: null + }; + var opts = $.extend(config, options); + + return this.each(function(){ + var obj = $(this); + + var triState = function() { + + var pub = {}; + + pub.init = function(){ + $('input[type="checkbox"]', obj).change(function(e) { + config.checked = $(this).prop("checked") + config.container = $(this).parent() + config.siblings = config.container.siblings(); + + config.container.find('input[type="checkbox"]').prop({ + indeterminate: false, + checked: config.checked + }); + + pub.checkSiblings(config.container); + }); + // run checkSiblings for every checked checkbox when the page loads + $('input[type=checkbox]:checked', obj).each( function() { + pub.checkSiblings($(this).parent()); + }); + }; + + pub.checkSiblings = function(el) { + var parent = el.parent().parent(); + var all = true; + + el.siblings().each(function() { + return all = ($(this).children('input[type="checkbox"]').prop("checked") === config.checked); + }); + + if (all && config.checked) { + parent.children('input[type="checkbox"]').prop({ + indeterminate: false, + checked: config.checked + }); + pub.checkSiblings(parent); + } else if (all && !config.checked) { + parent.children('input[type="checkbox"]').prop("checked", config.checked); + parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0)); + pub.checkSiblings(parent); + } else { + el.parents("li").children('input[type="checkbox"]').prop({ + indeterminate: true, + checked: false + }); + } + }; + + return pub; + + }(); + + triState.init(); + triState.checkSiblings(obj); + + + }); + }; +})(jQuery); diff --git a/app/views/refinery/banners/admin/banners/_form.html.erb b/app/views/refinery/banners/admin/banners/_form.html.erb index 1403fc4..b249f09 100644 --- a/app/views/refinery/banners/admin/banners/_form.html.erb +++ b/app/views/refinery/banners/admin/banners/_form.html.erb @@ -1,76 +1,81 @@ +<%- content_for(:javascripts) do -%> + <%= javascript_include_tag "banners/jquery.tristate.js", "banners/banners.js" -%> +<%- end -%> + <%= form_for [refinery, :banners_admin, @banner] do |f| -%> - <%= render '/refinery/admin/error_messages', - :object => @banner, - :include_object_name => true %> - - <%= hidden_field_tag 'banner[page_ids][]' %> - -
- <%= f.label :name -%> - <%= f.text_field :name, :class => 'larger widest' -%> - -
- -
- <%= f.label :title -%> - <%= f.text_field :title -%> - -
- -
- <%= f.label :description -%> - <%= f.text_field :description -%> - -
- -
- <%= f.label :image -%> - <%= render '/refinery/admin/image_picker', - :f => f, - :field => :image_id, - :image => @banner.image, - :toggle_image_display => false, - :description => t('refinery.banners.admin.banner') - %> - -
- -
- <%= f.label :url -%> - <%= f.text_field :url -%> - -
- -
- <%= f.label :is_active -%> - <%= f.check_box :is_active, :checked => @banner[:is_active] -%> - -
- -
- <%= f.label :start_date -%> - <%= f.date_select :start_date -%> - -
- -
- <%= f.label :expiry_date -%> - <%= f.date_select :expiry_date -%> - -
- -
- <%= f.label :pages %> - <% Refinery::Page.all.each do |page| %> -
- <%= check_box_tag "banner[page_ids][]", page.id, @banner.pages.include?(page) %> - <%= page.title %> -
- <% end %> -
- - <%= render '/refinery/admin/form_actions', :f => f, - :continue_editing => false, - :delete_title => t('delete', :scope => 'refinery.banners.admin.banners.banner'), - :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @banner.name) %> + <%= render '/refinery/admin/error_messages', + :object => @banner, + :include_object_name => true %> + + <%= hidden_field_tag 'banner[page_ids][]' %> + +
+ <%= f.label :name -%> + <%= f.text_field :name, :class => 'larger widest' -%> + +
+ +
+ <%= f.label :title -%> + <%= f.text_field :title -%> + +
+ +
+ <%= f.label :description -%> + <%= f.text_field :description -%> + +
+ +
+ <%= f.label :image -%> + <%= render '/refinery/admin/image_picker', + :f => f, + :field => :image_id, + :image => @banner.image, + :toggle_image_display => false, + :description => t('refinery.banners.admin.banner') + %> + +
+ +
+ <%= f.label :url -%> + <%= f.text_field :url -%> + +
+ +
+ <%= f.label :is_active -%> + <%= f.check_box :is_active, :checked => @banner[:is_active] -%> + +
+ +
+ <%= f.label :start_date -%> + <%= f.date_select :start_date -%> + +
+ +
+ <%= f.label :expiry_date -%> + <%= f.date_select :expiry_date -%> + +
+ +
+ <%= f.label :pages %> + +
+ + +
+ <%= render '/refinery/admin/form_actions', :f => f, + :continue_editing => false, + :delete_title => t('delete', :scope => 'refinery.banners.admin.banners.banner'), + :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @banner.name) %> <% end -%> + + diff --git a/app/views/refinery/banners/admin/banners/_page.html.erb b/app/views/refinery/banners/admin/banners/_page.html.erb new file mode 100644 index 0000000..640d378 --- /dev/null +++ b/app/views/refinery/banners/admin/banners/_page.html.erb @@ -0,0 +1,19 @@ +
  • + + <% if page.children.present? %> + + <% else %> + + <% end %> + + <%= check_box_tag "banner[page_ids][]", page.id, @banner.pages.include?(page) %> + + '> + <%= page.title %> + + + + +
  • -- cgit v1.2.3 From a07146a6126a087f899318ab563582b3cf7f1760 Mon Sep 17 00:00:00 2001 From: Martin Markech Date: Thu, 27 Sep 2012 22:10:53 +0200 Subject: fix sk locale --- config/locales/sk.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/sk.yml b/config/locales/sk.yml index 05876fd..20d9a05 100644 --- a/config/locales/sk.yml +++ b/config/locales/sk.yml @@ -8,7 +8,7 @@ sk: banner: baner banners: actions: - create_new: Pridať novú stránku + create_new: Pridať nový baner reorder: Zmeniť usporiadanie banerov reorder_done: Uložiť usporiadanie records: -- cgit v1.2.3 From 5225251f0463662c5ccae4332dbd17f90036a760 Mon Sep 17 00:00:00 2001 From: Martin Markech Date: Sun, 30 Sep 2012 19:56:31 +0200 Subject: temp fix for jquery tristate - render only on new action, because on edit it has problems to correctly render some checkboxes --- app/views/refinery/banners/admin/banners/_form.html.erb | 4 ---- app/views/refinery/banners/admin/banners/new.html.erb | 6 ++++++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/views/refinery/banners/admin/banners/_form.html.erb b/app/views/refinery/banners/admin/banners/_form.html.erb index b249f09..3ea7421 100644 --- a/app/views/refinery/banners/admin/banners/_form.html.erb +++ b/app/views/refinery/banners/admin/banners/_form.html.erb @@ -1,7 +1,3 @@ -<%- content_for(:javascripts) do -%> - <%= javascript_include_tag "banners/jquery.tristate.js", "banners/banners.js" -%> -<%- end -%> - <%= form_for [refinery, :banners_admin, @banner] do |f| -%> <%= render '/refinery/admin/error_messages', :object => @banner, diff --git a/app/views/refinery/banners/admin/banners/new.html.erb b/app/views/refinery/banners/admin/banners/new.html.erb index 2d34363..967b41e 100644 --- a/app/views/refinery/banners/admin/banners/new.html.erb +++ b/app/views/refinery/banners/admin/banners/new.html.erb @@ -1 +1,7 @@ +<%# TODO temp fix - activate tristate only on new. There is bug and inproperly render some checkboxes in edit action %> +<%- content_for(:javascripts) do -%> + <%= javascript_include_tag "banners/jquery.tristate.js", "banners/banners.js" -%> +<%- end -%> + <%= render 'form' %> + -- cgit v1.2.3