String.prototype.escape = function() { var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return this.replace(/[&<>"']/g, function(m) { return map[m]; }); } class BirdyApi { //Need Lib jQuery ($) + js-cookies (cks) constructor() { this.#log("BirdyApi started"); } user = {token:0}; order = {}; lists = {}; users = {}; #log(data,json) { if (json) console.log(`[${new Date().toISOString()}] ${data}`,json); else console.log(`[${new Date().toISOString()}] ${data}`) } isConnected() {return (this.user && this.user.token && this.user.token != "0")} get token() {if (this.isConnected) return this.user.token; else return null;} login(email,pass) { return new Promise((resolve, reject) => { this.user={token:0}; this.#request("PUT","auth",{ email:email, pass:pass }) .then((data)=> { if (data.token!=="0") { this.user = data; setCookie("token",data.token,60); resolve(this.user); } else { reject(lg.LOGIN_ERROR); } }) .catch(reject) })} getUserMe(token) {return new Promise((resolve, reject) => { this.user={token:token}; this.#requestT("GET","user/me") .then((data)=> { if (data.token!==0) { this.user = data; setCookie("token",data.token,60); resolve(this.user); } else { reject(lg.LOGIN_ERROR); } }) .catch(reject) })} getOrder() { return new Promise((resolve, reject) => { this.#requestT("GET","list/order") .then((data)=> { this.order = data; this.order.forEach(elt => { if (elt.id == 1) elt.name="A, B, C, D..."; else if (elt.id == 2) elt.name="Plus récent"; else if (elt.id == 3) elt.name="Dernièrement modifié"; }); resolve(this.order); }) .catch(reject) })} postList(list) { return new Promise((resolve, reject) => { console.log(list); this.#requestT("POST","list",list) .then((data)=> { this.updateListSync(data); resolve(this.getListSync(data.id)); }) .catch(reject) })} putList(list) { return new Promise((resolve, reject) => { this.#requestT("PUT","list/"+list.id,list) .then((data)=> { this.updateListSync(data); resolve(this.getListSync(data.id)); }) .catch(reject) })} deleteList(idList) { return new Promise((resolve, reject) => { this.#requestT("DELETE","list/"+idList).then((list)=>{ this.removeListSync(list); resolve(list); }).catch(reject); })} getLists(idListOrder) { return new Promise((resolve, reject) => { this.#requestT("GET","list?order="+idListOrder) .then((data)=> { this.lists = data; this.lists.forEach(list => { list = this.#updateList(list); }); resolve(this.lists); }) .catch(reject) })} getListShareInfo(idList) { return new Promise((resolve, reject) => { this.#requestT("GET","/list/"+idList+"/_share.json") .then((list)=> { resolve(list); }) .catch(reject) })} getShareUrl(idList) { return `http://api.birdywood.fr/api/list/${idList}/_share` } #updateList(list) { list.item_new=this.nListViewed(list); list.name = list.name.escape(); list.background = list.background.escape(); list.item.forEach(elt => { elt.name = elt.name.escape(); elt.user_friendlyname = elt.user_friendlyname.escape(); elt.user_img = elt.user_img.escape(); elt.item_new=!this.alreadyView(list.my_date_read*1000,elt.date_update*1000); }); return list; } postItem(texte,idList) { return new Promise((resolve, reject) => { this.#requestT("POST","list/"+idList,{"name":texte}).then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} deleteItem(idList,idItem) { return new Promise((resolve, reject) => { this.#requestT("DELETE","list/"+idList+"/"+idItem).then((item)=>{ resolve(item); }).catch(reject); })} putListToMe(idList) { return new Promise((resolve, reject) => { this.#requestT("PUT","list/"+idList+"/_addme").then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} putListAsRead(idList) { return new Promise((resolve, reject) => { this.#requestT("PUT","list/"+idList+"/_watched").then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} putItem(idList,idItem,texte) { return new Promise((resolve, reject) => { this.#requestT("PUT","list/"+idList+"/"+idItem,{name:texte}).then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} putItemChecked(idList,idItem,checked) { return new Promise((resolve, reject) => { this.#requestT("PUT","list/"+idList+"/"+idItem,{checked:(checked?"1":"0")}).then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} getListUpdate(idList) { return new Promise((resolve, reject) => { this.#requestT("GET","list/"+idList).then((list)=>{ this.updateListSync(list); resolve(list); }).catch(reject); })} getList(idList) { return new Promise((resolve, reject) => { var find=false; for (var i in this.lists) if (this.lists[i].id == idList) { resolve(this.lists[i]) find=true; } if (!find) reject(); })} getUsers(search="") { return new Promise((resolve, reject) => { this.#requestT("GET","user/search?all_off&q="+search).then((users)=>{ if (search=="") this.users = users resolve(users); }).catch(reject); })} updateListSync(list) { list = this.#updateList(list); for (var i in this.lists) if (this.lists[i].id == list.id) { this.lists[i] = list; return true } this.lists.unshift(list); return false } removeListSync(list) { for (var i in this.lists) if (this.lists[i].id == list.id) { this.lists.splice(i,1); return true } return false } getListSync(idList) { for (var i in this.lists) if (this.lists[i].id == idList) { return this.lists[i] } return null } getItemSync(idList,idItem) { var list = this.getListSync(idList); if (list) { for (var i in list.item) if (list.item[i].id == idItem) { return list.item[i] } } return null } reportUser() {return new Promise((resolve, reject) => { var obj = { "type":"LITEM WEB", "subType":"userlists", "user":this.user } this.#requestT("POST","report",obj).then(()=>{ resolve(language_fr.REPORT_USER_SUCCESS); }).catch(reject); })} reportList(idList) {return new Promise((resolve, reject) => { var obj = { "type":"LITEM WEB", "subType":"list", "id":this.getListSync(idList) } this.#requestT("POST","report",obj).then(()=>{ resolve(language_fr.REPORT_LIST_SUCCESS); }).catch(reject); })} nListViewed(list) { const dateListViewed = list.my_date_read ? list.my_date_read*1000 : 0; var n = 0 list.item.forEach(elt => { if (this.alreadyView(dateListViewed, elt.date_update * 1000)) n++ }); return n } alreadyView(dateListViewed, dateItemUpdated) { if (dateListViewed == 0) return true; return ((dateListViewed+5000) < dateItemUpdated); } #requestT(method,link,data=undefined) { return this.#request(method,`${this.token}/${link}`,data)} #request(method,link,data=undefined) { return new Promise((resolve, reject) => { const url = "https://api.birdywood.fr/api/"; var options = { url: `${url}${link}`, method:method } if (method!="GET") { this.#log(`>> ${method} ${url}${link} data:`,data); options.data = JSON.stringify(data); options.contentType = "application/json; charset=utf-8"; options.dataType = "json"; } else { this.#log(`>> ${method} ${url}${link}`); } $.ajax(options).then((data)=> { this.#log(`<< ${method} ${url}${link} data:`,data); if (typeof data == "object") resolve(data); else reject({}); }) .catch((data)=>{ reject(data); }) })} getIAImageUrl(name) { return "https://api.birdywood.fr/api/ia/image/" + encodeURI(name).replaceAll('%20','+').replaceAll('/',' ').replaceAll('#','%23'); } getColorImageUrl(name) { return "https://api.birdywood.fr/api/ia/color/" + encodeURI(name).replaceAll('%20','+').replaceAll('/',' ').replaceAll('#','%23'); } getListBackground(list) { switch (list.background_type) { case "ia": return this.getIAImageUrl(list.name); break; case "url": return list.background; break; case "file": return list.background; break; case "color": return this.getColorImageUrl(list.background); break; default: return "ARG"; } } getImageUser(user_img,user_friendlyname) { if (user_img=="") return this.getIAImageUrl(user_friendlyname); else return user_img; } } window.mobileCheck = function() { let check = false; (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera); return check; }; function setCookie(cname, cvalue, exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); let expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;SameSite=None;Secure"; } function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for(let i = 0; i