-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinmarklet.js
339 lines (336 loc) · 18.1 KB
/
pinmarklet.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* author: javiers
Usage:
use the pinterestButton.tag
OR
in the html include a pin button
<a id="pinterest" href="#"><img src="/ps/images/pinterest/PinExt.png"/></a>
attach a click event handler to the button, and use this as the handler logic:
$("#pinterest").click(function (){
UTIL.pinterest.addPinMarklet({images : imgArray});
return false;
})
OR giving a function onPin (for example events for GA
$("#pinterest").click(function (){
UTIL.pinterest.addPinMarklet({
images : imgArray,
onPin : function(){
console.log("onPin event you can execute whatever you need");
}
});
return false;
})
Example:
index.html
*/
var UTIL = UTIL || {};
(function ($) {
"use strict";
UTIL.pinterest = (function () {
var pinmarkletConfig = {
idroot : "PIN",
pin : "http://pinterest.com/pin/create/button/",
minImgSize : 80,
thumbCellSize : 200,
altRegex : {
defaultString : /^picture\.*/
},
msg : {
cancelTitle : "Cancel",
defaultAlt : "I found this on yoursite"
},
pop : "status=no,resizable=no,scrollbars=yes,personalbar=no,directories=no,location=no,toolbar=no,menubar=no,width=632,height=270,left=0,top=0",
rules : [
"#_bg {position:fixed;z-index:8675309; top:0; right:0; bottom:0; left:0; background-color:#f2f2f2; opacity:.95; }",
"#_bd {position: absolute; text-align: center; padding-top: 36px; top: 0; left: 0; right: 0; z-index:8675320; font:16px hevetica neue,arial,san-serif; }",
"#_hd { z-index:2147483643; -moz-box-shadow: 0 1px 2px #aaa; -webkit-box-shadow: 0 1px 2px #aaa; box-shadow: 0 1px 2px #aaa; position: fixed; *position:absolute; width:100%; top: 0; left: 0; right: 0; height: 45px; line-height: 45px; font-size: 14px; font-weight: bold; display: block; margin: 0; background: #fbf7f7; border-bottom: 1px solid #aaa; }",
"#_hd #_logo {height: 43px; width: 100px; display: inline-block; margin-right: -100px; background: transparent url(images/LogoRed.png) 50% 50% no-repeat; border: none;}",
"#_hd a#_x { display: inline-block; cursor: pointer; color: #524D4D; line-height: 45px; text-shadow: 0 1px #fff; float: right; text-align: center; width: 100px; border-left: 1px solid #aaa; }",
"#_hd a#_x:hover { color: #524D4D; background: #e1dfdf; text-decoration: none; }",
"#_hd a#_x:active { color: #fff; background: #cb2027; text-decoration: none; text-shadow:none;}",
"#_bd span { zoom:1; display: inline-block; background: #fff; height:200px; width:200px; border: 1px solid #ddd; border-top: none; border-left:none; text-decoration: none; text-shadow: 0 1px #fff; position: relative; cursor: pointer; vertical-align:middle; }",
"#_bd a img {max-height:100%; max-width:100%; top: 50%; left: 50%; position: absolute; }",
"#_bd span._pinContainer cite, #_bd span._pinContainer cite span { z-index: 1; position: absolute; bottom: 0; left: 0; right: 0; width: 200px; color: #000; height: 22px; line-height: 24px; font-size: 10px; font-style: normal; text-align: center; overflow: hidden; }",
"#_bd span._pinContainer {z-index: 1; height: 200px; width: 200px; background: #fff; }",
"#_bd span._pinButton {z-index: 2; height: 200px; width: 200px; background: transparent url(images/button.png) 50% 50% no-repeat; position:absolute; top:0; left:0; display:none; }",
"#_bd span._pinButton:hover {background-color: rgba(0, 0, 0, 0.5);}" ]
}, pinmarkletPrep = function (win, doc, nav, config) {
var global = win[config.idroot] = {
win : win,
doc : doc,
nav : nav,
config : config,
structure : {},
onPin : undefined,
fns : {
callback : [],
make : function (elements) {
var newElement = false, element, child;
for (element in elements) {
if (elements[element].hasOwnProperty) {
newElement = global.doc.createElement(element);
for (child in elements[element]) {
if (elements[element][child].hasOwnProperty && typeof elements[element][child] === "string") {
newElement[child] = elements[element][child];
}
}
break;
}
}
return newElement;
},
listen : function (tag, event, handle) {
if (global.win.addEventListener !== undefined) {
tag.addEventListener(event, handle, false);
} else if (global.win.attachEvent !== undefined) {
tag.attachEvent("on" + event, handle);
}
},
pin : function (link) {
var img = link.getElementsByTagName("IMG")[0],
params = global.config.pin + "?";
params = params + "media=" + encodeURIComponent(img.src);
params = params + "&url=" + encodeURIComponent(img.getAttribute("url") || global.doc.URL);
params = params + "&title=" + encodeURIComponent(global.doc.title);
params = params + "&description=" + encodeURIComponent(img.title || img.alt);
if ($.isFunction(global.onPin)) {
global.onPin.call();
}
global.win.open(params, "pin", global.config.pop);
},
close : function (msg) {
if (global.structure.bg) {
// remove elements from page
$(global.structure.bg).remove();
$(global.structure.bd).remove();
}
win.hasPinningNow = false;
if (msg) {
global.win.alert(msg);
}
// scroll to the position Y that was before
global.win.scroll(0, global.values.saveScrollTop);
},
click : function (b) {
b = b || global.win.event;
var clicked = b.target ? b.target.nodeType === 3 ? b.target.parentNode : b.target : b.srcElement;
if (clicked) {
// if press the button cancel or in the background close
if (clicked === global.structure.x) {
global.fns.close();
} else if (clicked.parentNode.className === global.config.idroot + "_pinContainer" || clicked.className === global.config.idroot + "_pinButton") {
global.fns.pin(clicked.parentNode.getElementsByTagName("A")[0]);
global.win.setTimeout(function () {
global.fns.close();
}, 10);
} else if ((clicked === global.structure.bd && clicked !== global.structure.hd) || clicked === global.structure.bg) {
global.fns.close();
}
}
},
behavior : function () {
global.fns.listen(global.structure.bd, "click", global.fns.click);
global.fns.listen(global.structure.bg, "click", global.fns.click);
},
presentation : function () {
var cssTag = global.fns.make({
STYLE : {
type : "text/css"
}
}), css = global.config.rules.join("\n").replace(/#_/g, "#" + config.idroot + "_").replace(/\._/g, "." + config.idroot + "_");
if (cssTag.styleSheet) {
cssTag.styleSheet.cssText = css;
} else {
$(cssTag).append(global.doc.createTextNode(css));
}
$('head').append(cssTag);
},
thumb : function (src, height, width, alt, url) {
var container, cite, citeText, link, image;
container = global.fns.make({
SPAN : {
className : global.config.idroot + "_pinContainer"
}
});
link = global.fns.make({
A : {
rel : "image"
}
});
image = new Image();
image.style.visibility = "hidden";
if (alt) {
image.title = alt;
}
if (url) {
image.setAttribute("url", url);
}
image.onload = function () {
global.fns.resizeImages(this);
};
image.src = src;
$(link).append(image);
cite = global.fns.make({
CITE : {}
});
$(cite).append(global.fns.make({
span : {
className : global.config.idroot + "_mask"
}
}));
citeText = height + " x " + width;
citeText = global.fns.make({
span : {
innerHTML : citeText
}
});
$(cite).append(citeText);
$(link).append(cite);
$(container).append(link);
$(container).append(global.fns.make({
SPAN : {
className : global.config.idroot + "_pinButton"
}
}));
// add event on pictures for display button on IE
$("span[class$='_pinContainer']").live("mouseover", function () {
$(this).find("span[class$='_pinButton']").show();
});
$("span[class$='_pinContainer']").live("mouseout", function () {
$(this).find("span[class$='_pinButton']").hide();
});
$(global.structure.bd).append(container);
},
resizeImages : function (img) {
var maxWidth = global.config.thumbCellSize, // Max width for the image
maxHeight = global.config.thumbCellSize, // Max height for the image
width = img.width, // Current image width
height = img.height, // Current image height
ratio;
if (width >= height) {
// Check if the current width is larger than the max
if (width > maxWidth) {
ratio = maxWidth / width; // get ratio for scaling image
img.style.width = maxWidth; // Set new width
img.style.height = height * ratio; // Scale height based on ratio
height = height * ratio; // Reset height to match scaled image
width = maxWidth;
}
} else {
// Check if current height is larger than max
if (height > maxHeight) {
ratio = maxHeight / height; // get ratio for scaling image
img.style.height = maxHeight; // Set new height
img.style.width = width * ratio; // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
height = maxHeight;
}
}
img.style.marginTop = height < global.config.thumbCellSize
? -height / 2 + "px"
: "-" + global.config.thumbCellSize / 2 + "px";
img.style.marginLeft = width < global.config.thumbCellSize
? -width / 2 + "px"
: "-" + global.config.thumbCellSize / 2 + "px";
img.removeAttribute('width');
img.removeAttribute('height');
img.style.visibility = "";
},
structure : function () {
// create DIV opacity
global.structure.bg = global.fns.make({
DIV : {
id : global.config.idroot + "_bg"
}
});
$(global.doc.body).append(global.structure.bg);
// create container
global.structure.bd = global.fns.make({
DIV : {
id : global.config.idroot + "_bd"
}
});
// create header
global.structure.hd = global.fns.make({
DIV : {
id : global.config.idroot + "_hd"
}
});
global.structure.x = global.fns.make({
A : {
id : global.config.idroot + "_x",
innerHTML : global.config.msg.cancelTitle
}
});
global.structure.hd.appendChild(global.fns.make({
SPAN : {
id : global.config.idroot + "_logo"
}
}));
$(global.structure.hd).append(global.structure.x);
$(global.structure.bd).append(global.structure.hd);
$(global.doc.body).append(global.structure.bd);
global.win.scroll(0, 0);
},
checkParamImages : function (img, callback) {
var imgChecksComplete = 0, imgChecksRequested = 0, oneImageCheckComplete = function () {
imgChecksComplete += 1;
if (imgChecksComplete === imgChecksRequested) {
callback();
}
}, max, i;
// load images
for (i = 0, max = img.length; i < max; i += 1) {
imgChecksRequested += 1;
global.fns.loadParamImage(img[i], oneImageCheckComplete);
}
// edge case: nothing to check, so exhibit synchronous behavior
if (imgChecksRequested === 0) {
callback();
}
},
loadParamImage : function (img, callback) {
var imgPin = new Image();
imgPin.onload = function () {
// validate size image
if (imgPin.height > global.config.minImgSize && imgPin.width > global.config.minImgSize) {
global.fns.thumb(imgPin.src, imgPin.height, imgPin.width, imgPin.alt);
}
callback();
};
imgPin.src = img.src;
// assign the alt information for the description on pinterest
if (img.alt.toLowerCase().match(global.config.altRegex.defaultString)) {
imgPin.alt = global.config.msg.defaultAlt;
} else {
imgPin.alt = img.alt;
}
},
init : function (params) {
if (!win.hasPinningNow) {
global.values = {
// save the Y scroll position
saveScrollTop : global.win.pageYOffset
};
global.fns.structure();
global.fns.presentation();
if (params) {
global.onPin = params.onPin;
global.fns.behavior();
global.fns.checkParamImages(params.images,
function () {
win.hasPinningNow = true;
});
}
}
}
}
};
return {
initPinMarklet : global.fns.init
};
};
return {
addPinMarklet : pinmarkletPrep(window, document, navigator, pinmarkletConfig).initPinMarklet
};
}());
}(jQuery));