lib/tabit/item.rb in tabit-0.4.0 vs lib/tabit/item.rb in tabit-1.0.0

- old
+ new

@@ -1,7 +1,30 @@ -# -*- encoding : utf-8 -*- -module Tabit +# -*- coding: UTF-8 -*- +# +# Copyright (c) 2012-2015 Webhippie <http://www.webhippie.de> +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +class Tabit class Item include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper cattr_accessor :template @@ -9,139 +32,175 @@ attr_accessor :name attr_accessor :url attr_accessor :options attr_accessor :active + attr_accessor :type def initialize(name, url = nil, options = {}) - @name, @url, @options = name, url, options + self.name = name + self.url = url + self.options = options - @active = @options.delete(:active) || configuration.active_detect - @type = @options.delete(:type) || :default - @children = [] - - @options[:inner] ||= {} - @options[:outer] ||= {} + self.active = self.options.delete(:active) || configuration.active_detect + self.type = self.options.delete(:type) || :default end def add(name, url = nil, options = {}) Item.new(name, url, options).tap do |adding| - @children << adding + children.push adding yield adding if block_given? end end def heading(name = nil, options = {}) - options[:class] = '' if options[:class].nil? - options[:class] << " #{configuration.heading_class}" - options[:class].strip! + append_class_to_options configuration.heading_class, options - @children << template.content_tag( + children.push template.content_tag( :li, name, options ) end - def divider(name = nil) - options[:class] = '' if options[:class].nil? - options[:class] << " #{configuration.divider_class}" - options[:class].strip! + def divider(name = nil, options = {}) + append_class_to_options configuration.divider_class, options - @children << template.content_tag( + children.push template.content_tag( :li, name, options ) end def to_s - clazz = if active? - configuration.active_class + configure_item_attrs + + case type + when :dropdown + generate_dropdown_item else - '' + generate_default_item end - - case configuration.active_scope - when :outer - options[:outer][:class] = '' if @options[:outer][:class].nil? - options[:outer][:class] << " #{clazz}" - options[:outer][:class].strip! - when :inner - options[:inner][:class] = '' if @options[:inner][:class].nil? - options[:inner][:class] << " #{clazz}" - options[:inner][:class].strip! - end - - case @type - when :dropdown - options[:outer][:class] = '' if @options[:outer][:class].nil? - options[:outer][:class] << " dropdown" - options[:outer][:class].strip! - - options[:inner][:class] = '' if @options[:inner][:class].nil? - options[:inner][:class] << " dropdown-toggle" - options[:inner][:class].strip! - - options[:inner][:data] ||= {} - options[:inner][:data][:toggle] = 'dropdown' - - template.content_tag( - :li, - [ - template.link_to([name, content_tag(:b, '', :class => 'caret')].join.html_safe, url, options[:inner]), - output - ].compact.join.html_safe, - options[:outer] - ) - else - template.content_tag( - :li, - [ - template.link_to(name, url, options[:inner]), - output - ].compact.join.html_safe, - options[:outer] - ) - end end def active? if template.is_active_link? url, active true else children.each do |child| - if child.active? - return true - end + next if child.is_a? String + return true if child.active? end false end end + def children + @children ||= [] + end + + def options + @options[:inner] ||= {} + @options[:outer] ||= {} + + @options + end + protected - def output - if children.empty? - nil - else - case @type - when :dropdown - template.content_tag( - :ul, - children.collect(&:to_s).join.html_safe, - :class => 'dropdown-menu' - ) - else - template.content_tag( - :ul, - children.collect(&:to_s).join.html_safe - ) - end - end + + def current_active_class + configuration.active_class if active? + end + + def configure_item_attrs + case configuration.active_scope + when :outer + append_class_to_options current_active_class, options[:outer] + when :inner + append_class_to_options current_active_class, options[:inner] end + end - def configuration - Tabit.configuration + def generate_dropdown_item + append_class_to_options "dropdown", options[:outer] + append_class_to_options "dropdown-toggle", options[:inner] + + append_toggle_to_options "dropdown", options[:inner] + + generate_simple_item( + generate_caret_label(name) + ) + end + + def generate_default_item + generate_simple_item( + name + ) + end + + def generate_caret_label(label) + [ + label, + content_tag( + configuration.caret_element, + "", + class: "caret" + ) + ].join(" ").html_safe + end + + def generate_simple_item(label) + template.content_tag( + :li, + [ + template.link_to( + label, + url, + options[:inner] + ), + output + ].compact.join.html_safe, + options[:outer] + ) + end + + def append_class_to_options(value, target = nil) + target ||= options + + target[:class] ||= "" + target[:class] << " #{value}" + target[:class].strip! + end + + def append_toggle_to_options(value, target = nil) + target ||= options + + target[:data] ||= {} + target[:data][:toggle] = value + end + + def output + return if children.empty? + + case type + when :dropdown + list "dropdown-menu" + else + list end + end + + def list(clazz = nil) + template.content_tag( + :ul, + children.collect(&:to_s).join.html_safe, + class: clazz + ) + end + + def configuration + Tabit.configuration + end end end