Module: Card::View::Classy

Included in:
Card::View
Defined in:
lib/card/view/classy.rb

Overview

API to change css classes in other places

Instance Method Summary collapse

Instance Method Details

#add_extra_classes(key, classier, scope) ⇒ Object



55
56
57
58
# File 'lib/card/view/classy.rb', line 55

def add_extra_classes key, classier, scope
  list = class_list class_list_type(scope)
  list[key] = [list[key], classier].flatten.compact.join " "
end

#class_down(klass, classier) ⇒ Object



30
31
32
# File 'lib/card/view/classy.rb', line 30

def class_down klass, classier
  remove_extra_classes klass, classier, :private
end

#class_up(klass, classier, scope = :subviews) ⇒ Object

Add additional css classes to a css class

Example

class_up "card-slot", "card-dark text-muted"

If a view later adds the css "card-slot" to a html tag with

  classy("card-slot")

then all additional css classes will be added.

The scope when these additional classes apply can be restricted

Parameters:

  • klass (String, Symbol)

    the css class to be enriched with additional classes

  • classier (String, Array<String>)

    additional css classes

  • scope (Symbol) (defaults to: :subviews)

    :view only in the same view :subviews the same and all subviews; not in nests or where its nested :format all views, sub and parent views; not in nests or where its nested :nests the same as :format but also in nests :single_use the same as :nests but is removed after the first use :global always everywhere



26
27
28
# File 'lib/card/view/classy.rb', line 26

def class_up klass, classier, scope=:subviews
  storage_voo(scope).add_extra_classes klass.to_s, classier, scope
end

#classy(*classes) ⇒ Object



50
51
52
53
# File 'lib/card/view/classy.rb', line 50

def classy *classes
  classes = Array.wrap(classes).flatten
  [classes, extra_classes(classes)].flatten.compact.join " "
end

#deep_extra_classes(klass, space) ⇒ Object

recurse through voos and formats to find all extra classes

Parameters:

  • space (:self, :self_format, :ancestor_format)


87
88
89
90
# File 'lib/card/view/classy.rb', line 87

def deep_extra_classes klass, space
  [self_extra_classes(klass, space),
   ancestor_extra_classes(klass, space)].flatten.compact
end

#extra_classes(klass) ⇒ Object



80
81
82
83
# File 'lib/card/view/classy.rb', line 80

def extra_classes klass
  klass = klass.first if klass.is_a?(Array)
  deep_extra_classes klass.to_s, :self
end

#remove_extra_classes(klass, classier, type) ⇒ Object

remove classes everywhere where they are visible for the given scope



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/card/view/classy.rb', line 61

def remove_extra_classes klass, classier, type
  # TODO: scope handling
  # Method is not used and maybe no longer necessary with the scope feature
  # for class_up.

  # It's no longer sufficient to remove only public classes for ancestors.
  # Needs an approach similar to extra_classes with the "space" argument
  next_ancestor&.remove_extra_classes klass, classier, :public

  cl = class_list type
  return unless cl[klass]

  if cl[klass] == classier
    cl.delete klass
  else
    cl[klass].gsub!(/#{classier}\s?/, "")
  end
end

#with_class_up(klass, classier, scope = :subviews) ⇒ Object



34
35
36
37
38
39
# File 'lib/card/view/classy.rb', line 34

def with_class_up klass, classier, scope=:subviews
  class_up klass, classier, scope
  yield
ensure
  class_down klass, classier
end

#without_upped_class(klass) ⇒ Object

don’t use in the given block the additional class that was added to ‘klass`



43
44
45
46
47
48
# File 'lib/card/view/classy.rb', line 43

def without_upped_class klass
  tmp_class = class_list.delete klass
  result = yield tmp_class
  class_list[klass] = tmp_class
  result
end