fix some bugs
[smart-stock.git] / feedprovider / src / main / webapp / js / dynamicrequest.js
blob22119b06046def3b881842c8950ce97aa46e5405
1 function loadstocks(){
2 var uri = finduri();
3 jQuery.ajax({
4 type: "GET",
5 url: uri,
6 dataType: "json",
7 timeout: 120000,
8 success: function(data){
9 $(".loading").text("Loading...");
10 if(data==null){
11 $(".loading").text("Data can't be loaded due to some error");
13 else
15 if(isLoadingFirstTime()){
16 var arrayOfQuotes = new Array();
17 var index=0;
18 var baseuri = "http://www.stockbangladesh.com/company_details.php?code=";
19 $.each(data.exchanges, function(i, se){
20 $.each(se.quotes, function(j, x){
21 arrayOfQuotes[index] = $(".savedExchange").clone(true);
22 arrayOfQuotes[index].addClass("exchange");
23 arrayOfQuotes[index].removeClass("savedExchange");
24 arrayOfQuotes[index].children(".companyLink").children(".exchangeHyperlink").attr("href", baseuri+x.company.symbol);
25 arrayOfQuotes[index].children(".companyLink").children(".exchangeHyperlink").children(".companyName").text(x.company.symbol);
26 arrayOfQuotes[index].children(".value").text(x.value);
27 arrayOfQuotes[index].children(".lastPrice").text(x.lastPrice);
28 arrayOfQuotes[index].children(".high").text(x.high);
29 arrayOfQuotes[index].children(".low").text(x.low);
30 arrayOfQuotes[index].children(".changePercentage").text(x.changePercentage);
31 arrayOfQuotes[index].children(".trade").text(x.trade);
32 arrayOfQuotes[index].children(".volume").text(x.volume);
33 arrayOfQuotes[index].children(".version").text(x.version);
34 var d = new Date(x.quoteTime);
35 arrayOfQuotes[index].children(".quoteTime").text(getDateString(d));
36 arrayOfQuotes[index].children(".quoteTimeNumber").text(x.quoteTime);
37 arrayOfQuotes[index].attr("id", x.company.symbol);
38 index++;
39 });
40 });
41 var i;
42 for (i=0;i<index;i++){
43 arrayOfQuotes[i].show();
44 arrayOfQuotes[i].appendTo("#exchanges");
46 $(".savedExchange").remove();
47 setRowColored();
48 $(".loading").text("");
50 else{
51 $.each(data.exchanges, function(i, se){
52 $.each(se.quotes, function(j, x){
53 $("#"+x.company.symbol).children(".value").text(x.value);
54 $("#"+x.company.symbol).children(".lastPrice").text(x.lastPrice);
55 $("#"+x.company.symbol).children(".high").text(x.high);
56 $("#"+x.company.symbol).children(".low").text(x.low);
57 $("#"+x.company.symbol).children(".changePercentage").text(x.changePercentage);
58 $("#"+x.company.symbol).children(".trade").text(x.trade);
59 $("#"+x.company.symbol).children(".volume").text(x.volume);
60 $("#"+x.company.symbol).children(".version").text(x.version);
61 var d = new Date(x.quoteTime);
62 $("#"+x.company.symbol).children(".quoteTime").text(getDateString(d));
63 $("#"+x.company.symbol).children(".quoteTimeNumber").text(x.quoteTime);
64 });
65 });
66 $(".loading").text("");
70 error: function(error){
71 $(".loading").text("Data can't be loaded due to some error");
73 });
76 function finduri(){
77 var symbol = $(".symbol").text();
78 var fulluri = "/api/latest/" + symbol + ".json";
79 var uri = "/api/exchange/" + symbol + ".json?";
80 var total = 0;
81 var numberVisible = 0;
82 var isFirst = true;
83 if(isLoadingFirstTime())
84 return fulluri;
85 $(".companyName").each(function(){
86 if($(this).parents(".exchange").is(":visible")){
87 if(isFirst){
88 isFirst = false;
89 }else{
90 uri = uri + "&";
92 uri = uri + "tickr=" + $(this).text();
93 numberVisible++;
95 total++;
96 });
97 if(numberVisible > total/2 || numberVisible == 0){
98 return fulluri;
99 }else{
100 return uri;
104 function isLoadingFirstTime(){
105 var totalExchange=0;
106 $(".exchange").each(function(){
107 totalExchange++;
108 if(totalExchange>0)
109 return false;
111 if(totalExchange==0)
112 return true;
113 else
114 return false;