|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
- /*jslint browser: true, continue: true, eqeq: true, plusplus: true, vars: true, white: true */
- var FlattrLoader = function() {
- "use strict";
- var a = {
- instance: !1,
- queryString: !1,
- validParams: ["mode", "https", "uid", "category", "button", "language", "html5-key-prefix", "popout", "revsharekey"],
- validButtonParams: ["uid", "owner", "category", "button", "language", "hidden", "tags", "title", "url", "description", "revsharekey", "popout"],
- options: {},
- POPOUT_WIDTH: 401,
- POPOUT_HEIGHT: 230,
- TIMEOUT: 1500,
- createIframe: function(b) {
- var c = b.button == "compact",
- d = document.createElement("iframe");
- return d.setAttribute("src", (this.getParam("https") == 1 ? "https": "http") + "://" + this.getParam("domain", "api.flattr.com") + "/button/view/?" + this.encodeData(b)),
- d.setAttribute("class", "FlattrButton"),
- d.setAttribute("width", c == 1 ? 110 : 55),
- d.setAttribute("height", c == 1 ? 20 : 62),
- d.setAttribute("frameBorder", 0),
- d.setAttribute("scrolling", "no"),
- d.setAttribute("title", "Flattr"),
- d.setAttribute("border", 0),
- d.setAttribute("marginHeight", 0),
- d.setAttribute("marginWidth", 0),
- d.setAttribute("allowTransparency", "true"),
- d.data = b,
- b.popout != 0 && (d.onmouseover = function() {
- this.popoutIframe === undefined && (a.removeAllOpenPopoutIframes(), a.showPopoutForButton(this), this.popoutIframe.onmouseover = function() {
- this.timeout && (clearTimeout(this.timeout), this.timeout = undefined)
- },
- this.popoutIframe.onmouseout = function() {
- this.parentNode && (this.timeout = setTimeout(function() {
- d.popoutIframe && a.removePopoutForButton(d)
- },
- a.TIMEOUT))
- })
- },
- d.onmouseout = function() {
- this.popoutIframe && (this.popoutIframe.timeout = setTimeout(function() {
- d.popoutIframe && a.removePopoutForButton(d)
- },
- a.TIMEOUT))
- }),
- d
- },
- getAbsolutePositionForElement: function(a) {
- var b = {
- x: 0,
- y: 0
- };
- if (a.offsetParent) do b.x += a.offsetLeft,
- b.y += a.offsetTop,
- a = a.offsetParent;
- while (a);
- return b
- },
- showPopoutForButton: function(a) {
- var b, c = "s",
- d = "e",
- e = window.innerWidth !== undefined ? window.innerWidth: document.documentElement.clientWidth,
- f = window.innerHeight !== undefined ? window.innerHeight: document.documentElement.clientHeight,
- g = this.getAbsolutePositionForElement(a);
- g.x > e / 2 && (d = "w"),
- g.y + Number(a.height) + this.POPOUT_HEIGHT > f && (c = "n"),
- b = c + d,
- a.data.dir = b,
- a.popoutIframe = this.createPopoutIframe(a.data),
- d === "w" ? a.popoutIframe.style.left = Number(g.x) - Number(this.POPOUT_WIDTH) + Number(a.width) + "px": d === "e" && (a.popoutIframe.style.left = g.x + "px"),
- c === "n" ? a.popoutIframe.style.top = Number(g.y) - Number(this.POPOUT_HEIGHT) + "px": c === "s" && (a.popoutIframe.style.top = Number(g.y) + Number(a.height) + "px"),
- document.querySelector("body").appendChild(a.popoutIframe)
- },
- createPopoutIframe: function(a) {
- var b = document.createElement("iframe");
- return b.setAttribute("src", (this.getParam("https") == 1 ? "https": "http") + "://" + this.getParam("domain", "api.flattr.com") + "/button/popout/?" + this.encodeData(a)),
- b.setAttribute("frameBorder", 0),
- b.setAttribute("allowTransparency", "true"),
- b.setAttribute("style", "position: absolute; display:block; z-index: 9999;"),
- b.setAttribute("width", this.POPOUT_WIDTH),
- b.setAttribute("height", this.POPOUT_HEIGHT),
- b
- },
- removePopoutForButton: function(a) {
- a.popoutIframe.timeout && clearTimeout(a.popoutIframe.timeout),
- a.popoutIframe.parentNode.removeChild(a.popoutIframe),
- a.popoutIframe = undefined
- },
- removeAllOpenPopoutIframes: function() {
- var a = document.querySelectorAll("iframe.FlattrButton"),
- b,
- c;
- for (b = 0; b < a.length; b += 1) c = a[b],
- c.popoutIframe && this.removePopoutForButton(c)
- },
- reshowAllOpenPopoutIframes: function() {
- var a = document.querySelectorAll("iframe.FlattrButton"),
- b,
- c;
- for (b = 0; b < a.length; b += 1) c = a[b],
- c.popoutIframe && (this.removePopoutForButton(c), this.showPopoutForButton(c))
- },
- encodeData: function(a) {
- var b, c, d = "";
- for (b in a) a.hasOwnProperty(b) && (c = a[b], b == "description" && (c = this.stripTags(c, "<br>"), c.length > 1e3 && (c = c.substring(0, 1e3))), c = c.replace(/^\s+|\s+$/g, "").replace(/\s{2,}|\t+/g, " "), d += b + "=" + encodeURIComponent(c) + "&");
- return d
- },
- getParam: function(a, b) {
- return typeof this.options[a] != "undefined" ? this.options[a] : b
- },
- init: function() {
- var b, c, d, e, f, g, h, i, j, k, l, m, n = document.getElementsByTagName("script");
- try {
- for (b = n.length - 1; b >= 0; b--) {
- c = n[b];
- if (!c.hasAttribute("src")) continue;
- d = c.src,
- e = new RegExp("^(http(?:s?))://(api\\.(?:.*\\.?)flattr\\.(?:com|dev))", "i"),
- f = d.match(e);
- if (f) {
- this.options.domain = f[2].toString(),
- this.options.https = f[1].toString() == "https" ? 1 : 0,
- g = d.indexOf("?");
- if (g) {
- h = d.substring(++g),
- i = h.split("&");
- for (k = 0; k < i.length; k++) j = i[k].split("="),
- this.validParam(j[0], this.validParams) && (this.options[j[0]] = j[1])
- }
- this.instance = c;
- break
- }
- }
- } catch(o) {}
- window.addEventListener !== undefined ? (l = window.addEventListener, m = "message") : (l = window.attachEvent, m = "onmessage"),
- l(m,
- function(b) {
- var c;
- try {
- c = JSON.parse(b.data)
- } catch(d) {
- c = {}
- }
- c.flattr_button_event === "popout_close_button_clicked" ? a.removeAllOpenPopoutIframes() : c.flattr_button_event === "click_successful" && a.reshowAllOpenPopoutIframes()
- },
- !1);
- switch (this.getParam("mode", "manual")) {
- case "direct":
- this.render();
- break;
- case "auto":
- case "automatic":
- var p = this;
- this.domReady(function() {
- p.setup()
- });
- break;
- case "manual":
- default:
- }
- return this
- },
- loadButton: function(a) {
- var b, c, d, e, f, g = {},
- h = null;
- for (b in this.options) this.options.hasOwnProperty(b) && this.validParam(b, this.validButtonParams) && (g[b] = this.options[b]);
- a.href && (g.url = a.href),
- a.getAttribute("title") && (g.title = a.getAttribute("title")),
- a.getAttribute("lang") && (g.language = a.getAttribute("lang")),
- a.innerHTML && (g.description = a.innerHTML);
- if ((h = a.getAttribute("rev")) !== null && h.substring(0, 6) == "flattr" || (h = a.getAttribute("rel")) !== null && h.substring(0, 6) == "flattr") {
- h = h.substring(7).split(";");
- for (c = 0; c < h.length; c++) d = h[c].split(":"),
- e = d.shift(),
- this.validParam(e, this.validButtonParams) && (g[e] = d.join(":"))
- } else for (f in this.validButtonParams) this.validButtonParams.hasOwnProperty(f) && (h = a.getAttribute(this.getParam("html5-key-prefix", "data-flattr") + "-" + this.validButtonParams[f])) !== null && (g[this.validButtonParams[f]] = h);
- this.replaceWith(a, this.createIframe(g))
- },
- render: function(a, b, c) {
- var d, e = {};
复制代码 |
|