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



57
58
59
60
61
62
# File 'lib/card/view/classy.rb', line 57

def add_extra_classes key, classier, scope
  type = class_list_type scope

  class_list(type)[key] =
    [class_list(type)[key], classier].flatten.compact.join(" ")
end

#class_down(klass, classier) ⇒ Object



32
33
34
# File 'lib/card/view/classy.rb', line 32

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
29
30
# File 'lib/card/view/classy.rb', line 26

def class_up klass, classier, scope=:subviews
  klass = klass.to_s

  storage_voo(scope).add_extra_classes klass, classier, scope
end

#classy(*classes) ⇒ Object



52
53
54
55
# File 'lib/card/view/classy.rb', line 52

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)


93
94
95
96
# File 'lib/card/view/classy.rb', line 93

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

#extra_classes(klass) ⇒ Object



84
85
86
87
88
89
# File 'lib/card/view/classy.rb', line 84

def extra_classes klass
  klass = klass.first if klass.is_a?(Array)
  klass = klass.to_s

  deep_extra_classes klass, :self
end

#remove_extra_classes(klass, classier, type) ⇒ Object

remove classes everywhere where they are visible for the given scope



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/card/view/classy.rb', line 65

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



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

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



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

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