/* This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ // // Function: load() // Called by HTML body element's onload event when the widget is ready to start // var mydir = ''; var csv_file = ''; var scroll_area = ''; var product_parts = ''; var vat = 23; var margin = 0; var shipping = 10; var exchange_rate = 1; function load() { dashcode.setupParts(); scroll_area = document.getElementById('scrollArea'); var d = new Date(); mydir = widget.system("dirname '"+location.pathname+"'", null).outputString.trim(); csv_file = mydir + '/parts.csv'; try { var o = widget.system("stat -f '%c' " + csv_file, null); d.setTime(parseInt(o.outputString.trim())*100); var db_date = d.getDate() + '.' + d.getMonth() + '.' + d.getYear(); } catch (e) { alert(o.errorString); return false; } load_products(); } function load_products() { var p = widget.system('awk -F "\t" \'{print $1}\' '+csv_file + ' | uniq', null); var p = p.outputString.trim().split("\n"); var products = new Array(['Select product...', 0]); for (var i = 0; i < p.length; i++) { prod = p[i]; products.push([prod, prod]); } var popup = document.getElementById('popup'); popup.object.setOptions(products); var o = widget.system("wc -l " + csv_file + " | awk '{print $1}'", null); $('#search').attr('placeholder', o.outputString.trim()+ ' parts'); document.getElementById('search').disabled = false; $('#search').val(''); // clicking a part number puts it on the clipboard $('#front td').live('click', function() { widget.system("/bin/echo '" + $(this).text() + "' | /usr/bin/pbcopy", null); }); } // // Function: remove() // Called when the widget has been removed from the Dashboard // function remove() { // Stop any timers to prevent CPU usage // Remove any preferences as needed // widget.setPreferenceForKey(null, dashcode.createInstancePreferenceKey("your-key")); } // // Function: hide() // Called when the widget has been hidden // function hide() { // Stop any timers to prevent CPU usage } // // Function: show() // Called when the widget has been shown // function show() { } // // Function: sync() // Called when the widget has been synchronized with .Mac // function sync() { // Retrieve any preference values that you need to be synchronized here // Use this for an instance key's value: // instancePreferenceValue = widget.preferenceForKey(null, dashcode.createInstancePreferenceKey("your-key")); // // Or this for global key's value: // globalPreferenceValue = widget.preferenceForKey(null, "your-key"); } // // Function: showBack(event) // Called when the info button is clicked to show the back of the widget // // event: onClick event from the info button // function showBack(event) { $('#tax_field').val(widget.preferenceForKey('vat')); $('#shipping_field').val(widget.preferenceForKey('shipping')); $('#margin_field').val(widget.preferenceForKey('margin')); $('#rate_field').val(widget.preferenceForKey('rate')); var front = document.getElementById("front"); var back = document.getElementById("back"); if (window.widget) { widget.prepareForTransition("ToBack"); } front.style.display = "none"; back.style.display = "block"; if (window.widget) { setTimeout('widget.performTransition();', 0); } } // // Function: showFront(event) // Called when the done button is clicked from the back of the widget // // event: onClick event from the done button // function showFront(event) { var vat = document.getElementById('tax_field').value; widget.setPreferenceForKey(vat, 'vat'); var shipping = document.getElementById('shipping_field').value; widget.setPreferenceForKey(shipping, 'shipping'); var margin = document.getElementById('margin_field').value; widget.setPreferenceForKey(margin, 'margin'); var rate = document.getElementById('rate_field').value; widget.setPreferenceForKey(rate, 'rate'); var front = document.getElementById("front"); var back = document.getElementById("back"); if (window.widget) { widget.prepareForTransition("ToFront"); } front.style.display="block"; back.style.display="none"; if (window.widget) { setTimeout('widget.performTransition();', 0); } } if (window.widget) { widget.onremove = remove; widget.onhide = hide; widget.onshow = show; widget.onsync = sync; } function list_parts(o) { scroll_area.object.content.innerHTML = ''; var parts = o.outputString.trim().split("\n"); var rows = ''; var vat = parseInt(widget.preferenceForKey('vat')); vat = parseFloat('1.'+vat); var rate = parseFloat(widget.preferenceForKey('rate')); var margin = parseFloat(widget.preferenceForKey('margin')); var shipping = parseFloat(widget.preferenceForKey('shipping')); document.getElementById('search').placeholder = parts.length + ' parts found'; for (var i = 0; i < parts.length; i++) { var p = parts[i].split('|'); var number = p[0]; var description = p[1]; var stock = parseFloat(p[2]); var exchange = parseFloat(p[3]); var stock_price = 0; var ex_price = 0; // calculate stock price, if given if (stock > 0) { stock_price = ((stock*100)/(100-margin)+shipping)*rate; stock_price = Math.ceil(stock_price*vat); } else { stock_price = '-'; } if (exchange > 0) { ex_price = ((exchange*100)/(100-margin)+shipping)*rate; ex_price = Math.ceil(ex_price*vat); } else { ex_price = '-'; } rows += '
Part # | Description | Exchg | Stock |
---|