diff --git a/public/js/main.js b/public/js/main.js index 3707496..0e5dc95 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,7 +1,6 @@ var KWS = function(){ return { - db:undefined, table: undefined, myIp: undefined, options: {}, @@ -49,17 +48,17 @@ var KWS = function(){ * Get the service url based on options set in the dom. * @return {String} A jsonp url for search suggestions with query missing from the end. */ - getUrl :function(){ + getUrl :function(service, options){ // Ref: https://github.com/estivo/Instantfox/blob/master/firefox/c1hrome/content/defaultPluginList.js // Ref: https://github.com/bnoordhuis/mozilla-central/tree/master/browser/locales/en-US/searchplugins // Ref: https://developers.google.com/custom-search/json-api/v1/reference/cse/list // https://developers.google.com/custom-search/docs/ref_languages - return _.template(this.services[this.options.service])(this.options); + return _.template(this.services[(service||this.options.service)])(options||this.options); }, /** Parse response per service **/ - parseServiceResponse: function(res){ + parseServiceResponse: function(res, service){ // Each take a json response tand return a keyword array RESPONSE_TEMPLATES = { // opensearch default @@ -91,55 +90,10 @@ var KWS = function(){ "google play movies": function(res){return _.map(res,'s')}, "google play books": function(res){return _.map(res,'s')}, }; - var parser = RESPONSE_TEMPLATES[this.options.service] || RESPONSE_TEMPLATES["default"]; + var parser = RESPONSE_TEMPLATES[(service||this.options.service)] || RESPONSE_TEMPLATES["default"]; return parser(res); }, - setUpDb: function(success){ - var self=this; - // setup a db. Ref: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB - var dbReq = window.indexedDB.open("KeywordShitter2", 3); - /** Error handler for all child transactions as events bubbe up **/ - dbReq.onerror = function (event) { - return console.error('Error opening indexedDB database KeywordShitter2', event); - }; - dbReq.onsuccess = function (event) { - self.db = event.target.result; - self.db.onerror = function (event) { - // Generic error handler requests - if (event && event.target && event.target.error) - console.error("Database error",event.target.error.errorCode, event.target.error.message, event); - else - console.error("Database error",arguments); - return this; - }; - if (success) success(self.db,dbReq); - return self.db; - }; - dbReq.onupgradeneeded = function (event) { - console.log("running onupgradeneeded"); - self.db = event.target.result; - - if (!self.db.objectStoreNames.contains("suggestions")) { - var objectStore = self.db.createObjectStore("suggestions", { - autoIncrement: true, keyPath: 'id' - }); - // Create an index to search suggestions by - // he query that prompted the suggestion - if (!objectStore.indexNames.contains('search')) - objectStore.createIndex("search", "search", {unique: false}); - - // and by suggestion/keyword - if (!objectStore.indexNames.contains('keyword')) - objectStore.createIndex("keyword", "keyword", {unique: false}); - - } - - return self.db; - }; - return dbReq; - }, - toggleWork: function(){ if (this.doWork === false) this.StartWork(); @@ -287,60 +241,6 @@ var KWS = function(){ return newInputs; }, - /** export db as json **/ - exportDB: function(success){ - var reqObj = this.db.transaction(["suggestions"],"readonly") - .objectStore("suggestions") - .getAll(); - reqObj.onsuccess = function(e) { - var blob, name; - if (e.target.result.length){ - var jsonData = JSON.stringify(e.target.result); - blob = new Blob([jsonData], {type: "octet/stream"}); - var timeStamp = (new Date()).toISOString().replace(/[:\.]/g,'_').slice(0,-5); - name = 'keywordshitter_'+timeStamp+'_r'+e.target.result.length+'.json'; - saveAs(blob,name); - if (success instanceof Function) success(blob); - } - return blob; - }; - return reqObj; - }, - - clearDB: function(){ - this.db.transaction(["suggestions"], "readwrite") - .objectStore("suggestions") - .clear(); - console.warn('cleared all indexedDB data'); - }, - - /** Display data from db upon pressing load button **/ - loadFromDB: function(){ - var self = this; - var reqObj = this.db.transaction(["suggestions"],"readonly") - .objectStore("suggestions") - .getAll() - .onsuccess = function(e) { - if (e.target.result.length){ - /// grab the fields we want - var data =[]; - for (var i = 0; i < e.target.result.length; i++) { - var da = e.target.result[i]; - - // also remove undefined so datatables doesn't bring up alerts - da = _.mapValues(da, function(v){return v===undefined ? null: v;}); - - // parse nums - // da = da.map(v => /^[\d+\.]+$/.test(v) ? Number(v): v); - // - data.push(da); - } - self.table.rows.add(data); - return self.table.draw(false); - } - }; - return; - }, /** Display results **/ displayResults: function(retList, search, dontDisplay, url,data){ @@ -411,49 +311,6 @@ var KWS = function(){ return domain; }, - /** Store new results in db and hashmap **/ - storeResults: function(retList, search, url){ - var self = this; - retList=retList.map(this.CleanVal); - - // We will add the items async in order - // Ref: http://stackoverflow.com/a/13666741/221742 - var transaction = this.db.transaction(["suggestions"], "readwrite"); - var store = transaction.objectStore("suggestions"); - var i=0; - addNext(); - - /** Like an async for loop to add each to db **/ - function addNext() { - if (i