/Users/johnr/Desktop/IA pdf Downloads/Criteria__P__-_Coding_Project_Upload_all_2022-05-03/Product_Henri_05_01/Website/htdocs/JS/searchScript.js
  1 function getURL(){  //Creates URL for story page
  2    let DEPLOY_ID = "AKfycbzEdX1hyrbE77wRezjqLsuhca4_jfPtHrzCusORFm0d";
  3    let URL = "https://script.google.com/macros/s/" + DEPLOY_ID + "/dev";
  4    return URL;
  5 }
  6 
  7 var lockStatus = false;
  8 var emailVar;
  9 
 10 var signInMsg=false;   //Checks that the user has signed in
 11 
 12 function onSignIn(googleUser) {
 13     var profile = googleUser.getBasicProfile();
 14     emailVar=profile.getEmail();
 15     console.log("Logged in as "+emailVar);
 16 
 17     signInMsg=true;
 18     hide("lockScreen");
 19 }
 20 
 21 function makeRequestJQuery(){   //Sumbit a search for the story
 22     let queryString = document.getElementById('searchBar').value;
 23     var url = getURL() + "?searchString=" + queryString+"<>USER<>"+emailVar;  
 24     console.log("URL "+url);
 25     // Make an AJAX call to Google Script
 26     jQuery.ajax({
 27         crossDomain: true,
 28         url: url,
 29         method: "GET",
 30         dataType: "jsonp",      //JSONP allows the request to bypass "cross-origin-blocking"
 31     })
 32     .done(function( data ) {    //Waits until the site gets a response
 33         var str=data;
 34         var lockStatus= str.slice(str.indexOf("<>SECURITY<>")+12, str.lastIndexOf("<>SECURITY<>"));    //Find lock status from recieved URL
 35 
 36         var datePubl= str.slice(str.indexOf("<DP>")+4, str.lastIndexOf("<DP>"));    //Load in first story
 37         var date= str.slice(str.indexOf("<DA>")+4, str.lastIndexOf("<DA>"));
 38         var title= str.slice(str.indexOf("<TI>")+4, str.lastIndexOf("<TI>"));
 39         var article= str.slice(str.indexOf("<AR>")+4, str.lastIndexOf("<AR>"));
 40         var author= str.slice(str.indexOf("<AU>")+4, str.lastIndexOf("<AU>"));
 41         var notes= str.slice(str.indexOf("<NO>")+4, str.lastIndexOf("<NO>"));
 42 
 43         fillData("articleTitle", title)
 44         fillData("article", article)
 45         fillData("articlePublisher",("-"+author))
 46         fillData("articleDate", ("Date: "+date+"<br></br>Date Published: "+datePubl))
 47         fillData("notes", notes)
 48  
 49         var datePubl2= str.slice(str.indexOf("<DP2>")+5, str.lastIndexOf("<DP2>"));    //Load in second story
 50         var date2= str.slice(str.indexOf("<DA2>")+5, str.lastIndexOf("<DA2>"));
 51         var title2= str.slice(str.indexOf("<TI2>")+5, str.lastIndexOf("<TI2>"));
 52         var article2= str.slice(str.indexOf("<AR2>")+5, str.lastIndexOf("<AR2>"));
 53         var author2= str.slice(str.indexOf("<AU2>")+5, str.lastIndexOf("<AU2>"));
 54         var notes2= str.slice(str.indexOf("<NO2>")+5, str.lastIndexOf("<NO2>"));
 55         
 56         fillData("articleTitle2", title2)
 57         fillData("article2", article2)
 58         fillData("articlePublisher2",("-"+author2))
 59         fillData("articleDate2", ("Date: "+date2+"<br></br>Date Published: "+datePubl2))
 60         fillData("notes2", notes2)
 61         show('articleContainer2','articlePublisher2');
 62 
 63         var datePubl3= str.slice(str.indexOf("<DP3>")+5, str.lastIndexOf("<DP3>"));    //Load in third story
 64         var date3= str.slice(str.indexOf("<DA3>")+5, str.lastIndexOf("<DA3>"));
 65         var title3= str.slice(str.indexOf("<TI3>")+5, str.lastIndexOf("<TI3>"));
 66         var article3= str.slice(str.indexOf("<AR3>")+5, str.lastIndexOf("<AR3>"));
 67         var author3= str.slice(str.indexOf("<AU3>")+5, str.lastIndexOf("<AU3>"));
 68         var notes3= str.slice(str.indexOf("<NO3>")+5, str.lastIndexOf("<NO3>"));
 69         
 70         fillData("articleTitle3", title3)
 71         fillData("article3", article3)
 72         fillData("articlePublisher3",("-"+author3))
 73         fillData("articleDate3", ("Date: "+date3+"<br></br>Date Published: "+datePubl3))
 74         fillData("notes3", notes3)
 75         show('articleContainer3','articlePublisher3');
 76 
 77         console.log("Lock status "+lockStatus+" signInMsg "+signInMsg);
 78         if(lockStatus=="false"){    //Show security message as they are not authorised
 79             hide("articleContainer");
 80             hide("articleContainer2");
 81             hide("articleContainer3");
 82             show("lockScreen");
 83             if(signInMsg==true){    //If signed in but not authorised
 84                 fillData("lockScreen", "This content is private, if this is your content please make sure you are signed in with the correct account. If you still encounter problems, please contact: FAKEEMAIL@EMAIL.COM");
 85             }else{    //If not signed in
 86                 fillData("lockScreen", "Please sign in to view this content!");
 87             }
 88         }
 89         if(lockStatus=="true"){
 90             hide("lockScreen");
 91             signInMsg=false;    //Hide Please sign in message
 92         }
 93     });
 94 }
 95 
 96 function makeRequestJQuerySpecial(queryString){ //Search for a story with the search prompts
 97     var url = getURL() + "?searchString=" + queryString;  
 98     // Make an AJAX call to Google Script
 99     jQuery.ajax({
100         crossDomain: true,
101         url: url,
102         method: "GET",
103         dataType: "jsonp",
104     })
105     .done(function( data ) {
106         var str=data;
107         var lockStatus= str.slice(str.indexOf("<>SECURITY<>")+12, str.lastIndexOf("<>SECURITY<>"));    //Find lock status
108 
109         var datePubl= str.slice(str.indexOf("<DP>")+4, str.lastIndexOf("<DP>"));    //Load in story
110         var date= str.slice(str.indexOf("<DA>")+4, str.lastIndexOf("<DA>"));
111         var title= str.slice(str.indexOf("<TI>")+4, str.lastIndexOf("<TI>"));
112         var article= str.slice(str.indexOf("<AR>")+4, str.lastIndexOf("<AR>"));
113         var author= str.slice(str.indexOf("<AU>")+4, str.lastIndexOf("<AU>"));
114         var notes= str.slice(str.indexOf("<NO>")+4, str.lastIndexOf("<NO>"));
115         
116         fillData("articleTitle", title)
117         fillData("article", article)
118         fillData("articlePublisher",("-"+author))
119         fillData("articleDate", ("Date: "+date+"<br></br>Date Published: "+datePubl))
120         fillData("notes", notes)
121         hide('centered', 'infoText'); 
122         show('articleContainer','articlePublisher');
123         if(queryString=="<FREQUENT-SEARCHES-1>"||queryString=="<FREQUENT-SEARCHES-2>"){
124             hide("articleDate");
125         }
126 
127         if(lockStatus==false){    //Show security message as they are not authorised
128             hide("articleContainer");
129             show("lockScreen");
130             if(signInMsg==true){    //If signed in but not authorised
131                 fillData("lockScreen", "This content is private, if this is your content please make sure you are signed in with the correct account. If you still encounter problems, please contact: FAKEEMAIL@EMAIL.COM");
132             }else{    //If not signed in
133                 fillData("lockScreen", "Please sign in to view this content!");
134             }
135         }
136         if(lockStatus=="true"){
137             hide("lockScreen");
138             signInMsg=false;    //Hide Please sign in message
139         }
140     });
141 }
142 
143 function hide(hideThis) {
144   var z = document.getElementById(hideThis);
145     z.style.display = "none";
146 }
147 function show(showThis) {
148   var z = document.getElementById(showThis);
149     z.style.display = "block";
150 }
151 
152 function fillData(element, data) { 
153   document.getElementById(element).innerHTML = data;
154 }
155 
156 $(function(){   //Makes the search prompts interactible
157     let viewed_1 = document.getElementById("VIEWED_1");
158     let viewed_2 = document.getElementById("VIEWED_2");
159     let frequent_1 = document.getElementById("FREQUENT_1");
160     let frequent_2 = document.getElementById("FREQUENT_2");
161     let lastadded = document.getElementById("LASTADDED");
162     viewed_1.addEventListener("click", callViewed_1);
163     viewed_2.addEventListener("click", callViewed_2);
164     frequent_1.addEventListener("click", callFrequent_1);
165     frequent_2.addEventListener("click", callFrequent_2);
166     lastadded.addEventListener("click", callLastadded);
167 });
168 function callViewed_1(){makeRequestJQuerySpecial("<MOST-VIEWED-1>");}
169 function callViewed_2(){makeRequestJQuerySpecial("<MOST-VIEWED-2>");}
170 function callFrequent_1(){makeRequestJQuerySpecial("<FREQUENT-SEARCHES-1>");}
171 function callFrequent_2(){makeRequestJQuerySpecial("<FREQUENT-SEARCHES-2>");}
172 function callLastadded(){makeRequestJQuerySpecial("<LAST-ADDED>");}
173 
174 function homeButton(){  //Resets the site
175     fillData("article", "Please be patient. Your memory may take up to 30 seconds to load.");
176     fillData("article2", "Please be patient. Your memory may take up to 30 seconds to load.");
177     fillData("article3", "Please be patient. Your memory may take up to 30 seconds to load.");
178 
179     fillData("articleTitle", "");
180     fillData("articlePublisher", "");
181     fillData("articleDate", "");
182     fillData("notes", "");
183 
184     fillData("articleTitle2", "");
185     fillData("articlePublisher2", "");
186     fillData("articleDate2", "");
187     fillData("notes2", "");
188 
189     fillData("articleTitle3", "");
190     fillData("articlePublisher3", "");
191     fillData("articleDate3", "");
192     fillData("notes3", "");
193 
194     hide("lockScreen");
195     hide("articleContainer");
196     hide("articleContainer2");
197     hide("articleContainer3");
198 }