react-pinterest2
Version:
Collection of embeddable Pinterest buttons and widgets
116 lines (100 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _PinConfig = require('./PinConfig');
var _PinConfig2 = _interopRequireDefault(_PinConfig);
var _PinConst = require('./PinConst');
var _PinConst2 = _interopRequireDefault(_PinConst);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
/**
* A collection of helpful utility functions
*/
exports.default = {
/**
* Utility function for creating a logging request based on parameter data
* @param {object} data - key/value pairs of query parameters to log
*/
log: function log(data) {
var query = '?guid=' + _PinConfig2.default.guid + '&via=' + encodeURIComponent(location.href);
Object.keys(data).forEach(function (key) {
return query += '&' + key + '=' + encodeURIComponent(data[key]);
});
this.loadScript(_PinConst2.default.URL.LOG + query, {});
},
/**
* Utility function for loading a <script>
* @param {string} src - the script src
* @param {object} attributes - attributes to add to the <script>
*/
loadScript: function loadScript(src) {
var attributes = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var script = document.createElement('script');
script.src = src;
Object.keys(attributes).forEach(function (key) {
script[key] = attributes[key];
});
script.onload = function () {
return document.body.removeChild(script);
};
document.body.appendChild(script);
},
/**
* Utility function for loading a script with a jsonp callback
* @param {string} url - the url for the <script> src
* @param {function} callback - the callback to be called on complete
*/
fetch: function fetch(url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
callback(JSON.parse(request.responseText));
}
};
request.send();
},
/**
* Utility function for extending an object with other objects
* @param {object} base - the object to extend
* @param {array} ...args - the objects to do the extending
* @returns {object} <base> extended
*/
extend: function extend(base) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
args.forEach(function (arg) {
if (arg && (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === 'object') {
Object.keys(arg || {}).forEach(function (key) {
base[key] = arg[key];
});
}
});
return base;
},
/**
* Allow for server rendering
* @returns {number} the screen resolution
*/
getResolution: function getResolution() {
if (window) {
return window.devicePixelRatio >= 2 ? 2 : 1;
} else {
return 1;
}
},
/**
* Utility function for autobinding <this> to the function
* @param {array<string>} args - list of function names to bind
*/
bind: function bind(context) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
args.forEach(function (fn) {
return context[fn] = context[fn].bind(context);
});
}
};