Module: Compass::SassExtensions::Functions::Sprites

Included in:
Sass::Script::Functions
Defined in:
lib/compass/sass_extensions/functions/sprites.rb

Defined Under Namespace

Modules: VariableReader

Constant Summary collapse

ZERO =
Sass::Script::Number::new(0)
VALID_SELECTORS =
%w(hover active target)

Instance Method Summary collapse

Instance Method Details

#inline_sprite(map) ⇒ Object

Returns the sprite file as an inline image

@include "icon/*.png";
 #{$icon-sprite-base-class} {
   background-image: inline-sprite($icon-sprites);
  }


30
31
32
33
34
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 30

def inline_sprite(map)
  verify_map(map, "sprite-url")
  map.generate
  inline_image(sprite_path(map))
end

#sprite(map, sprite, offset_x = ZERO, offset_y = ZERO) ⇒ Object

Returns the image and background position for use in a single shorthand property:

$icons: sprite-map("icons/*.png"); // contains icons/new.png among others.
background: sprite($icons, new) no-repeat;

Becomes:

background: url('/images/icons.png?12345678') 0 -24px no-repeat;


60
61
62
63
64
65
66
67
68
69
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 60

def sprite(map, sprite, offset_x = ZERO, offset_y = ZERO)
  sprite = convert_sprite_name(sprite)    
  verify_map(map)
  unless sprite.is_a?(Sass::Script::String)
    raise Sass::SyntaxError, %Q(The second argument to sprite() must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
  end
  url = sprite_url(map)
  position = sprite_position(map, sprite, offset_x, offset_y)
  Sass::Script::List.new([url] + position.value, :space)
end

#sprite_does_not_have_parent(map, sprite) ⇒ Object

Returns boolean if sprite has a parent



96
97
98
99
100
101
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 96

def sprite_does_not_have_parent(map, sprite)
  sprite = convert_sprite_name(sprite)
  verify_map map
  verify_sprite sprite
  Sass::Script::Bool.new map.image_for(sprite.value).parent.nil?
end

#sprite_file(map, sprite) ⇒ Object

Returns the path to the original image file for the sprite with the given name



83
84
85
86
87
88
89
90
91
92
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 83

def sprite_file(map, sprite)
  sprite = convert_sprite_name(sprite)
  verify_map(map, "sprite")
  verify_sprite(sprite)
  if image = map.image_for(sprite.value)
    Sass::Script::String.new(image.file)
  else
    missing_image!(map, sprite)
  end
end

#sprite_has_selector(map, sprite, selector) ⇒ Object

Returns boolean if sprite has the selector



106
107
108
109
110
111
112
113
114
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 106

def sprite_has_selector(map, sprite, selector)
  sprite = convert_sprite_name(sprite)
  verify_map map
  verify_sprite sprite
  unless VALID_SELECTORS.include?(selector.value)
    raise Sass::SyntaxError, "Invalid Selctor did you mean one of: #{VALID_SELECTORS.join(', ')}"
  end
  Sass::Script::Bool.new map.send(:"has_#{selector.value}?", sprite.value)
end

#sprite_image(*args) ⇒ Object

Raises:

  • (Sass::SyntaxError)


173
174
175
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 173

def sprite_image(*args)
  raise Sass::SyntaxError, %Q(The sprite-image() function has been replaced by sprite(). See http://compass-style.org/help/tutorials/spriting/ for more information.)
end

#sprite_map(glob, kwargs = {}) ⇒ Object

Creates a Compass::SassExtensions::Sprites::SpriteMap object. A sprite map, when used in a property is the same as calling sprite-url. So the following background properties are equivalent:

$icons: sprite-map("icons/*.png");
background: sprite-url($icons) no-repeat;
background: $icons no-repeat;

The sprite map object will generate the sprite map image, if necessary, the first time it is converted to a url. Simply constructing it has no side-effects.



46
47
48
49
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 46

def sprite_map(glob, kwargs = {})
  kwargs.extend VariableReader
  Compass::SassExtensions::Sprites::SpriteMap.from_uri(glob, self, kwargs)
end

#sprite_map_name(map) ⇒ Object

Returns the name of a sprite map The name is derived from the folder than contains the sprites.



76
77
78
79
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 76

def sprite_map_name(map)
  verify_map(map, "sprite-map-name")
  Sass::Script::String.new(map.name)
end

#sprite_names(map) ⇒ Object

Returns a list of all sprite names



14
15
16
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 14

def sprite_names(map)
  Sass::Script::List.new(map.sprite_names.map { |f| Sass::Script::String.new(f) }, ' ')
end

#sprite_path(map) ⇒ Object

Returns the system path of the sprite file



20
21
22
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 20

def sprite_path(map)
  Sass::Script::String.new(map.name_and_hash)
end

#sprite_position(map, sprite = nil, offset_x = ZERO, offset_y = ZERO) ⇒ Object

Returns the position for the original image in the sprite. This is suitable for use as a value to background-position:

$icons: sprite-map("icons/*.png");
background-position: sprite-position($icons, new);

Might generate something like:

background-position: 0 -34px;

You can adjust the background relative to this position by passing values for ‘$offset-x` and `$offset-y`:

$icons: sprite-map("icons/*.png");
background-position: sprite-position($icons, new, 3px, -2px);

Would change the above output to:

background-position: 3px -36px;


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 146

def sprite_position(map, sprite = nil, offset_x = ZERO, offset_y = ZERO)
  assert_type offset_x, :Number
  assert_type offset_y, :Number
  sprite = convert_sprite_name(sprite)
  verify_map(map, "sprite-position")
  unless sprite && sprite.is_a?(Sass::Script::String)
    raise Sass::SyntaxError, %Q(The second argument to sprite-position must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
  end
  image = map.image_for(sprite.value)
  unless image
    missing_image!(map, sprite)
  end
  if offset_x.unit_str == "%"
    x = offset_x # CE: Shouldn't this be a percentage of the total width?
  else
    x = offset_x.value - image.left
    x = Sass::Script::Number.new(x, x == 0 ? [] : ["px"])
  end
  y = offset_y.value - image.top
  y = Sass::Script::Number.new(y, y == 0 ? [] : ["px"])
  Sass::Script::List.new([x, y],:space)
end

#sprite_url(map) ⇒ Object

Returns a url to the sprite image.



120
121
122
123
124
# File 'lib/compass/sass_extensions/functions/sprites.rb', line 120

def sprite_url(map)
  verify_map(map, "sprite-url")
  map.generate
  generated_image_url(Sass::Script::String.new("#{map.path}-s#{map.uniqueness_hash}.png"))
end