/****************** * APP FUNCTIONALITY ******************/ //user settings const settings = require('electron-settings'); settings.set('developer', { first: 'Nathan', last: 'Parikh' }); //default coins if(settings.has('user.coins')) { //do nothing because coins already set } else { settings.set('user', { coins: 'BTC,ETH,LTC' }); } //default base currency if(settings.has('user.currency')) { //do nothing because currency already set } else { settings.set('user.currency', 'USD'); } (function() { function loadJSON(callback) { var file = 'https://www.cryptocompare.com/api/data/coinlist/'; var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', file, true); xobj.onreadystatechange = function () { if (xobj.readyState == 4 && xobj.status == "200") { // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode callback(xobj.responseText); } }; xobj.send(null); } //loadJSON // Generate the list of all coins loadJSON(function(response) { // Parse JSON string into object var myDiv = document.getElementById("coinlist"); var actual_JSON = JSON.parse(response); //alert(settings.get('user.coins')); //console.log(actual_JSON.Data); //loop through data, get coin info, generate checkbox for each coin Object.keys(actual_JSON.Data).forEach(function(key) { //console.log(actual_JSON.Data[key].Name); //console.log(actual_JSON.Data[key].CoinName); var li = document.createElement("li"); var checkBox = document.createElement("input"); checkBox.className = "coinCode"; var label = document.createElement("label"); label.className = "coinName"; var div = document.createElement("div"); checkBox.type = "checkbox"; checkBox.value = actual_JSON.Data[key].Name; checkBox.name = "cl[]"; //check the coins the user has already set var str = String(settings.get('user.coins')); var split_str = str.split(","); if (split_str.indexOf(actual_JSON.Data[key].Name) !== -1) { checkBox.checked = true; } myDiv.appendChild(li); li.appendChild(checkBox); li.appendChild(label); label.appendChild(document.createTextNode(actual_JSON.Data[key].CoinName)); label.appendChild(document.createTextNode(' ('+actual_JSON.Data[key].Name+')')); label.appendChild(div); }); //forEach }); //loadJSON base = settings.get('user.currency'); // get the user's base currency var currSel = document.getElementById('base'); //select the currency select box currSel.value = settings.get('user.currency'); //select the option that corresponds to the user's currency setBase = function() { //selected base currency var sel = document.getElementById('base'); var x = sel.selectedIndex; var y = sel.options; base = y[x].text; settings.set('user.currency', base); //save the user's selection updateData(); //immediately reflect the changed currency }; })(); //Functions for creating/appending elements function createNode(element) { return document.createElement(element); } function append(parent, el) { return parent.appendChild(el); } // Returns an array with values of the selected (checked) checkboxes in "frm" function getSelectedChbox(frm) { var selchbox = []; // array that will store the value of selected checkboxes // gets all the input tags in frm, and their number var inpfields = frm.getElementsByTagName('input'); var nr_inpfields = inpfields.length; // traverse the inpfields elements, and adds the value of selected (checked) checkbox in selchbox for(var i=0; i ' + coinRate + '' + coinDISPLAYchange + '%'; // % Change let change = document.querySelector("#coin-"+[key]+" .change"); if(coinDISPLAYchange > 0) { change.className += " positive"; change.classList.remove("negative"); } else if(coinDISPLAYchange < 0) { change.className += " negative"; change.classList.remove("postive"); } else { change.classList.remove("postive"); change.classList.remove("negative"); } } }); } ) setTimeout(function(){updateData()}, 5000); // run this once every 5 seconds } // Let's do this thing! initData(); /* Test this function */ //document.getElementById('firstname').innerHTML = settings.get('user.coins'); // Click on #saveCoins, save the coin selection to the user document.getElementById('saveCoins').onclick = function(){ var coinForm = document.getElementById('coinlist'); var selchb = getSelectedChbox(coinForm); // gets the array returned by getSelectedChbox() //alert(selchb); settings.set('user.coins', selchb); // just reloading the entire app because I have yet to figure out how to add/remove a coin from the primary list without a page reload location.reload(false); //alert(settings.get('user.currency')); } /******* * APP UI ********/ //Window controls const remote = require('electron').remote; document.getElementById("close-btn").addEventListener("click", function (e) { var window = remote.getCurrentWindow(); window.close(); }); document.getElementById("min-btn").addEventListener("click", function (e) { var window = remote.getCurrentWindow(); window.minimize(); }); //settings tab/icon function toggleSettings() { var divs = document.getElementsByClassName('panel'), i; for (i = 0; i < divs.length; ++i) { if(divs[i].classList.contains('inactive')) { divs[i].classList.remove('inactive'); divs[i].classList.add('active'); } else { divs[i].classList.remove('active'); divs[i].classList.add('inactive'); } }//for }//toggleSettings //Coin search filter function myFunction() { // Declare variables var input, filter, ul, li, a, i; input = document.getElementById('myInput'); filter = input.value.toUpperCase(); ul = document.getElementById("coinlist"); li = ul.getElementsByTagName('li'); // Loop through all list items, and hide those who don't match the search query for (i = 0; i < li.length; i++) { label = li[i].getElementsByTagName("label")[0]; checkbox = li[i].getElementsByTagName("input")[0].value; if (label.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } //for } //myFunction //sort by attribute function sortChildren(wrap, f, isNum) { var l = wrap.children.length, arr = new Array(l); for(var i=0; ib[0] ? 1 : 0; } ); var par = wrap.parentNode, ref = wrap.nextSibling; par.removeChild(wrap); for(var i=0; i