@ucd-lib/theme-elements 0.0.7 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @class PopStateObserverController
3
+ * @classdesc Controller for attaching a popstate event listener to a Lit element.
4
+ *
5
+ * @property {LitElement} host - 'this' from a Lit element
6
+ * @property {String} callback - Name of element method called on popstate. Default: '_onPopstate'
7
+ *
8
+ * @examples
9
+ * // Instantiate this controller in the constructor of your element
10
+ * new PopStateObserverController(this, "_onLocationChange");
11
+ */
12
+ export class PopStateObserverController{
13
+
14
+ constructor(host, callback="_onPopstate"){
15
+ (this.host = host).addController(this);
16
+ this.callback = callback;
17
+ this._onPopstate = this._onPopstate.bind(this);
18
+ }
19
+
20
+ hostConnected(){
21
+ window.addEventListener('popstate', this._onPopstate);
22
+ }
23
+
24
+ hostDisconnected(){
25
+ window.removeEventListener('popstate', this._onPopstate);
26
+ }
27
+
28
+ _onPopstate(e){
29
+ if ( !this.host[this.callback]){
30
+ console.warn(
31
+ `Element has no '${this.callback}' method.
32
+ Either add this method, or change the 'callback' argument on instantiation.`
33
+ );
34
+ return;
35
+ }
36
+ let locationObject = this._getLocationObject();
37
+ this.host[this.callback](locationObject, e);
38
+
39
+ }
40
+
41
+ _getLocationObject(){
42
+ let location = {
43
+ fullpath : window.location.href.replace(window.location.origin, '').replace(/^\/+/, '/'),
44
+ pathname : window.location.pathname.replace(/^\/+/, '/'),
45
+ path : window.location.pathname.replace(/(^\/+|\/+$)/g, '').split('/'),
46
+ query : new URLSearchParams(window.location.search),
47
+ hash : window.location.hash.replace(/^#/, '')
48
+ };
49
+ return location;
50
+ }
51
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @class SilsPrimoController
3
+ * @classdesc Utility for interacting with UC Libraries' discovery tool
4
+ *
5
+ * @property {LitElement} host - 'this' from a Lit element
6
+ * @property {Object} config - Basic Primo configuration values (host, uris, etc)
7
+ */
8
+ export class SilsPrimoController{
9
+
10
+ /**
11
+ * @method constructor
12
+ * @description Called on instantiation
13
+ * @param {LitElement} host - Element
14
+ * @param {Object} config - Config values
15
+ */
16
+ constructor(host, config={}){
17
+ (this.host = host).addController(this);
18
+ this.updateConfig(config);
19
+ }
20
+
21
+ /**
22
+ * @method updateConfig
23
+ * @description Updates the config property.
24
+ * @param {Object} config - Values to overide the default.
25
+ */
26
+ updateConfig(config){
27
+ const UCD_TAB = "LibraryCatalog";
28
+ let _config = {
29
+ host: "https://search.library.ucdavis.edu",
30
+ paths: {
31
+ search: "discovery/search",
32
+ browse: "discovery/browse"
33
+ },
34
+ defaultParams: {
35
+ vid: "01UCD_INST:UCD"
36
+ },
37
+ corpora: {
38
+ everything: {
39
+ tab: "UCSILSDefaultSearch",
40
+ scope: "DN_and_CI"
41
+ },
42
+ uc: {
43
+ tab: "UCSDiscoveryNetwork",
44
+ scope: "UCSDiscoveryNetwork"
45
+ },
46
+ ucd: {
47
+ tab: UCD_TAB,
48
+ scope: "MyInstitution",
49
+ },
50
+ specialCollections: {
51
+ tab: UCD_TAB,
52
+ scope: "SSPEC"
53
+ },
54
+ medical: {
55
+ tab: UCD_TAB,
56
+ scope: "BLAISDELL"
57
+ },
58
+ healthSciences: {
59
+ tab: UCD_TAB,
60
+ scope: "CARLSON"
61
+ },
62
+ law: {
63
+ tab: UCD_TAB,
64
+ scope: "Mabie"
65
+ }
66
+ }
67
+ };
68
+
69
+ this.config = Object.assign(_config, config);
70
+ }
71
+
72
+ /**
73
+ * @method makeSearchUrl
74
+ * @description Makes a Primo Search URL
75
+ * @param {String} query - A search term or phrase
76
+ * @param {String} corpus - The bib corpus to search against.
77
+ * Sets 'tab' and 'search_scope' params. Must be a recognized keyword in the corpora config object:
78
+ * everything, uc, ucd, specialCollections, medical, healthSciences, law
79
+ * @param {Boolean} advanced - Expands the advanced search interface
80
+ * @param {Object} additionalParams - Any additional url params. Has the final say.
81
+ * @returns
82
+ */
83
+ makeSearchUrl( query, corpus="everything", advanced=false, additionalParams={} ){
84
+ let url = `${this._trailingSlashIt(this.config.host)}${this.config.paths.search}`;
85
+
86
+ let params = Object.assign({}, this.config.defaultParams);
87
+
88
+ if ( advanced ) {
89
+ params['mode'] = 'advanced';
90
+ }
91
+
92
+ if ( query ) {
93
+ params['query'] = 'any,contains,' + query.replace(/,/g, ' ');
94
+ }
95
+
96
+ if ( this.config.corpora[corpus] ) {
97
+ params['tab'] = this.config.corpora[corpus].tab;
98
+ params['search_scope'] = this.config.corpora[corpus].scope;
99
+ } else {
100
+ console.warn(`${corpus} is not a recognized corpus`);
101
+ }
102
+
103
+ if ( additionalParams ){
104
+ Object.assign(params, additionalParams);
105
+ }
106
+
107
+ params = new URLSearchParams(params);
108
+ return `${url}?${params.toString()}`;
109
+ }
110
+
111
+ /**
112
+ * @method _trailingSlashIt
113
+ * @description Adds trailing slash to string if not already present
114
+ * @private
115
+ * @param {String} str
116
+ * @returns
117
+ */
118
+ _trailingSlashIt(str){
119
+ if ( str.endsWith('/') ){
120
+ return str;
121
+ }
122
+ return str + "/";
123
+ }
124
+ }