|
| 1 | +class Process { |
| 2 | + constructor(invoker,title){ |
| 3 | + this._ = {}; |
| 4 | + if(invoker instanceof Function){ |
| 5 | + this._.promise = new Promise(invoker); |
| 6 | + } else if (invoker instanceof Promise){ |
| 7 | + this._.promise = invoker; |
| 8 | + } else { |
| 9 | + throw new TypeError('Invalid invoker passed for Process constructor'); |
| 10 | + } |
| 11 | + |
| 12 | + this._.component = (function(){ |
| 13 | + var div = document.createElement('div'); |
| 14 | + |
| 15 | + var statusIcon = document.createElement('span'); |
| 16 | + statusIcon.setAttribute('uk-spinner','ratio: .7'); |
| 17 | + div.appendChild(statusIcon); |
| 18 | + |
| 19 | + var processTitle = document.createElement('span'); |
| 20 | + if(title){ |
| 21 | + processTitle.innerText = ' '+title; |
| 22 | + } |
| 23 | + div.appendChild(processTitle); |
| 24 | + |
| 25 | + return { |
| 26 | + success: function(){ |
| 27 | + statusIcon.removeAttribute('uk-spinner'); |
| 28 | + statusIcon.classList.remove('uk-spinner'); |
| 29 | + statusIcon.setAttribute('uk-icon','icon: check'); |
| 30 | + div.classList.add('uk-text-success'); |
| 31 | + }, |
| 32 | + failure: function(){ |
| 33 | + statusIcon.removeAttribute('uk-spinner'); |
| 34 | + statusIcon.classList.remove('uk-spinner'); |
| 35 | + statusIcon.setAttribute('uk-icon','icon: close'); |
| 36 | + div.classList.add('uk-text-danger'); |
| 37 | + }, |
| 38 | + updateTitle: function(title){ |
| 39 | + processTitle.innerText = ' '+title |
| 40 | + }, |
| 41 | + getElement: function(){ |
| 42 | + return div; |
| 43 | + } |
| 44 | + }; |
| 45 | + })(); |
| 46 | + |
| 47 | + var parent = this._; |
| 48 | + this._.status = ProcessStatus.INPROGRESS; |
| 49 | + this._.promise.then(function(){ |
| 50 | + parent.status = ProcessStatus.SUCCESS; |
| 51 | + parent.component.success(); |
| 52 | + },function(){ |
| 53 | + parent.status = ProcessStatus.FAILED; |
| 54 | + parent.component.failure(); |
| 55 | + }); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + updateTitle(title){ |
| 60 | + this._.component.updateTitle(title); |
| 61 | + } |
| 62 | + |
| 63 | + then(resolve, reject){ |
| 64 | + this._.promise.then(resolve, reject); |
| 65 | + } |
| 66 | + |
| 67 | + catch(reject){ |
| 68 | + this._.promise.catch(reject); |
| 69 | + } |
| 70 | + |
| 71 | + appendToElement(element){ |
| 72 | + element.appendChild(this._.component.getElement()); |
| 73 | + } |
| 74 | + |
| 75 | + getStatus(){ |
| 76 | + return this._.status; |
| 77 | + } |
| 78 | +} |
| 79 | +class ProcessQueue { |
| 80 | + constructor(title){ |
| 81 | + this._ = {}; |
| 82 | + |
| 83 | + this._.component = (function(){ |
| 84 | + var div = document.createElement('div'); |
| 85 | + |
| 86 | + var subDiv = document.createElement('div'); |
| 87 | + div.appendChild(subDiv); |
| 88 | + |
| 89 | + var statusIcon = document.createElement('span'); |
| 90 | + statusIcon.setAttribute('uk-spinner','ratio: .7'); |
| 91 | + subDiv.appendChild(statusIcon); |
| 92 | + |
| 93 | + var processTitle = document.createElement('span'); |
| 94 | + if(title){ |
| 95 | + processTitle.innerText = ' '+title; |
| 96 | + } |
| 97 | + subDiv.appendChild(processTitle); |
| 98 | + |
| 99 | + var processElements = document.createElement('div'); |
| 100 | + processElements.classList.add('uk-margin-medium-left'); |
| 101 | + div.appendChild(processElements); |
| 102 | + |
| 103 | + return { |
| 104 | + success: function(){ |
| 105 | + statusIcon.removeAttribute('uk-spinner'); |
| 106 | + statusIcon.classList.remove('uk-spinner'); |
| 107 | + statusIcon.setAttribute('uk-icon','icon: check'); |
| 108 | + subDiv.classList.add('uk-text-success'); |
| 109 | + }, |
| 110 | + failure: function(){ |
| 111 | + statusIcon.removeAttribute('uk-spinner'); |
| 112 | + statusIcon.classList.remove('uk-spinner'); |
| 113 | + statusIcon.setAttribute('uk-icon','icon: close'); |
| 114 | + subDiv.classList.add('uk-text-danger'); |
| 115 | + }, |
| 116 | + updateTitle: function(){ |
| 117 | + processTitle.innerText = ' '+title |
| 118 | + }, |
| 119 | + getProcessElement: function(){ |
| 120 | + return processElements; |
| 121 | + }, |
| 122 | + getElement: function(){ |
| 123 | + return div; |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + })(); |
| 128 | + |
| 129 | + var parentData = this._; |
| 130 | + this._.status = ProcessStatus.INPROGRESS; |
| 131 | + this._.promise = (function(){ |
| 132 | + var resolve; |
| 133 | + var reject; |
| 134 | + var promise = new Promise(function(res,rej){ |
| 135 | + resolve = res; |
| 136 | + reject = rej; |
| 137 | + }); |
| 138 | + |
| 139 | + return { |
| 140 | + get: function(){ |
| 141 | + return promise; |
| 142 | + }, |
| 143 | + resolve: function(){ |
| 144 | + resolve.apply(undefined,arguments); |
| 145 | + }, |
| 146 | + reject: function(){ |
| 147 | + reject.apply(undefined,arguments); |
| 148 | + } |
| 149 | + }; |
| 150 | + })(); |
| 151 | + |
| 152 | + this._.processList = (function(){ |
| 153 | + var processList = []; |
| 154 | + var resolvedCount = 0; |
| 155 | + var successCount = 0; |
| 156 | + var failureCount = 0; |
| 157 | + var parent = parentData; |
| 158 | + |
| 159 | + var successCallback = function(){ |
| 160 | + resolvedCount++; |
| 161 | + successCount++; |
| 162 | + if(resolvedCount == processList.length){ |
| 163 | + if(failureCount == 0){ |
| 164 | + parent.status = ProcessStatus.SUCCESS; |
| 165 | + parent.component.success(); |
| 166 | + parent.promise.resolve(); |
| 167 | + } else { |
| 168 | + parent.status = ProcessStatus.FAILED; |
| 169 | + parent.component.failure(); |
| 170 | + parent.promise.reject(); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + var failureCallback = function(){ |
| 176 | + resolvedCount++; |
| 177 | + failureCount++; |
| 178 | + if(resolvedCount == processList.length){ |
| 179 | + parent.status = ProcessStatus.FAILED; |
| 180 | + parent.component.failure(); |
| 181 | + parent.promise.reject(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return { |
| 186 | + push : function(process){ |
| 187 | + if(process instanceof Process || process instanceof ProcessQueue){ |
| 188 | + process.then(successCallback, failureCallback); |
| 189 | + } else { |
| 190 | + throw new TypeError('Invalid process type pushed to the queue'); |
| 191 | + } |
| 192 | + processList.push(process); |
| 193 | + process.appendToElement(parent.component.getProcessElement()); |
| 194 | + } |
| 195 | + } |
| 196 | + })(); |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + appendToElement(element){ |
| 201 | + element.appendChild(this._.component.getElement()); |
| 202 | + } |
| 203 | + |
| 204 | + push(process){ |
| 205 | + this._.processList.push(process); |
| 206 | + } |
| 207 | + |
| 208 | + updateTitle(title){ |
| 209 | + this._.component.updateTitle(title); |
| 210 | + } |
| 211 | + |
| 212 | + then(resolve, reject){ |
| 213 | + this._.promise.get().then(resolve, reject); |
| 214 | + } |
| 215 | + |
| 216 | + catch(reject){ |
| 217 | + this._.promise.get().catch(reject); |
| 218 | + } |
| 219 | + |
| 220 | + getStatus(){ |
| 221 | + return this._.status; |
| 222 | + } |
| 223 | +} |
| 224 | +ProcessStatus = Object.freeze({ |
| 225 | + 'INPROGRESS':'INPROGRESS', |
| 226 | + 'SUCCESS':'SUCCESS', |
| 227 | + 'FAILED':'FAILED' |
| 228 | +}); |
0 commit comments