|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * Making file/module accessible through the global 'window' under the |
5 |
| - * namespace 'URLForm'. |
6 |
| - * Taken from |
7 |
| - * https://github.com/paulmillr/noble-secp256k1/releases/tag/1.6.3 |
8 |
| - * and |
9 |
| - * https://stackoverflow.com/a/63751410/15147681 |
10 |
| - */ |
11 |
| -(function (global, factory) { |
| 3 | + |
| 4 | +// URLFormJS is used for sticky forms and sharable URL links. See README. |
| 5 | + |
| 6 | + |
| 7 | +// UMD export pattern. See //TODO LINk |
| 8 | +(function(global, factory) { |
12 | 9 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
13 | 10 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
14 | 11 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.URLForm = {}));
|
15 |
| -})(this, (function (exports) { |
| 12 | +})(this, (function(exports) { |
16 | 13 | exports.Init = Init;
|
17 | 14 | exports.PopulateFromValues = PopulateFromValues;
|
18 | 15 | exports.PopulateFromURI = PopulateFromURI;
|
@@ -685,9 +682,8 @@ function shareURI(formOptions) {
|
685 | 682 | }
|
686 | 683 | }
|
687 | 684 |
|
688 |
| - // Inherit default query location if empty, not set, or not a recognized |
689 |
| - // 'QueryLocation'. |
690 |
| - if ((fp.queryLocation === "" || isEmpty(fp.queryLocation) || (fp.queryLocation !== "fragment" || fp.queryLocation !== "query"))) { |
| 685 | + // Inherit default query location if not a recognized 'QueryLocation'. |
| 686 | + if (fp.queryLocation !== "fragment" && fp.queryLocation !== "query") { |
691 | 687 | fp.queryLocation = formOptions.defaultQueryLocation;
|
692 | 688 | }
|
693 | 689 |
|
@@ -715,8 +711,11 @@ function shareURI(formOptions) {
|
715 | 711 | url.searchParams.set(extra, extras.query[extra]);
|
716 | 712 | }
|
717 | 713 | }
|
| 714 | + |
| 715 | + console.log(q); |
718 | 716 | // Rebuild fragment query in case new form fields were set.
|
719 | 717 | url.hash = quagPartsToURLHash(q, extras);
|
| 718 | + console.log(url.href); |
720 | 719 |
|
721 | 720 | // URI Link
|
722 | 721 | let shareUrl = document.querySelector(formOptions.shareURL);
|
@@ -780,50 +779,46 @@ function getExtraParameters(formOptions) {
|
780 | 779 | *
|
781 | 780 | * @param {QuagParts} qp QuagParts
|
782 | 781 | * @param {ExtraParameters} extras ExtraParameters.
|
783 |
| - * @param {FormOptions} formOptions |
| 782 | + * @param {FormOptions} formOptions |
784 | 783 | * @returns {String} Fragment query string (#?...).
|
785 | 784 | */
|
786 | 785 | function quagPartsToURLHash(qp, extras, formOptions) {
|
787 |
| - if (isEmpty(qp.fragmentParts)) { |
| 786 | + if (isEmpty(qp.fragmentPairs)) { |
788 | 787 | return "";
|
789 | 788 | }
|
790 | 789 | let fqs = "#";
|
791 |
| - if (!isEmpty(qp.fragmentParts.before)) { |
792 |
| - fqs += qp.fragmentParts.before; |
793 |
| - } |
| 790 | + // Append anything that was in *fragment* before, but not in, *fragment query*. |
| 791 | + fqs += qp.fragmentParts.before; |
| 792 | + |
| 793 | + // Build the fragment query |
794 | 794 | fqs += "?";
|
795 | 795 | var last = Object.keys(qp.fragmentPairs).length - 1;
|
796 | 796 | var i = 0;
|
797 |
| - var suffix = "&"; |
798 |
| - if (last === 0) { |
799 |
| - suffix = ""; |
800 |
| - } |
| 797 | + |
801 | 798 | for (let key in qp.fragmentPairs) {
|
802 |
| - if (i === (last)) { |
803 |
| - suffix = ""; |
804 |
| - } |
805 | 799 | i++;
|
806 | 800 | if (extras.fragKeys.includes(key)) {
|
807 | 801 | continue;
|
808 | 802 | }
|
809 |
| - fqs += key + "=" + qp.fragmentPairs[key] + suffix; |
| 803 | + fqs += key + "=" + qp.fragmentPairs[key] |
| 804 | + if (i !== 0) { |
| 805 | + fqs += "&"; // Add separator on everything except the last. |
| 806 | + } |
810 | 807 | }
|
811 | 808 |
|
812 | 809 | // Set extras back in query params, if given and 'cleanURL' is false in 'formOptions'.
|
813 | 810 | if (!isEmpty(extras.frag) && !formOptions.cleanURL) {
|
814 |
| - i = 0; |
815 |
| - suffix = "&"; |
816 | 811 | last = extras.fragKeys.length - 1;
|
817 | 812 | for (let extra in extras.frag) {
|
818 |
| - if (i === (last)) { |
819 |
| - suffix = ""; |
820 |
| - } |
821 | 813 | i++;
|
822 |
| - fqs += extra + "=" + extras.frag[extra] + suffix; |
| 814 | + fqs += extra + "=" + extras.frag[extra] |
| 815 | + if (i !== 0) { |
| 816 | + fqs += "&"; |
| 817 | + } |
823 | 818 | }
|
824 | 819 | }
|
825 | 820 |
|
826 |
| - // Append text-fragment. |
| 821 | + // Append anything that was in *fragment* after, but not in, *fragment query*. |
827 | 822 | fqs += qp.fragmentParts.after;
|
828 | 823 | return fqs;
|
829 | 824 | }
|
|
0 commit comments