UNPKG

formiojs

Version:

Common js library for client side interaction with <form.io>

121 lines (113 loc) • 2.73 MB
/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = "./lib/index.js"); /******/ }) /************************************************************************/ /******/ ({ /***/ "./lib/Form.js": /*!*********************!*\ !*** ./lib/Form.js ***! \*********************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _Formio = __webpack_require__(/*! ./Formio */ \"./lib/Formio.js\");\n\nvar _Formio2 = _interopRequireDefault(_Formio);\n\nvar _Wizard = __webpack_require__(/*! ./Wizard */ \"./lib/Wizard.js\");\n\nvar _Wizard2 = _interopRequireDefault(_Wizard);\n\nvar _PDF = __webpack_require__(/*! ./PDF */ \"./lib/PDF.js\");\n\nvar _PDF2 = _interopRequireDefault(_PDF);\n\nvar _Webform = __webpack_require__(/*! ./Webform */ \"./lib/Webform.js\");\n\nvar _Webform2 = _interopRequireDefault(_Webform);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar Form = function () {\n /**\n * Creates an easy to use interface for embedding webforms, pdfs, and wizards into your application.\n *\n * @param {Object} element - The DOM element you wish to render this form within.\n * @param {Object | string} form - Either a Form JSON schema or the URL of a hosted form via. form.io.\n * @param {Object} options - The options to create a new form instance.\n * @param {boolean} options.readOnly - Set this form to readOnly\n * @param {boolean} options.noAlerts - Set to true to disable the alerts dialog.\n * @param {boolean} options.i18n - The translation file for this rendering. @see https://github.com/formio/formio.js/blob/master/i18n.js\n * @param {boolean} options.template - Provides a way to inject custom logic into the creation of every element rendered within the form.\n *\n * @example\n * import Form from 'formiojs/Form';\n * const form = new Form(document.getElementById('formio'), 'https://examples.form.io/example');\n * form.build();\n */\n function Form() {\n var _this = this;\n\n _classCallCheck(this, Form);\n\n this.ready = new Promise(function (resolve, reject) {\n _this.readyResolve = resolve;\n _this.readyReject = reject;\n });\n\n this.instance = null;\n if (arguments[0] instanceof HTMLElement) {\n this.options = arguments[2];\n this.form = arguments[1];\n this.build(arguments[0]);\n } else {\n this.options = arguments[1];\n this.form = arguments[0];\n }\n\n return this.ready;\n }\n\n _createClass(Form, [{\n key: 'create',\n value: function create(display) {\n switch (display) {\n case 'wizard':\n return new _Wizard2.default(this.options);\n case 'pdf':\n return new _PDF2.default(this.options);\n default:\n return new _Webform2.default(this.options);\n }\n }\n }, {\n key: 'setDisplay',\n value: function setDisplay(display) {\n this.form.display = display;\n return this.build();\n }\n }, {\n key: 'empty',\n value: function empty() {\n if (this.element) {\n while (this.element.firstChild) {\n this.element.removeChild(this.element.firstChild);\n }\n }\n }\n }, {\n key: 'build',\n value: function build(element) {\n var _this2 = this;\n\n this.element = element;\n return this.render().then(function (html) {\n _this2.element.innerHTML = html;\n return _this2.attach(_this2.element).then(function () {\n return _this2.instance;\n });\n });\n }\n }, {\n key: 'render',\n value: function render() {\n this.empty();\n return this.ready.then(function (instance) {\n return instance.render();\n });\n }\n }, {\n key: 'attach',\n value: function attach(element) {\n var _this3 = this;\n\n this.element = element;\n return this.ready.then(function (instance) {\n return instance.attach(_this3.element);\n });\n }\n }, {\n key: 'form',\n set: function set(formParam) {\n var _this4 = this;\n\n formParam = formParam || this.form;\n if (typeof formParam === 'string') {\n return new _Formio2.default(formParam).loadForm().then(function (form) {\n _this4.instance = _this4.create(form.display);\n _this4.instance.url = formParam;\n _this4.instance.nosubmit = false;\n _this4.instance.loadSubmission();\n _this4._form = _this4.instance.form = form;\n return _this4.instance.ready.then(function () {\n _this4.readyResolve(_this4.instance);\n return _this4.ready;\n });\n });\n } else {\n if (this.instance) {\n this.instance.destroy();\n }\n this.instance = this.create(formParam.display);\n this._form = this.instance.form = formParam;\n return this.instance.ready.then(function () {\n _this4.readyResolve(_this4.instance);\n return _this4.ready;\n });\n }\n },\n get: function get() {\n return this._form;\n }\n }], [{\n key: 'embed',\n value: function embed(_embed) {\n if (!_embed || !_embed.src) {\n return null;\n }\n var id = this.id || 'formio-' + Math.random().toString(36).substring(7);\n var className = _embed.class || 'formio-form-wrapper';\n var code = _embed.styles ? '<link rel=\"stylesheet\" href=\"' + _embed.styles + '\">' : '';\n code += '<div id=\"' + id + '\" class=\"' + className + '\"></div>';\n document.write(code);\n var formElement = document.getElementById(id);\n return new Form(formElement, _embed.src).build();\n }\n }]);\n\n return Form;\n}();\n\n// Allow simple embedding.\n\n\nexports.default = Form;\n_Formio2.default.embedForm = function (embed) {\n return Form.embed(embed);\n};\n\n/**\n * Creates a new form based on the form parameter.\n *\n * @param element {HMTLElement} - The HTML Element to add this form to.\n * @param form {string|Object} - The src of the form, or a form object.\n * @param options {Object} - The options to create this form.\n *\n * @return {Promise} - When the form is instance is ready.\n */\n_Formio2.default.createForm = function (element, form, options) {\n return new Form(element, form, options);\n};\n\n_Formio2.default.Form = Form;\n\n//# sourceURL=webpack:///./lib/Form.js?"); /***/ }), /***/ "./lib/FormBuilder.js": /*!****************************!*\ !*** ./lib/FormBuilder.js ***! \****************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _Formio = __webpack_require__(/*! ./Formio */ \"./lib/Formio.js\");\n\nvar _Formio2 = _interopRequireDefault(_Formio);\n\nvar _WebformBuilder = __webpack_require__(/*! ./WebformBuilder */ \"./lib/WebformBuilder.js\");\n\nvar _WebformBuilder2 = _interopRequireDefault(_WebformBuilder);\n\nvar _WizardBuilder = __webpack_require__(/*! ./WizardBuilder */ \"./lib/WizardBuilder.js\");\n\nvar _WizardBuilder2 = _interopRequireDefault(_WizardBuilder);\n\nvar _PDFBuilder = __webpack_require__(/*! ./PDFBuilder */ \"./lib/PDFBuilder.js\");\n\nvar _PDFBuilder2 = _interopRequireDefault(_PDFBuilder);\n\nvar _Form2 = __webpack_require__(/*! ./Form */ \"./lib/Form.js\");\n\nvar _Form3 = _interopRequireDefault(_Form2);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return call && (typeof call === \"object\" || typeof call === \"function\") ? call : self; }\n\nfunction _inherits(subClass, superClass) { if (typeof superClass !== \"function\" && superClass !== null) { throw new TypeError(\"Super expression must either be null or a function, not \" + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }\n\nvar FormBuilder = function (_Form) {\n _inherits(FormBuilder, _Form);\n\n /**\n * Creates an easy to use interface for embedding a form builder into your application..\n *\n * @param {Object} element - The DOM element you wish to render this form within.\n * @param {Object | string} form - Either a Form JSON schema or the URL of a hosted form via. form.io.\n * @param {Object} options - The options to create a new form instance.\n * @param {boolean} options.readOnly - Set this form to readOnly\n * @param {boolean} options.noAlerts - Set to true to disable the alerts dialog.\n * @param {boolean} options.i18n - The translation file for this rendering. @see https://github.com/formio/formio.js/blob/master/i18n.js\n * @param {boolean} options.template - Provides a way to inject custom logic into the creation of every element rendered within the form.\n *\n * @example\n * import Form from 'formiojs/FormBuilder';\n * const builder = new FormBuilder(document.getElementById('formio'), {components:[\n * {\n * type: 'textfield',\n * label: 'First Name',\n * key: 'firstName',\n * input: true\n * }\n * ]});\n * builder.render();\n */\n function FormBuilder() {\n var _ref;\n\n _classCallCheck(this, FormBuilder);\n\n for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return _possibleConstructorReturn(this, (_ref = FormBuilder.__proto__ || Object.getPrototypeOf(FormBuilder)).call.apply(_ref, [this].concat(args)));\n }\n\n _createClass(FormBuilder, [{\n key: 'create',\n value: function create(display) {\n if (display === 'wizard') {\n return new _WizardBuilder2.default(this.options);\n } else if (display === 'pdf') {\n return new _PDFBuilder2.default(this.options);\n } else {\n return new _WebformBuilder2.default(this.options);\n }\n }\n }]);\n\n return FormBuilder;\n}(_Form3.default);\n\n/**\n * Creates a new form based on the form parameter.\n *\n * @param element {HMTLElement} - The HTML Element to add this form to.\n * @param form {string|Object} - The src of the form, or a form object.\n * @param options {Object} - The options to create this form.\n *\n * @return {Promise} - When the form is instance is ready.\n */\n\n\nexports.default = FormBuilder;\n_Formio2.default.builder = function (element, form, options) {\n return new FormBuilder(element, form, options);\n};\n\n_Formio2.default.FormBuilder = FormBuilder;\n\n//# sourceURL=webpack:///./lib/FormBuilder.js?"); /***/ }), /***/ "./lib/Formio.js": /*!***********************!*\ !*** ./lib/Formio.js ***! \***********************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { "use strict"; eval("/* WEBPACK VAR INJECTION */(function(global) {\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; /* globals OktaAuth */\n\n// Intentionally use native-promise-only here... Other promise libraries (es6-promise)\n// duck-punch the global Promise definition which messes up Angular 2 since it\n// also duck-punches the global Promise definition. For now, keep native-promise-only.\n\n\nvar _nativePromiseOnly = __webpack_require__(/*! native-promise-only */ \"./node_modules/native-promise-only/lib/npo.src.js\");\n\nvar _nativePromiseOnly2 = _interopRequireDefault(_nativePromiseOnly);\n\n__webpack_require__(/*! whatwg-fetch */ \"./node_modules/whatwg-fetch/fetch.js\");\n\nvar _eventemitter = __webpack_require__(/*! eventemitter2 */ \"./node_modules/eventemitter2/lib/eventemitter2.js\");\n\nvar _browserCookies = __webpack_require__(/*! browser-cookies */ \"./node_modules/browser-cookies/src/browser-cookies.js\");\n\nvar _browserCookies2 = _interopRequireDefault(_browserCookies);\n\nvar _shallowCopy = __webpack_require__(/*! shallow-copy */ \"./node_modules/shallow-copy/index.js\");\n\nvar _shallowCopy2 = _interopRequireDefault(_shallowCopy);\n\nvar _providers = __webpack_require__(/*! ./providers */ \"./lib/providers/index.js\");\n\nvar _providers2 = _interopRequireDefault(_providers);\n\nvar _get2 = __webpack_require__(/*! lodash/get */ \"./node_modules/lodash/get.js\");\n\nvar _get3 = _interopRequireDefault(_get2);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar isBoolean = function isBoolean(val) {\n return (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === _typeof(true);\n};\nvar isNil = function isNil(val) {\n return val === null || val === undefined;\n};\nvar isObject = function isObject(val) {\n return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object';\n};\n\n/**\n * The Formio interface class.\n *\n * let formio = new Formio('https://examples.form.io/example');\n */\n\nvar Formio = function () {\n /* eslint-disable max-statements */\n function Formio(path) {\n var _this = this;\n\n var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};\n\n _classCallCheck(this, Formio);\n\n // Ensure we have an instance of Formio.\n if (!(this instanceof Formio)) {\n return new Formio(path);\n }\n\n // Initialize our variables.\n this.base = '';\n this.projectsUrl = '';\n this.projectUrl = '';\n this.projectId = '';\n this.formUrl = '';\n this.formsUrl = '';\n this.formId = '';\n this.submissionsUrl = '';\n this.submissionUrl = '';\n this.submissionId = '';\n this.actionsUrl = '';\n this.actionId = '';\n this.actionUrl = '';\n this.vsUrl = '';\n this.vId = '';\n this.vUrl = '';\n this.query = '';\n\n if (options.hasOwnProperty('base')) {\n this.base = options.base;\n } else if (Formio.baseUrl) {\n this.base = Formio.baseUrl;\n } else {\n this.base = window.location.href.match(/http[s]?:\\/\\/api./)[0];\n }\n\n if (!path) {\n // Allow user to create new projects if this was instantiated without\n // a url\n this.projectUrl = this.base + '/project';\n this.projectsUrl = this.base + '/project';\n this.projectId = false;\n this.query = '';\n return;\n }\n\n if (options.hasOwnProperty('project')) {\n this.projectUrl = options.project;\n }\n\n var project = this.projectUrl || Formio.projectUrl;\n var projectRegEx = /(^|\\/)(project)($|\\/[^/]+)/;\n var isProjectUrl = path.search(projectRegEx) !== -1;\n\n // The baseURL is the same as the projectUrl, and does not contain \"/project/MONGO_ID\" in\n // its domain. This is almost certainly against the Open Source server.\n if (project && this.base === project && !isProjectUrl) {\n this.noProject = true;\n this.projectUrl = this.base;\n }\n\n // Normalize to an absolute path.\n if (path.indexOf('http') !== 0 && path.indexOf('//') !== 0) {\n path = this.base + path;\n }\n\n var hostparts = this.getUrlParts(path);\n var parts = [];\n var hostName = hostparts[1] + hostparts[2];\n path = hostparts.length > 3 ? hostparts[3] : '';\n var queryparts = path.split('?');\n if (queryparts.length > 1) {\n path = queryparts[0];\n this.query = '?' + queryparts[1];\n }\n\n // Register a specific path.\n var registerPath = function registerPath(name, base) {\n _this[name + 'sUrl'] = base + '/' + name;\n var regex = new RegExp('/' + name + '/([^/]+)');\n if (path.search(regex) !== -1) {\n parts = path.match(regex);\n _this[name + 'Url'] = parts ? base + parts[0] : '';\n _this[name + 'Id'] = parts.length > 1 ? parts[1] : '';\n base += parts[0];\n }\n return base;\n };\n\n // Register an array of items.\n var registerItems = function registerItems(items, base, staticBase) {\n for (var i in items) {\n if (items.hasOwnProperty(i)) {\n var item = items[i];\n if (Array.isArray(item)) {\n registerItems(item, base, true);\n } else {\n var newBase = registerPath(item, base);\n base = staticBase ? base : newBase;\n }\n }\n }\n };\n\n if (!this.projectUrl || this.projectUrl === this.base) {\n this.projectUrl = hostName;\n }\n\n if (!this.noProject) {\n // Determine the projectUrl and projectId\n if (isProjectUrl) {\n // Get project id as project/:projectId.\n registerItems(['project'], hostName);\n path = path.replace(projectRegEx, '');\n } else if (hostName === this.base) {\n // Get project id as first part of path (subdirectory).\n if (hostparts.length > 3 && path.split('/').length > 1) {\n var pathParts = path.split('/');\n pathParts.shift(); // Throw away the first /.\n this.projectId = pathParts.shift();\n path = '/' + pathParts.join('/');\n this.projectUrl = hostName + '/' + this.projectId;\n }\n } else {\n // Get project id from subdomain.\n if (hostparts.length > 2 && (hostparts[2].split('.').length > 2 || hostName.indexOf('localhost') !== -1)) {\n this.projectUrl = hostName;\n this.projectId = hostparts[2].split('.')[0];\n }\n }\n this.projectsUrl = this.projectsUrl || this.base + '/project';\n }\n\n // Configure Form urls and form ids.\n if (path.search(/(^|\\/)(form)($|\\/)/) !== -1) {\n registerItems(['form', ['submission', 'action', 'v']], this.projectUrl);\n } else {\n var subRegEx = new RegExp('/(submission|action|v)($|/.*)');\n var subs = path.match(subRegEx);\n this.pathType = subs && subs.length > 1 ? subs[1] : '';\n path = path.replace(subRegEx, '');\n path = path.replace(/\\/$/, '');\n this.formsUrl = this.projectUrl + '/form';\n this.formUrl = path ? this.projectUrl + path : '';\n this.formId = path.replace(/^\\/+|\\/+$/g, '');\n var items = ['submission', 'action', 'v'];\n for (var i in items) {\n if (items.hasOwnProperty(i)) {\n var item = items[i];\n this[item + 'sUrl'] = this.projectUrl + path + '/' + item;\n if (this.pathType === item && subs.length > 2 && subs[2]) {\n this[item + 'Id'] = subs[2].replace(/^\\/+|\\/+$/g, '');\n this[item + 'Url'] = this.projectUrl + path + subs[0];\n }\n }\n }\n }\n\n // Set the app url if it is not set.\n if (!Formio.projectUrlSet) {\n Formio.projectUrl = this.projectUrl;\n }\n }\n /* eslint-enable max-statements */\n\n _createClass(Formio, [{\n key: 'delete',\n value: function _delete(type, opts) {\n var _id = type + 'Id';\n var _url = type + 'Url';\n if (!this[_id]) {\n _nativePromiseOnly2.default.reject('Nothing to delete');\n }\n Formio.cache = {};\n return this.makeRequest(type, this[_url], 'delete', null, opts);\n }\n }, {\n key: 'index',\n value: function index(type, query, opts) {\n var _url = type + 'Url';\n query = query || '';\n if (query && isObject(query)) {\n query = '?' + Formio.serialize(query.params);\n }\n return this.makeRequest(type, this[_url] + query, 'get', null, opts);\n }\n }, {\n key: 'save',\n value: function save(type, data, opts) {\n var _id = type + 'Id';\n var _url = type + 'Url';\n var method = this[_id] || data._id ? 'put' : 'post';\n var reqUrl = this[_id] ? this[_url] : this[type + 'sUrl'];\n if (!this[_id] && data._id && method === 'put' && !(reqUrl.indexOf(data._id) !== -1)) {\n reqUrl += '/' + data._id;\n }\n Formio.cache = {};\n return this.makeRequest(type, reqUrl + this.query, method, data, opts);\n }\n }, {\n key: 'load',\n value: function load(type, query, opts) {\n var _id = type + 'Id';\n var _url = type + 'Url';\n if (query && isObject(query)) {\n query = Formio.serialize(query.params);\n }\n if (query) {\n query = this.query ? this.query + '&' + query : '?' + query;\n } else {\n query = this.query;\n }\n if (!this[_id]) {\n return _nativePromiseOnly2.default.reject('Missing ' + _id);\n }\n return this.makeRequest(type, this[_url] + query, 'get', null, opts);\n }\n }, {\n key: 'makeRequest',\n value: function makeRequest() {\n for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return Formio.makeRequest.apply(Formio, [this].concat(args));\n }\n }, {\n key: 'loadProject',\n value: function loadProject(query, opts) {\n return this.load('project', query, opts);\n }\n }, {\n key: 'saveProject',\n value: function saveProject(data, opts) {\n return this.save('project', data, opts);\n }\n }, {\n key: 'deleteProject',\n value: function deleteProject(opts) {\n return this.delete('project', opts);\n }\n }, {\n key: 'loadForm',\n value: function loadForm(query, opts) {\n var _this2 = this;\n\n return this.load('form', query, opts).then(function (currentForm) {\n // Check to see if there isn't a number in vId.\n if (!currentForm.revisions || isNaN(parseInt(_this2.vId))) {\n return currentForm;\n }\n // If a submission already exists but form is marked to load current version of form.\n if (currentForm.revisions === 'current' && _this2.submissionId) {\n return currentForm;\n }\n // If they specified a revision form, load the revised form components.\n if (query && isObject(query)) {\n query = Formio.serialize(query.params);\n }\n if (query) {\n query = _this2.query ? _this2.query + '&' + query : '?' + query;\n } else {\n query = _this2.query;\n }\n return _this2.makeRequest('form', _this2.vUrl + query, 'get', null, opts).then(function (revisionForm) {\n currentForm.components = revisionForm.components;\n // Using object.assign so we don't cross polinate multiple form loads.\n return Object.assign({}, currentForm);\n })\n // If we couldn't load the revision, just return the original form.\n .catch(function () {\n return Object.assign({}, currentForm);\n });\n });\n }\n }, {\n key: 'saveForm',\n value: function saveForm(data, opts) {\n return this.save('form', data, opts);\n }\n }, {\n key: 'deleteForm',\n value: function deleteForm(opts) {\n return this.delete('form', opts);\n }\n }, {\n key: 'loadForms',\n value: function loadForms(query, opts) {\n return this.index('forms', query, opts);\n }\n }, {\n key: 'loadSubmission',\n value: function loadSubmission(query, opts) {\n var _this3 = this;\n\n return this.load('submission', query, opts).then(function (submission) {\n _this3.vId = submission._fvid;\n _this3.vUrl = _this3.formUrl + '/v/' + _this3.vId;\n return submission;\n });\n }\n }, {\n key: 'saveSubmission',\n value: function saveSubmission(data, opts) {\n if (!isNaN(parseInt(this.vId))) {\n data._fvid = this.vId;\n }\n return this.save('submission', data, opts);\n }\n }, {\n key: 'deleteSubmission',\n value: function deleteSubmission(opts) {\n return this.delete('submission', opts);\n }\n }, {\n key: 'loadSubmissions',\n value: function loadSubmissions(query, opts) {\n return this.index('submissions', query, opts);\n }\n }, {\n key: 'loadAction',\n value: function loadAction(query, opts) {\n return this.load('action', query, opts);\n }\n }, {\n key: 'saveAction',\n value: function saveAction(data, opts) {\n return this.save('action', data, opts);\n }\n }, {\n key: 'deleteAction',\n value: function deleteAction(opts) {\n return this.delete('action', opts);\n }\n }, {\n key: 'loadActions',\n value: function loadActions(query, opts) {\n return this.index('actions', query, opts);\n }\n }, {\n key: 'availableActions',\n value: function availableActions() {\n return this.makeRequest('availableActions', this.formUrl + '/actions');\n }\n }, {\n key: 'actionInfo',\n value: function actionInfo(name) {\n return this.makeRequest('actionInfo', this.formUrl + '/actions/' + name);\n }\n }, {\n key: 'isObjectId',\n value: function isObjectId(id) {\n var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');\n return checkForHexRegExp.test(id);\n }\n }, {\n key: 'getProjectId',\n value: function getProjectId() {\n if (!this.projectId) {\n return _nativePromiseOnly2.default.resolve('');\n }\n if (this.isObjectId(this.projectId)) {\n return _nativePromiseOnly2.default.resolve(this.projectId);\n } else {\n return this.loadProject().then(function (project) {\n return project._id;\n });\n }\n }\n }, {\n key: 'getFormId',\n value: function getFormId() {\n if (!this.formId) {\n return _nativePromiseOnly2.default.resolve('');\n }\n if (this.isObjectId(this.formId)) {\n return _nativePromiseOnly2.default.resolve(this.formId);\n } else {\n return this.loadForm().then(function (form) {\n return form._id;\n });\n }\n }\n }, {\n key: 'currentUser',\n value: function currentUser(options) {\n return Formio.currentUser(this, options);\n }\n }, {\n key: 'accessInfo',\n value: function accessInfo() {\n return Formio.accessInfo(this);\n }\n\n /**\n * Returns the JWT token for this instance.\n *\n * @return {*}\n */\n\n }, {\n key: 'getToken',\n value: function getToken() {\n return Formio.getToken();\n }\n\n /**\n * Returns a temporary authentication token for single purpose token generation.\n */\n\n }, {\n key: 'getTempToken',\n value: function getTempToken(expire, allowed) {\n var token = Formio.getToken();\n if (!token) {\n return _nativePromiseOnly2.default.reject('You must be authenticated to generate a temporary auth token.');\n }\n return this.makeRequest('tempToken', this.projectUrl + '/token', 'GET', null, {\n ignoreCache: true,\n header: new Headers({\n 'x-expire': expire,\n 'x-allow': allowed\n })\n });\n }\n\n /**\n * Get a download url for a submission PDF of this submission.\n *\n * @return {*}\n */\n\n }, {\n key: 'getDownloadUrl',\n value: function getDownloadUrl(form) {\n var _this4 = this;\n\n if (!this.submissionId) {\n return _nativePromiseOnly2.default.resolve('');\n }\n\n if (!form) {\n // Make sure to load the form first.\n return this.loadForm().then(function (_form) {\n if (!_form) {\n return '';\n }\n return _this4.getDownloadUrl(_form);\n });\n }\n\n var apiUrl = '/project/' + form.project;\n apiUrl += '/form/' + form._id;\n apiUrl += '/submission/' + this.submissionId;\n apiUrl += '/download';\n\n var download = this.base + apiUrl;\n return new _nativePromiseOnly2.default(function (resolve, reject) {\n _this4.getTempToken(3600, 'GET:' + apiUrl).then(function (tempToken) {\n download += '?token=' + tempToken.key;\n resolve(download);\n }, function () {\n resolve(download);\n }).catch(reject);\n });\n }\n }, {\n key: 'uploadFile',\n value: function uploadFile(storage, file, fileName, dir, progressCallback, url) {\n var _this5 = this;\n\n var requestArgs = {\n provider: storage,\n method: 'upload',\n file: file,\n fileName: fileName,\n dir: dir\n };\n var request = Formio.pluginWait('preRequest', requestArgs).then(function () {\n return Formio.pluginGet('fileRequest', requestArgs).then(function (result) {\n if (storage && isNil(result)) {\n if (Formio.providers.storage.hasOwnProperty(storage)) {\n var provider = new Formio.providers.storage[storage](_this5);\n return provider.uploadFile(file, fileName, dir, progressCallback, url);\n } else {\n throw 'Storage provider not found';\n }\n }\n return result || { url: '' };\n });\n });\n\n return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs);\n }\n }, {\n key: 'downloadFile',\n value: function downloadFile(file) {\n var _this6 = this;\n\n var requestArgs = {\n method: 'download',\n file: file\n };\n\n var request = Formio.pluginWait('preRequest', requestArgs).then(function () {\n return Formio.pluginGet('fileRequest', requestArgs).then(function (result) {\n if (file.storage && isNil(result)) {\n if (Formio.providers.storage.hasOwnProperty(file.storage)) {\n var provider = new Formio.providers.storage[file.storage](_this6);\n return provider.downloadFile(file);\n } else {\n throw 'Storage provider not found';\n }\n }\n return result || { url: '' };\n });\n });\n\n return Formio.pluginAlter('wrapFileRequestPromise', request, requestArgs);\n }\n\n // Determine if the user can submit the form.\n\n }, {\n key: 'canSubmit',\n value: function canSubmit() {\n /* eslint-disable max-statements, max-depth */\n return _nativePromiseOnly2.default.all([this.loadForm(), this.currentUser(), this.accessInfo()]).then(function (results) {\n var form = results.shift();\n var user = results.shift();\n var access = results.shift();\n\n // Get the anonymous and admin roles.\n var anonRole = {};\n var adminRole = {};\n for (var roleName in access.roles) {\n if (access.roles.hasOwnProperty(roleName)) {\n var role = access.roles[roleName];\n if (role.default) {\n anonRole = role;\n }\n if (role.admin) {\n adminRole = role;\n }\n }\n }\n\n var canSubmit = false;\n var canSubmitAnonymously = false;\n\n // If the user is an admin, then they can submit this form.\n if (user && user.roles.indexOf(adminRole._id) !== -1) {\n return true;\n }\n\n for (var i in form.submissionAccess) {\n if (form.submissionAccess.hasOwnProperty(i)) {\n var subRole = form.submissionAccess[i];\n if (subRole.type === 'create_all' || subRole.type === 'create_own') {\n for (var j in subRole.roles) {\n if (subRole.roles.hasOwnProperty(j)) {\n // Check if anonymous is allowed.\n if (anonRole._id === subRole.roles[j]) {\n canSubmitAnonymously = true;\n }\n // Check if the logged in user has the appropriate role.\n if (user && user.roles.indexOf(subRole.roles[j]) !== -1) {\n canSubmit = true;\n break;\n }\n }\n }\n if (canSubmit) {\n break;\n }\n }\n }\n }\n // If their user cannot submit, but anonymous can, then delete token and allow submission.\n if (!canSubmit && canSubmitAnonymously) {\n canSubmit = true;\n Formio.setUser(null);\n }\n return canSubmit;\n });\n /* eslint-enable max-statements, max-depth */\n }\n }, {\n key: 'getUrlParts',\n value: function getUrlParts(url) {\n return Formio.getUrlParts(url, this);\n }\n }], [{\n key: 'loadProjects',\n value: function loadProjects(query, opts) {\n query = query || '';\n if (isObject(query)) {\n query = '?' + Formio.serialize(query.params);\n }\n return Formio.makeStaticRequest(Formio.baseUrl + '/project' + query, 'GET', null, opts);\n }\n }, {\n key: 'getUrlParts',\n value: function getUrlParts(url, formio) {\n var base = formio && formio.base ? formio.base : Formio.baseUrl;\n var regex = '^(http[s]?:\\\\/\\\\/)';\n if (base && url.indexOf(base) === 0) {\n regex += '(' + base.replace(/^http[s]?:\\/\\//, '') + ')';\n } else {\n regex += '([^/]+)';\n }\n regex += '($|\\\\/.*)';\n return url.match(new RegExp(regex));\n }\n }, {\n key: 'serialize',\n value: function serialize(obj) {\n var str = [];\n for (var p in obj) {\n if (obj.hasOwnProperty(p)) {\n str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));\n }\n }\n return str.join('&');\n }\n }, {\n key: 'getRequestArgs',\n value: function getRequestArgs(formio, type, url, method, data, opts) {\n method = (method || 'GET').toUpperCase();\n if (!opts || !isObject(opts)) {\n opts = {};\n }\n\n var requestArgs = {\n url: url,\n method: method,\n data: data || null,\n opts: opts\n };\n\n if (type) {\n requestArgs.type = type;\n }\n\n if (formio) {\n requestArgs.formio = formio;\n }\n return requestArgs;\n }\n }, {\n key: 'makeStaticRequest',\n value: function makeStaticRequest(url, method, data, opts) {\n var requestArgs = Formio.getRequestArgs(null, '', url, method, data, opts);\n var request = Formio.pluginWait('preRequest', requestArgs).then(function () {\n return Formio.pluginGet('staticRequest', requestArgs).then(function (result) {\n if (isNil(result)) {\n return Formio.request(url, method, requestArgs.data, requestArgs.opts.header, requestArgs.opts);\n }\n return result;\n });\n });\n\n return Formio.pluginAlter('wrapStaticRequestPromise', request, requestArgs);\n }\n }, {\n key: 'makeRequest',\n value: function makeRequest(formio, type, url, method, data, opts) {\n if (!formio) {\n return Formio.makeStaticRequest(url, method, data, opts);\n }\n\n var requestArgs = Formio.getRequestArgs(formio, type, url, method, data, opts);\n var request = Formio.pluginWait('preRequest', requestArgs).then(function () {\n return Formio.pluginGet('request', requestArgs).then(function (result) {\n if (isNil(result)) {\n return Formio.request(url, method, requestArgs.data, requestArgs.opts.header, requestArgs.opts);\n }\n return result;\n });\n });\n\n return Formio.pluginAlter('wrapRequestPromise', request, requestArgs);\n }\n }, {\n key: 'request',\n value: function request(url, method, data, header, opts) {\n if (!url) {\n return _nativePromiseOnly2.default.reject('No url provided');\n }\n method = (method || 'GET').toUpperCase();\n\n // For reverse compatibility, if they provided the ignoreCache parameter,\n // then change it back to the options format where that is a parameter.\n if (isBoolean(opts)) {\n opts = { ignoreCache: opts };\n }\n if (!opts || !isObject(opts)) {\n opts = {};\n }\n\n // Generate a cachekey.\n var cacheKey = btoa(url);\n\n // Get the cached promise to save multiple loads.\n if (!opts.ignoreCache && method === 'GET' && Formio.cache.hasOwnProperty(cacheKey)) {\n return _nativePromiseOnly2.default.resolve(Formio.cache[cacheKey]);\n }\n\n // Set up and fetch request\n var headers = header || new Headers(opts.headers || {\n 'Accept': 'application/json',\n 'Content-type': 'application/json; charset=UTF-8'\n });\n var token = Formio.getToken();\n if (token && !opts.noToken) {\n headers.append('x-jwt-token', token);\n }\n\n var options = {\n method: method,\n headers: headers,\n mode: 'cors'\n };\n if (data) {\n options.body = JSON.stringify(data);\n }\n\n // Allow plugins to alter the options.\n options = Formio.pluginAlter('requestOptions', options, url);\n\n var requestToken = options.headers.get('x-jwt-token');\n return fetch(url, options).then(function (response) {\n // Allow plugins to respond.\n response = Formio.pluginAlter('requestResponse', response, Formio);\n\n if (!response.ok) {\n if (response.status === 440) {\n Formio.setToken(null);\n Formio.events.emit('formio.sessionExpired', response.body);\n } else if (response.status === 401) {\n Formio.events.emit('formio.unauthorized', response.body);\n }\n // Parse and return the error as a rejected promise to reject this promise\n return (response.headers.get('content-type').indexOf('application/json') !== -1 ? response.json() : response.text()).then(function (error) {\n return _nativePromiseOnly2.default.reject(error);\n });\n }\n\n // Handle fetch results\n var token = response.headers.get('x-jwt-token');\n\n // In some strange cases, the fetch library will return an x-jwt-token without sending\n // one to the server. This has even been debugged on the server to verify that no token\n // was introduced with the request, but the response contains a token. This is an Invalid\n // case where we do not send an x-jwt-token and get one in return for any GET request.\n var tokenIntroduced = false;\n if (method === 'GET' && !requestToken && token && !opts.external && !(url.indexOf('token=') !== -1) && !(url.indexOf('x-jwt-token=') !== -1)) {\n console.warn('Token was introduced in request.');\n tokenIntroduced = true;\n }\n\n if (response.status >= 200 && response.status < 300 && token && token !== '' && !tokenIntroduced) {\n Formio.setToken(token);\n }\n // 204 is no content. Don't try to .json() it.\n if (response.status === 204) {\n return {};\n }\n\n var getResult = response.headers.get('content-type').indexOf('application/json') !== -1 ? response.json() : response.text();\n return getResult.then(function (result) {\n // Add some content-range metadata to the result here\n var range = response.headers.get('content-range');\n if (range && isObject(result)) {\n range = range.split('/');\n if (range[0] !== '*') {\n var skipLimit = range[0].split('-');\n result.skip = Number(skipLimit[0]);\n result.limit = skipLimit[1] - skipLimit[0] + 1;\n }\n result.serverCount = range[1] === '*' ? range[1] : Number(range[1]);\n }\n\n if (!opts.getHeaders) {\n return result;\n }\n\n var headers = {};\n response.headers.forEach(function (item, key) {\n headers[key] = item;\n });\n\n // Return the result with the headers.\n return {\n result: result,\n headers: headers\n };\n });\n }).then(function (result) {\n if (opts.getHeaders) {\n return result;\n }\n\n var resultCopy = {};\n\n // Shallow copy result so modifications don't end up in cache\n if (Array.isArray(result)) {\n resultCopy = result.map(_shallowCopy2.default);\n resultCopy.skip = result.skip;\n resultCopy.limit = result.limit;\n resultCopy.serverCount = result.serverCount;\n } else {\n resultCopy = (0, _shallowCopy2.default)(result);\n }\n\n // Cache the response.\n if (method === 'GET') {\n Formio.cache[cacheKey] = resultCopy;\n }\n\n return resultCopy;\n }).catch(function (err) {\n if (err === 'Bad Token') {\n Formio.setToken(null);\n Formio.events.emit('formio.badToken', err);\n }\n if (err.message) {\n err.message = 'Could not connect to API server (' + err.message + ')';\n err.networkError = true;\n }\n return _nativePromiseOnly2.default.reject(err);\n });\n }\n }, {\n key: 'setToken',\n value: function setToken(token) {\n token = token || '';\n if (token === this.token) {\n return;\n }\n this.token = token;\n if (!token) {\n Formio.setUser(null);\n // iOS in private browse mode will throw an error but we can't detect ahead of time that we are in private mode.\n try {\n return localStorage.removeItem('formioToken');\n } catch (err) {\n return _browserCookies2.default.erase('formioToken', { path: '/' });\n }\n }\n // iOS in private browse mode will throw an error but we can't detect ahead of time that we are in private mode.\n try {\n localStorage.setItem('formioToken', token);\n } catch (err) {\n _browserCookies2.default.set('formioToken', token, { path: '/' });\n }\n return Formio.currentUser(); // Run this so user is updated if null\n }\n }, {\n key: 'getToken',\n value: function getToken() {\n if (this.token) {\n return this.token;\n }\n try {\n this.token = localStorage.getItem('formioToken') || '';\n return this.token;\n } catch (e) {\n this.token = _browserCookies2.default.get('formioToken');\n return this.token;\n }\n }\n }, {\n key: 'setUser',\n value: function setUser(user) {\n if (!user) {\n this.setToken(null);\n // iOS in private browse mode will throw an error but we can't detect ahead of time that we are in private mode.\n try {\n return localStorage.removeItem('formioUser');\n } catch (err) {\n return _browserCookies2.default.erase('formioUser', { path: '/' });\n }\n }\n // iOS in private browse mode will throw an error but we can't detect ahead of time that we are in private mode.\n try {\n localStorage.setItem('formioUser', JSON.stringify(user));\n } catch (err) {\n _browserCookies2.default.set('formioUser', JSON.stringify(user), { path: '/' });\n }\n }\n }, {\n key: 'getUser',\n value: function getUser() {\n try {\n return JSON.parse(localStorage.getItem('formioUser') || null);\n } catch (e) {\n return JSON.parse(_browserCookies2.default.get('formioUser'));\n }\n }\n }, {\n key: 'setBaseUrl',\n value: function setBaseUrl(url) {\n Formio.baseUrl = url;\n if (!Formio.projectUrlSet) {\n Formio.projectUrl = url;\n }\n }\n }, {\n key: 'getBaseUrl',\n value: function getBaseUrl() {\n return Formio.baseUrl;\n }\n }, {\n key: 'setApiUrl',\n value: function setApiUrl(url) {\n return Formio.setBaseUrl(url);\n }\n }, {\n key: 'getApiUrl',\n value: function getApiUrl() {\n return Formio.getBaseUrl();\n }\n }, {\n key: 'setAppUrl',\n value: function setAppUrl(url) {\n console.warn('Formio.setAppUrl() is deprecate