1
1
// URLFormJS is used for sticky forms and sharable URL links. See README.
2
2
'use strict' ;
3
3
4
-
5
-
6
-
7
4
/**
8
5
* FormParameter are the options for a form's field/parameter.
9
6
*
15
12
* "funcTrue": ()=> ToggleVisible(document.querySelector("#advancedOptions"));
16
13
* }
17
14
*
18
- * - name: Parameter name in the URI. Is used as the default value for
15
+ * - name Parameter name in the URI. Is used as the default value for
19
16
* id.
20
17
*
21
- * - id: Id of the html element if it differs from the name. Example,
22
- * URI parameter "retrieve" and html id "Retrieve"
18
+ * - id Id of the HTML element if it differs from the name. Example,
19
+ * URI parameter "retrieve" and HTML id "Retrieve".
23
20
*
24
- * - type: Type of the parameter (bool/string/number). Defaults to
21
+ * - type Type of the parameter (bool/string/number). Defaults to
25
22
* string. For 'bool', if the parameter is present in the URL
26
23
* and has a function set in 'funcTrue', the function will be
27
24
* executed. (e.g. https://localhost/?send_news_and_updates)
28
25
* using the example above will execute the 'ToggleVisible'
29
26
* function.
30
27
*
31
- * - func: Called if set on each call to SetForm
28
+ * - func Called if set on each call to SetForm
32
29
* (Populate and PopulateFromValues).
33
30
*
34
- * - funcTrue: Execute if param is true. e.g. `"funcTrue": ()
35
- * => {
31
+ * - funcTrue Execute if param is true. e.g. `"funcTrue": () => {
36
32
* ToggleVisible(document.querySelector("#advancedOptions"))};`
37
33
*
38
- * - queryLocation: Option for overriding the param in the URL link to either
34
+ * - queryLocation Option for overriding the param in the URL link to either
39
35
* be a query parameter, or a fragment query. Defaults to empty
40
36
* string, which will inherit the 'defaultQueryLocation' from
41
37
* the form wide options.
42
38
*
43
- * - saveSetting: Save and use this setting from local storage. Will be
39
+ * - saveSetting (Bool) Save and use this setting from local storage. Will be
44
40
* overwritten by URL flag values if present.
45
41
* @typedef {Object } FormParameter
46
42
* @property {String } name
97
93
* - clearBtn: Button element for clearing the form and
98
94
* queries/fragments in the URL.
99
95
*
100
- * - shareURLBtn: Button element triggers generating share link.
101
- *
102
96
* - shareURL: Element ID of <a> for share link.
103
97
*
98
+ * - shareURLBtn: Button element triggers generating share link.
99
+ *
104
100
* - shareURLArea: Element ID of text area for share link.
105
101
*
106
102
* - defaultQueryLocation: Link sets parameter in query or fragment query.
@@ -586,7 +582,7 @@ function ShareURI(formOptions) {
586
582
var u = new URL ( window . location . origin + window . location . pathname ) ;
587
583
588
584
for ( let fp of formOptions . FormParameters ) {
589
- let value = formPairs [ fp . name ] ;
585
+ let value = encodeURIComponent ( formPairs [ fp . name ] ) ;
590
586
// console.log(fp, value);
591
587
if ( isEmpty ( value ) ) {
592
588
// Sets value if populated. Otherwise removes from the query/fragment. (A
@@ -661,7 +657,7 @@ function quagPartsToURLHash(fragment, formOptions) {
661
657
fqs += "?" ; //start fragment query delimiter ("?")
662
658
for ( let key in fragment . pairs ) {
663
659
i -- ;
664
- fqs += key + "=" + fragment . pairs [ key ]
660
+ fqs += key + "=" + encodeURIComponent ( fragment . pairs [ key ] ) ;
665
661
if ( i > 0 ) {
666
662
fqs += "&" ; // Add separator on everything except the last.
667
663
}
@@ -677,7 +673,7 @@ function quagPartsToURLHash(fragment, formOptions) {
677
673
if ( j > 0 && ! formOptions . cleanURL ) {
678
674
for ( let e in fragment . extras ) {
679
675
j -- ;
680
- fqs += e + "=" + fragment . extras [ e ]
676
+ fqs += e + "=" + encodeURIComponent ( fragment . extras [ e ] ) ;
681
677
if ( j > 0 ) {
682
678
fqs += "&" ;
683
679
}
@@ -686,12 +682,12 @@ function quagPartsToURLHash(fragment, formOptions) {
686
682
687
683
// After.
688
684
fqs += fragment . after ;
689
- return encodeURIComponent ( fqs ) ;
685
+ return fqs ;
690
686
}
691
687
692
688
693
689
/**
694
- * Returns from `key=value` string a `key:value` object .
690
+ * Helper that returns a `k:v,k:v` object from a `k=v&k=v` string .
695
691
*
696
692
* @param {String } s e.g. `key=value&key=value`.
697
693
* @returns {QuagPairs } {key:value}
@@ -730,6 +726,8 @@ function getPairs(s) {
730
726
* form, and puts values into the correct object based on formOptions.
731
727
* Includes extras. See docs on `QuagParts`.
732
728
*
729
+ * On duplicate the default behavior overwrites query pairs with fragment pairs.
730
+ *
733
731
* @param {FormOptions } formOptions
734
732
* @returns {QuagParts }
735
733
*/
@@ -813,8 +811,9 @@ function GetQuagParts(formOptions) {
813
811
}
814
812
815
813
/**
816
- * GetURLKeyValue is a helper func that returns the key:value pairs from the
817
- * URL. Default behavior overwrites query pairs with fragment pairs.
814
+ * GetURLKeyValue is a helper that returns k:v from the current URL.
815
+ *
816
+ * On duplicate the default behavior overwrites query pairs with fragment pairs.
818
817
*
819
818
* @param {FormOptions } formOptions
820
819
* @returns {QuagParts.pairs }
@@ -853,7 +852,7 @@ function GetForm(formOptions, ReturnPairOnZero) {
853
852
}
854
853
855
854
let pairs = { } ;
856
- // Normal usage, Not FormMode. On individual ID's, not in a <form>.
855
+ // Normal usage, not FormMode ( not in a <form>), select individual ID's .
857
856
if ( ! formOptions . FormMode ) {
858
857
for ( let fp of formOptions . FormParameters ) {
859
858
let value ;
@@ -919,19 +918,19 @@ function GetForm(formOptions, ReturnPairOnZero) {
919
918
} ;
920
919
921
920
/**
922
- * GetFormElements will return a key:value object, where the key's are the given
923
- * form parameters, and the values are the elements that hold the form
924
- * parameters' values.
921
+ * GetFormElements will return a key:value object from teh GUI form using form
922
+ * parameters, and the values are the elements that hold the form parameters'
923
+ * values.
925
924
*
926
925
* @param {FormOptions } formOptions
927
926
* @returns {QuagPairs } key/value (where value is an HTML Element)
928
927
*/
929
928
function GetFormElements ( formOptions ) {
930
- let elems = { } ;
929
+ let kv = { } ;
931
930
for ( let param of formOptions . FormParameters ) {
932
- elems [ param . name ] = document . getElementById ( formOptions . prefix + param . name ) ;
931
+ kv [ param . name ] = document . getElementById ( formOptions . prefix + param . name ) ;
933
932
}
934
- return elems ;
933
+ return kv ;
935
934
} ;
936
935
937
936
0 commit comments