From 1dd2e4f316268024baa7ce79b0f468ec49e4e716 Mon Sep 17 00:00:00 2001 From: Anna Sidwell Date: Thu, 12 Apr 2018 17:00:02 +1000 Subject: [PATCH] Add the formsaver module tied into nice UI for data saving (#52) --- apps/map/static/map/plugins/FormSaver.js | 269 ++++++++++++++++++ .../map/plugins/jquery.dirtyforms.min.js | 7 + apps/map/templates/map/form.html | 130 ++++++++- 3 files changed, 402 insertions(+), 4 deletions(-) create mode 100644 apps/map/static/map/plugins/FormSaver.js create mode 100644 apps/map/static/map/plugins/jquery.dirtyforms.min.js diff --git a/apps/map/static/map/plugins/FormSaver.js b/apps/map/static/map/plugins/FormSaver.js new file mode 100644 index 0000000..27814a0 --- /dev/null +++ b/apps/map/static/map/plugins/FormSaver.js @@ -0,0 +1,269 @@ +"use strict"; + +var _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; }; + +var _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; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var FormSaverSerialisers = { + "input:submit": function inputSubmit() { + return null; + }, + "input:button": function inputButton() { + return null; + }, + "input:image": function inputImage() { + return null; + }, + "input:reset": function inputReset() { + return null; + }, + "input:file": function inputFile() { + return null; + }, + + "input:radio": function inputRadio(group) { + return group.filter(function (input) { + return input.checked; + }).map(function (input) { + return input.value; + }); + }, + + "input:checkbox": "input:radio", + + "select": function select(group) { + return group.length == 1 ? Array.from(group[0].selectedOptions).map(function (elem) { + return elem.value; + }) : new Error('More than one select wth the same name'); + }, + + "textarea": function textarea(group) { + return group.length == 1 ? group[0].value : new Error('More than one textarea with the same name'); + }, + + "*": function _(group) { + return group.length == 1 ? group[0].value : new Error('More than one input with the same name'); + } +}; + +var FormSaver = function () { + function FormSaver(_ref) { + var formId = _ref.formId, + except = _ref.except; + + _classCallCheck(this, FormSaver); + + this.formId = formId; + this.skipList = except || []; + } + + _createClass(FormSaver, [{ + key: "getExtendedTagName", + value: function getExtendedTagName(element) { + var tag = element.tagName.toLowerCase(); + if (tag == 'input') { + return 'input:' + element.type; + } else { + return tag; + } + } + }, { + key: "deserialise", + value: function deserialise(data) { + var form = document.getElementById(this.formId); + + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var field = _step.value; + + var element = form.elements[field.name]; + + // Skip some fields + if (this.skipList.includes(field.name)) { + continue; + } + + // Deal with the case where we have a single tag first, + // ie. input type=text, selects, textareas + if (element.tagName) { + var tagName = this.getExtendedTagName(element); + + if (tagName == 'select') { + // Go over all the