let currentTab = '';
let currentTitle = 'most-active';
const allData = {
'losers': {
'data': [],
'titleClass': 'custom',
'titleImage': 'https://www.ndtvprofit.com/icons/arrow-bottom-right-thick.svg',
'altText': 'Losers Icon',
'title': 'Losers'
},
'gainers': {
'data': [],
'titleClass': 'custom',
'titleImage': 'https://www.ndtvprofit.com/icons/arrow-top-right-thick.svg',
'altText': 'Gainers Icon',
'title': 'Gainers'
},
'most-active': {
'data': [],
'titleClass': 'active',
'titleImage': 'https://www.ndtvprofit.com/icons/fire_icon.svg',
'altText': 'Fire Icon',
'title': 'Most Active'
}
};
const baseUrl = 'https://www.ndtvprofit.com/foapi/market/getStockInsight?limit=4';
const apiMapping = {
'nse': 'NIFTY%20500',
'bse': 'BSE%20-%20500'
};
const processApiValues = (responses) => {
const temp = [];
responses.forEach(res => {
const t = [];
Object.keys(res).forEach(r => {
if (typeof res[r] === "object") {
t.push(res[r]);
}
});
temp.push(t);
});
return temp;
};
const generateItems = (data) => {
let items = '';
data.forEach(d => {
items += '\n' +
' \n' +
'
\n' +
'
\n' +
' ' + d.name + '' +
' ' + (d.change < 0 ?
(new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(d.change)) :
'+' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(d.change))) + '\n' +
'
\n' +
'
\n' +
' ' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(d.lastPrice)) + '' +
' ' + d.changePer.toFixed(2) + '%\n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
' ';
});
return items;
};
const getIndicesData = async () => {
await fetch('https://www.ndtvprofit.com/foapi/market/getIndices')
.then(res => res.json())
.then(res => {
if (res.status) {
const indices = processApiValues([res])[0];
$('#indice-data-list').empty();
indices.forEach(r => {
$('#indice-data-list').append('\n' +
' \n' +
'
' +
(r.change < 0 ? '

' :
'

') +
'
\n' +
'
\n' +
'
\n' +
' ' + r.exchangeSymbol + '\n' +
' ' +
(r.change < 0 ?
(new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.change)) :
'+' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.change))) + '\n' +
'
\n' +
'
\n' +
' ' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.lastPrice)) + '\n' +
' ' + r.changePer.toFixed(2) + '%\n' +
'
\n' +
'
\n' +
'
\n' +
'

\n' +
'
' +
'
' +
'
' + r.exchangeSymbol + '

' +
'
' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.lastPrice)) + '
' +
'
' +
'' + (r.change < 0 ? +
(new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.change)) :
'+' + (new Intl.NumberFormat('en-IN', { minimumFractionDigits: 2 }).format(r.change))) +
'' +
'(' + r.changePer.toFixed(2) + '%)' +
'
' +
'
\n' +
'
\n' +
' ');
});
}
})
.catch(err => {
console.log('error in indices api = ', err);
});
};
const getData = async (indice, mode) => {
if ((mode !== 'click') || (indice !== currentTab)) {
$('#' + currentTab).removeClass('active');
currentTab = indice;
$('#' + indice).addClass('active');
try {
await fetch(baseUrl + '&group=' + apiMapping[indice] + '&type=0')
.then(res => res.json())
.then(res => allData['losers'].data = processApiValues([res])[0]);
await fetch(baseUrl + '&group=' + apiMapping[indice] + '&type=1')
.then(res => res.json())
.then(res => allData['gainers'].data = processApiValues([res])[0]);
await fetch(baseUrl + '&group=' + apiMapping[indice] + '&type=2')
.then(res => res.json())
.then(res => allData['most-active'].data = processApiValues([res])[0]);
$('#stock-tables').empty();
['most-active', 'gainers', 'losers'].forEach(tab => {
const section = '
\n' +
'
\n' +
'
![' + allData[tab].altText + '](' + allData[tab].titleImage + ')
\n' +
'
' + allData[tab].title + '\n' +
'
\n' +
'
\n' +
'
\n' +
generateItems(allData[tab].data) +
'
\n' +
'
\n' +
'
';
$('#stock-tables').append(section);
});
if (window.matchMedia('only screen and (max-width: 767px)').matches) {
$('.stock-list-ctx .stock-tables .stock-table-ctx').hide();
$('#' + currentTitle).show();
}
} catch (e) {
console.log('error = ', e);
}
}
};
const clickOnTitle = (title) => {
currentTitle = title;
$('.stock-list-ctx .all-titles .title-ctx').removeClass('active');
$('#' + title + '-title').addClass('active');
$('.stock-list-ctx .stock-tables .stock-table-ctx').hide();
$('#' + title).show();
};
const generateLoadingItems = () => {
let items = '';
Array.apply(null, Array(4)).forEach(() => {
items += '\n' +
' \n' +
'
\n' +
'
\n' +
' ' +
' \n' +
'
\n' +
'
\n' +
' ' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' ';
});
return items;
};
const createShimmerForIndices = () => {
$('#indice-data-list').empty();
Array.apply(null, Array(2)).forEach(() => {
$('#indice-data-list').append('' +
' \n' +
'
\n' +
'

\n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' \n' +
'
\n' +
'
\n' +
' \n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
' +
' ');
});
};
const createShimmerForStocks = () => {
$('#stock-tables').empty();
['most-active', 'gainers', 'losers'].forEach(tab => {
const section = '\n' +
'
\n' +
'
![' + allData[tab].altText + '](' + allData[tab].titleImage + ')
\n' +
'
' + allData[tab].title + '\n' +
'
\n' +
'
\n' +
'
\n' +
generateLoadingItems() +
'
\n' +
'
\n' +
'
';
$('#stock-tables').append(section);
});
if (window.matchMedia('only screen and (max-width: 767px)').matches) {
$('.stock-list-ctx .stock-tables .stock-table-ctx').hide();
$('#' + currentTitle).show();
}
};
$(document).ready(function() {
const url = new URL(window.location.href);
const darkMode = url.searchParams.get('dm') || url.searchParams.get('DM');
if (!!darkMode && darkMode === '1') {
$('.stocks-today-ctx').addClass('manual-dark-mode');
$('.stocks-today-ctx').css('background-color', '#1e1f1f').css('color', '#FFFFFF');
}
createShimmerForIndices();
createShimmerForStocks();
getData('nse');
getIndicesData();
setInterval(function() {
createShimmerForIndices();
createShimmerForStocks();
getData(currentTab);
getIndicesData();
}, 60000);
});