|
| 1 | +let usedCurrencies = { |
| 2 | + influence: 100, |
| 3 | + goldenInfluence: 100 |
| 4 | +}; |
| 5 | +let totalCurrencies = { |
| 6 | + influence: 1000, |
| 7 | + goldenInfluence: 300 |
| 8 | +}; |
| 9 | +let lists = { |
| 10 | + todo: { |
| 11 | + possibleVotes: [ |
| 12 | + { |
| 13 | + name: "influence", |
| 14 | + currency: "influence", |
| 15 | + maximum: 30, |
| 16 | + score: 1, |
| 17 | + step: 2, |
| 18 | + color: "blue" |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "golden", |
| 22 | + currency: "goldenInfluence", |
| 23 | + score: 1, |
| 24 | + step: 10, |
| 25 | + color: "#bfa203" |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "thumb", |
| 29 | + minimum: -5, |
| 30 | + maximum: 5, |
| 31 | + score: 5, |
| 32 | + step: 1, |
| 33 | + color: "#535353" |
| 34 | + } |
| 35 | + ], |
| 36 | + items: [ |
| 37 | + { id: "1234", list: "todo", title: "Finish up MVP documentation", description: "Take care for the remaining issues in the webpack.js.org repo which are relevant for the MVP.", influence: 15 }, |
| 38 | + { id: "2345", list: "todo", title: "Review whole documentation", description: "Read over **all** of the documentation to find errors.", golden: 20 }, |
| 39 | + ] |
| 40 | + } |
| 41 | +}; |
| 42 | +let allItems = { |
| 43 | + "1234": lists.todo.items[0], |
| 44 | + "2345": lists.todo.items[1], |
| 45 | +}; |
| 46 | + |
| 47 | +function delay(time) { |
| 48 | + return new Promise(function (fulfill) { |
| 49 | + setTimeout(fulfill, time); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function clone(json) { |
| 54 | + return JSON.parse(JSON.stringify(json)); |
| 55 | +} |
| 56 | + |
| 57 | +export function isLoginActive() { |
| 58 | + return /^\?login=/.test(window.location.search); |
| 59 | +} |
| 60 | + |
| 61 | +export function startLogin(callbackUrl) { |
| 62 | + window.location.search = "?login=" + encodeURIComponent(callbackUrl); |
| 63 | + return Promise.resolve(); |
| 64 | +} |
| 65 | + |
| 66 | +export function continueLogin() { |
| 67 | + if(/^\?login=/.test(window.location.search)) { |
| 68 | + return delay(2000).then(() => { |
| 69 | + setTimeout(() => window.location = decodeURIComponent(window.location.search.substr(7), 100)); |
| 70 | + return "developer"; |
| 71 | + }); |
| 72 | + } |
| 73 | + return Promise.resolve(); |
| 74 | +} |
| 75 | + |
| 76 | +export function getSelf(token) { |
| 77 | + if(token !== "developer") |
| 78 | + return Promise.reject(new Error("Not logged in as developer")); |
| 79 | + return delay(500).then(() => ({ |
| 80 | + login: "dev", |
| 81 | + name: "Developer", |
| 82 | + avatar: "https://github.com/webpack.png", |
| 83 | + currencies: [ |
| 84 | + { name: "influence", displayName: "Influence", description: "Some **description**", value: totalCurrencies.influence, used: usedCurrencies.influence, remaining: totalCurrencies.influence - usedCurrencies.influence }, |
| 85 | + { name: "goldenInfluence", displayName: "Golden Influence", description: "Some **description**", value: totalCurrencies.goldenInfluence, used: usedCurrencies.goldenInfluence, remaining: totalCurrencies.goldenInfluence - usedCurrencies.goldenInfluence } |
| 86 | + ] |
| 87 | + })); |
| 88 | +} |
| 89 | + |
| 90 | +export function getList(token, name) { |
| 91 | + const loggedIn = token === "developer"; |
| 92 | + const listData = lists[name]; |
| 93 | + return delay(500).then(() => ({ |
| 94 | + name: name, |
| 95 | + displayName: "DEV: " + name, |
| 96 | + description: "Some **description**", |
| 97 | + lockable: true, |
| 98 | + deletable: true, |
| 99 | + archivable: true, |
| 100 | + isAdmin: true, |
| 101 | + possibleVotes: listData.possibleVotes, |
| 102 | + items: lists[name].items.map(item => { |
| 103 | + const votes = listData.possibleVotes.map(pv => ({ |
| 104 | + name: pv.name, |
| 105 | + votes: (item[pv.name] || 0) + Math.floor(Math.random() * 100) |
| 106 | + })); |
| 107 | + const score = listData.possibleVotes.map((pv, i) => { |
| 108 | + return pv.score * votes[i].votes; |
| 109 | + }).reduce((a, b) => a + b, 0); |
| 110 | + return { |
| 111 | + id: item.id, |
| 112 | + list: item.list, |
| 113 | + title: item.title, |
| 114 | + description: item.description, |
| 115 | + votes, |
| 116 | + userVotes: loggedIn ? listData.possibleVotes.map(pv => ({ |
| 117 | + name: pv.name, |
| 118 | + votes: item[pv.name] || 0 |
| 119 | + })) : undefined, |
| 120 | + score |
| 121 | + }; |
| 122 | + }).sort((a, b) => b.score - a.score) |
| 123 | + })); |
| 124 | +} |
| 125 | + |
| 126 | +export function createItem(token, list, title, description) { |
| 127 | + if(token !== "developer") |
| 128 | + return Promise.reject(new Error("Not logged in as developer")); |
| 129 | + let newItem = { |
| 130 | + id: Math.random() + "", |
| 131 | + list, |
| 132 | + title, |
| 133 | + description |
| 134 | + }; |
| 135 | + allItems[newItem.id] = newItem; |
| 136 | + lists[list].items.push(newItem); |
| 137 | + return delay(500).then(() => ({ |
| 138 | + ...newItem, |
| 139 | + votes: lists[list].possibleVotes.map(pv => ({ |
| 140 | + name: pv.name, |
| 141 | + votes: 0 |
| 142 | + })), |
| 143 | + userVotes: lists[list].possibleVotes.map(pv => ({ |
| 144 | + name: pv.name, |
| 145 | + votes: 0 |
| 146 | + })), |
| 147 | + score: 0 |
| 148 | + })); |
| 149 | +} |
| 150 | + |
| 151 | +export function vote(token, itemId, voteName, value) { |
| 152 | + if(token !== "developer") |
| 153 | + return Promise.reject(new Error("Not logged in as developer")); |
| 154 | + var listId = allItems[itemId].list; |
| 155 | + let listData = lists[listId]; |
| 156 | + let pv = listData.possibleVotes.filter(pv => pv.name === voteName)[0]; |
| 157 | + if(pv.currency) { |
| 158 | + usedCurrencies[pv.currency] += value; |
| 159 | + } |
| 160 | + allItems[itemId][voteName] = (allItems[itemId][voteName] || 0) + value; |
| 161 | + return delay(500).then(() => ({ |
| 162 | + ok: true |
| 163 | + })); |
| 164 | +} |
0 commit comments