UNPKG

intelia-jquery-plugin

Version:

Intelia client for product recommendation and loyalty programms

117 lines (112 loc) 5.22 kB
"use strict"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } /*! * intelia-jquery-plugin 1.0.0 * Intelia client for product recommendation and loyalty programms * * Created by Intelia * * @license MIT */ (function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if ((typeof module === "undefined" ? "undefined" : _typeof(module)) === 'object' && module.exports) { // Node/CommonJS module.exports = function (root, jQuery) { if (jQuery === undefined) { // require('jQuery') returns a factory that requires window to // build a jQuery instance, we normalize how we use modules // that require this pattern but the window provided is a noop // if it's defined (how jquery works) if (typeof window !== 'undefined') { jQuery = require('jquery'); } else { jQuery = require('jquery')(root); } } factory(jQuery); return jQuery; }; } else { // Browser globals factory(jQuery); } })(function ($) { "use strict"; // Default Options var defaults = { // TODO: ADD YOUR DEFAULT OPTIONS HERE block: 0, host: 'https://cdp.plataformaintelia.com', successCallback: null, current_product: null, customer: null }; var InteliaPlugin = /*#__PURE__*/function () { function InteliaPlugin(element, options) { _classCallCheck(this, InteliaPlugin); _defineProperty(this, "\xE5", void 0); // Merge user settings with default this.options = $.extend(true, {}, defaults, options); // Main container element this.main = $(element); // Initial load this._init(); } // Initial Method _createClass(InteliaPlugin, [{ key: "_init", value: function _init() { // Plugin init and logic console.log(this.options); this.getRecommendations(); } }, { key: "getRecommendations", value: function getRecommendations() { var settings = { "url": this.options.host + "/api/v2/related_blocks/" + this.options.block + "/results", "method": "GET", "timeout": 0, "headers": { "Authorization": "Bearer " + this.options.apikey } }; $.ajax(settings).done(function (response) { //console.log(response); // aquí debería de ejecutarse un callback self.options.successCallback(response); }); } }]); return InteliaPlugin; }(); // Wrapper for the plugin $.fn.inteliaPlugin = function (options) { var pluginName = "inteliaPlugin"; if (options === undefined || _typeof(options) === 'object') { return this.each(function () { if (!$.data(this, pluginName)) { $.data(this, pluginName, new InteliaPlugin(this, options)); } }); } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { var instance = $.data(this[0], pluginName); if (options === 'destroy') { $.data(this, pluginName, null); } if (instance instanceof InteliaPlugin && typeof instance[options] === 'function') { return instance[options].apply(instance, Array.prototype.slice.call(arguments, 1)); } else { return this; } } }; });