forked from draeton/stitches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon.js
99 lines (83 loc) · 2.66 KB
/
icon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ## Stitches.Icon
//
// [http://draeton.github.com/stitches](http://draeton.github.com/stitches)
//
// Copyright 2011, Matthew Cobbs
// Licensed under the MIT license.
//
/*global jQuery, Stitches */
(function (window, Stitches) {
"use strict";
// ## Stitches.Icon namespace
//
// Constructs and manages icons
Stitches.Icon = (function () {
/* shortcut */
var S = window.Stitches;
/* Maintain a unique id for each icon */
var guid = 0;
/* Maintains a unique name for each icon */
var nameCache = {};
// ### Icon
//
// Wraps a single icon. Creates a new image from the source
// and sets additional properties after the image loads.
// The callback is generally used to handle queueuing
//
// @param {String} name
// @param {String} src
// @param {Function} cb Optional callback
// @constructor
var Icon = function (name, src, cb) {
var self = this;
this.guid = guid++;
this.name = S.Icon.getName(name);
this.image = new Image();
this.image.onload = function () {
self.x = 0;
self.y = 0;
self.width = self.image.width + S.settings.padding;
self.height = self.image.height + S.settings.padding;
self.area = self.width * self.height;
if (cb) {
cb(self);
}
}
this.image.src = src;
};
// ### Icon.getName
//
// Return a unique name. If the name is already in the nameCache,
// append a value until a unique name is found.
//
// @param {String} name
// @return {String}
Icon.getName = function (name) {
var i = 1, fix;
name = name.replace(/[\s.]+/gi, "-").replace(/[^a-z0-9\-]/gi, "_");
if (nameCache[name]) {
do {
fix = name + "-" + i++;
} while (nameCache[fix]);
name = fix;
}
nameCache[name] = true;
return name;
};
// ### Icon.clearNameCache
//
// Clear the name cache. If a name is passed in, only clear that key
//
// @param {String} name
// @return {String}
Icon.clearNameCache = function (name) {
if (name) {
delete nameCache[name];
} else {
nameCache = {};
}
};
/* return constructor */
return Icon;
})();
})(window, Stitches);