// Initialize empty stock list let stockList = ['2330.TW']; // Cache frequently-used DOM elements const $stockForm = $('#stock-form'); const $stockSelect = $('#stock-select'); const $compSelect = $('#competition'); const $stockList = $('#stock-list'); const $submitBtn = $('#submit-btn'); const $addStockBtn = $('#addStockBtn'); const $submitPort = $('#submit-port'); const $sendPort = $('#sendPort'); const $commentPort = $('#commentPort'); // Function to add a new stock item to the list function addStockItem(stock, text) { // Add item to array stockList.push(stock); // Update HTML list const $newItem = $(`
  • ${text}
  • `); $stockList.append($newItem); } // Function to delete a stock item from the list function deleteStockItem(itemIndex) { // Remove item from array stockList.splice(itemIndex, 1); // Update HTML list $stockList.children().eq(itemIndex).remove(); } // Event listener for delete button clicks $stockList.on('click', '.delete-btn', function(){ var itemIndex = $(this).closest('li').index() deleteStockItem(itemIndex); // console.log(stockList); }); // $stockList.on('click', '.delete-btn', function(event) { // // Get index of item to delete // const $deleteBtn = $(event.target); // const itemIndex = $deleteBtn.parent().index(); // // Delete item from list // deleteStockItem(itemIndex); // }); // Event listener for submit button click $addStockBtn.click(function(event) { event.preventDefault(); $('#search').val(''); $('#search').trigger("keyup"); // Get selected stock from form const selectedStock = $stockSelect.val(); var text = $('#stock-select option:selected').text(); if (selectedStock != null && stockList.indexOf(selectedStock)===-1) { // Add new item to list addStockItem(selectedStock, text); // Clear input field $stockSelect.val(''); } console.log(stockList); }); // Event listener for submit button click $submitPort.click(function(event) { event.preventDefault(); if (stockList.length > 1){ $('#portModal').modal('show'); console.log('asset confirm'); // $(this).prop('disabled', true); } }); // Event listener for submit button click $sendPort.click(function(event) { if (stockList.length > 1){ $('#confirmMes').text("投資組合已開始建立,請等待完成訊息,或1分鐘後至分析結果區查看!") $('#confirmModal').modal('show'); $submitPort.prop('disabled', true); $.ajax({ url: '/postPort', //todo create_strategy method: 'POST', data: { name: $('input[name=portName]').val(), ts: Date.now(), comp: $('#competition').val(), ratio: $('#ratio-select').val(), role: $('#role-select').val(), comment: $commentPort.val(), stockList: JSON.stringify(stockList) }, success: function(response) { console.log(response); // var res = JSON.parse(response); event.preventDefault(); $('#confirmMes').text(response.mes) if (stockList.length > 0){ $('#confirmModal').modal('show'); } $submitPort.prop('disabled', false); }, error: function(xhr) { console.log('Error submitting stock list: ' + xhr.responseText); } }); $commentPort.val(''); } // Get selected stock from form }); // Event listener for submit button click $submitBtn.click(function(event) { // Send stock list to server // console.log(event.target) // console.log(stockList) // console.log(cacheList.value, stockList); if (stockList.length > 0) { // cacheList = stockList; $.ajax({ url: '/postStock', //todo create_strategy method: 'POST', data: { stockList: JSON.stringify(stockList) }, success: function(response) { var graphs = JSON.parse(response); Plotly.newPlot("graph", graphs.data, graphs.layout, {responsive: true}); console.log(response.layout); }, error: function(xhr) { console.log('Error submitting stock list: ' + xhr.responseText); } }); } }); $(document).ready(function(){ $("#search").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#stock-select option").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) }); }); }); // window.onresize = function() { // Plotly.relayout('graph', { // 'xaxis.autorange': true, // 'yaxis.autorange': true // }); // };