// General data var data = { baseInfo: baseInfo, auth: 'checking', state: 'loading', user: false, token: '', sortKey: '', sortType: 'desc', reverse: false, results: [], newPrices: {}, searchKey: '', selectedcurrency : 'ریال-دلار', currencies : [ {text:'ریال-دلار',from:'IRR',to:'USD'}, {text:'ریال-یورو',from:'IRR',to:'EUR'}, {text:'دلار-یورو',from:'USD',to:'EUR'} ], selectedFromCurrency : '', selectedToCurrency : '', tocurrency : [], results : [], amount : '', reverseAmount : '', id : '', error : false, errorBorderColor : "", currencyList : "" }; var vm; axios.defaults.baseURL = data.baseInfo.baseURL; function setToken(token) { data.token = token; setTimeout(function () { getCurrencyLits (); getResult(); },400); } function setPermission(isPermitted) { data.auth = (isPermitted) ? 'permitted' : 'notPermitted'; } function setUser(user) { data.user = user; } function removeUser() { data.user = false; data.auth = 'notPermitted'; } function getCurrencyLits (){ axios.get(data.baseInfo.baseURL + 'currency/list?token=' + data.token).then(function(res){ data.currencyList = res.data.items }) } function getResult (){ axios.get(data.baseInfo.baseURL + 'exchanges?token=' + data.token).then(function(res){ data.results = res.data.items for(var key in data.results){ for(var inkey in data.currencyList){ if(data.results[key].from == data.currencyList[inkey].abbr){ data.results[key].from_abbr = data.currencyList[inkey].name; data.results[key].from_name_en = data.currencyList[inkey].name_en; } if(data.results[key].to == data.currencyList[inkey].abbr){ data.results[key].to_abbr = data.currencyList[inkey].name; data.results[key].to_name_en = data.currencyList[inkey].name_en; } } } // console.log(data.results) }) } // jQuery(document).ready(function ($) { // Currency component vm = new Vue({ el: '#app', data: function () { return data; }, ready : function(){ this.createText(); }, methods: { //create textuse in select input selectCurrency : function (){ this.createText(); data.amount = ''; data.reverseAmount = ''; }, createText : function (){ var text = [] text = data.selectedcurrency.split('-'); data.selectedFromCurrency = text[0]; data.selectedToCurrency = text[1] }, //add and update new currency addCurrency : function (){ if(data.amount.length>0 && data.reverseAmount.length>0){ data.error = false; for(var key in data.currencies){ if(data.currencies[key].text == data.selectedcurrency){ // console.log(data.currencies[key]) break } } var obj = { from : data.currencies[key].from, to : data.currencies[key].to, amount : data.amount, reverse_amount : data.reverseAmount } axios.post(data.baseInfo.baseURL + 'exchanges?token=' + data.token,obj).then(function(res){ data.amount = ''; data.reverseAmount = ''; getResult(); }) }else{ data.error = true; data.errorBorderColor = "1px solid red" } }, //computing amount and revers_amount ComputingReverseAmount : function (){ if(data.reverseAmount.length > 0){ data.amount = (1/data.reverseAmount).toString() }else{data.amount=0} }, computingAmount : function (){ if(data.amount.length > 0){ data.reverseAmount = (1/data.amount).toString() }else{data.reverseAmount=0} }, //set id for delete currency setDelete : function (id){ data.id = id }, //delete currency --- this function useed in deleteCurrency modal deleteCurrency : function (){ axios.get(data.baseInfo.baseURL + 'exchanges/delete/'+data.id+'?token=' + data.token).then(function(res){ for(var key in data.results){ if(data.results[key].id == data.id){ data.results.splice(key,1); break; } } }) }, //hide error Box when clicked on the inputs hideError : function (){ data.error = false; data.errorBorderColor = "" }, } // methods: { // getResults: function (val, context, key) { // $.ajax({ // method: 'GET', // url: this.baseInfo.baseURL + 'currency?token=' + this.token, // complete: function (e) { // if (e.status === 200) { // if (e.responseJSON.item.length) { // this.results = e.responseJSON.item; // this.state = false; // } else { // this.results = []; // this.state = 'noResults'; // } // } else { // this.state = 'failed'; // } // }.bind(this) // }); // }, // updatePrices: function () { // if(!_.isEmpty(this.newPrices) && this.user.group_id == 1) { // $.ajax({ // method: 'POST', // url: this.baseInfo.baseURL + 'currency?token=' + this.token, // beforeSend: function () { // this.state = 'updating'; // }.bind(this), // data: {new_prices: this.newPrices}, // complete: function (e) { // if (e.status === 200) { // _.each(this.results, function (item) { // if(this.newPrices[item.currency_id]) { // item.amount = this.newPrices[item.currency_id]; // } // }.bind(this)); // this.newPrices = {}; // this.state = 'success'; // } else { // this.state = 'failed'; // } // }.bind(this) // }); // } // }, // columnClass: function (key) { // if (this.sortKey == key && this.reverse) { // this.sortType = 'desc'; // return 'desc'; // } // if (this.sortKey == key) { // this.sortType = 'asc'; // return 'asc'; // } // }, // sortBy: function (key) { // if (this.sortKey == key) { // if (this.reverse) { // this.reverse = false; // } else { // this.reverse = -1; // } // } // this.sortKey = key; // } // } }); // });