checkupgrade.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /** https://github.com/dankogai/js-base64 BSD 3-Clause "New" or "Revised" License Copyright (c) 2014, Dan Kogai All rights reserved. */
  2. (function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory(global):typeof define==="function"&&define.amd?define(factory):factory(global)})(typeof self!=="undefined"?self:typeof window!=="undefined"?window:typeof global!=="undefined"?global:this,function(global){"use strict";global=global||{};var _Base64=global.Base64;var version="2.5.1";var buffer;if(typeof module!=="undefined"&&module.exports){try{buffer=eval("require('buffer').Buffer")}catch(err){buffer=undefined}}var b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64tab=function(bin){var t={};for(var i=0,l=bin.length;i<l;i++)t[bin.charAt(i)]=i;return t}(b64chars);var fromCharCode=String.fromCharCode;var cb_utob=function(c){if(c.length<2){var cc=c.charCodeAt(0);return cc<128?c:cc<2048?fromCharCode(192|cc>>>6)+fromCharCode(128|cc&63):fromCharCode(224|cc>>>12&15)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}else{var cc=65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320);return fromCharCode(240|cc>>>18&7)+fromCharCode(128|cc>>>12&63)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}};var re_utob=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob=function(u){return u.replace(re_utob,cb_utob)};var cb_encode=function(ccc){var padlen=[0,2,1][ccc.length%3],ord=ccc.charCodeAt(0)<<16|(ccc.length>1?ccc.charCodeAt(1):0)<<8|(ccc.length>2?ccc.charCodeAt(2):0),chars=[b64chars.charAt(ord>>>18),b64chars.charAt(ord>>>12&63),padlen>=2?"=":b64chars.charAt(ord>>>6&63),padlen>=1?"=":b64chars.charAt(ord&63)];return chars.join("")};var btoa=global.btoa?function(b){return global.btoa(b)}:function(b){return b.replace(/[\s\S]{1,3}/g,cb_encode)};var _encode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(u){return(u.constructor===buffer.constructor?u:buffer.from(u)).toString("base64")}:function(u){return(u.constructor===buffer.constructor?u:new buffer(u)).toString("base64")}:function(u){return btoa(utob(u))};var encode=function(u,urisafe){return!urisafe?_encode(String(u)):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=new RegExp(["[À-ß][€-¿]","[à-ï][€-¿]{2}","[ð-÷][€-¿]{3}"].join("|"),"g");var cb_btou=function(cccc){switch(cccc.length){case 4:var cp=(7&cccc.charCodeAt(0))<<18|(63&cccc.charCodeAt(1))<<12|(63&cccc.charCodeAt(2))<<6|63&cccc.charCodeAt(3),offset=cp-65536;return fromCharCode((offset>>>10)+55296)+fromCharCode((offset&1023)+56320);case 3:return fromCharCode((15&cccc.charCodeAt(0))<<12|(63&cccc.charCodeAt(1))<<6|63&cccc.charCodeAt(2));default:return fromCharCode((31&cccc.charCodeAt(0))<<6|63&cccc.charCodeAt(1))}};var btou=function(b){return b.replace(re_btou,cb_btou)};var cb_decode=function(cccc){var len=cccc.length,padlen=len%4,n=(len>0?b64tab[cccc.charAt(0)]<<18:0)|(len>1?b64tab[cccc.charAt(1)]<<12:0)|(len>2?b64tab[cccc.charAt(2)]<<6:0)|(len>3?b64tab[cccc.charAt(3)]:0),chars=[fromCharCode(n>>>16),fromCharCode(n>>>8&255),fromCharCode(n&255)];chars.length-=[0,0,2,1][padlen];return chars.join("")};var _atob=global.atob?function(a){return global.atob(a)}:function(a){return a.replace(/\S{1,4}/g,cb_decode)};var atob=function(a){return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g,""))};var _decode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(a){return(a.constructor===buffer.constructor?a:buffer.from(a,"base64")).toString()}:function(a){return(a.constructor===buffer.constructor?a:new buffer(a,"base64")).toString()}:function(a){return btou(_atob(a))};var decode=function(a){return _decode(String(a).replace(/[-_]/g,function(m0){return m0=="-"?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))};var noConflict=function(){var Base64=global.Base64;global.Base64=_Base64;return Base64};global.Base64={VERSION:version,atob:atob,btoa:btoa,fromBase64:decode,toBase64:encode,utob:utob,encode:encode,encodeURI:encodeURI,btou:btou,decode:decode,noConflict:noConflict,__buffer__:buffer};if(typeof Object.defineProperty==="function"){var noEnum=function(v){return{value:v,enumerable:false,writable:true,configurable:true}};global.Base64.extendString=function(){Object.defineProperty(String.prototype,"fromBase64",noEnum(function(){return decode(this)}));Object.defineProperty(String.prototype,"toBase64",noEnum(function(urisafe){return encode(this,urisafe)}));Object.defineProperty(String.prototype,"toBase64URI",noEnum(function(){return encode(this,true)}))}}if(global["Meteor"]){Base64=global.Base64}if(typeof module!=="undefined"&&module.exports){module.exports.Base64=global.Base64}else if(typeof define==="function"&&define.amd){define([],function(){return global.Base64})}return{Base64:global.Base64}});
  3. $(function()
  4. {
  5. var lastestVersion, isNewVersion, serverNotAvaliable = true;
  6. if(serverVersions && $.isArray(serverVersions))
  7. {
  8. serverNotAvaliable = false;
  9. if(serverVersions.length)
  10. {
  11. serverVersions.sort(function(v1, v2)
  12. {
  13. return window.compareVersions(v2.xxcVersion, v1.xxcVersion);
  14. });
  15. var lastestVersion = serverVersions[0];
  16. if(currentVersion && window.compareVersions(currentVersion.version, lastestVersion.xxcVersion) > 0)
  17. {
  18. isNewVersion = true;
  19. lastestVersion = null;
  20. }
  21. else
  22. {
  23. var newVersions = [];
  24. for(var i = 0; i < serverVersions.length; ++i)
  25. {
  26. var serverVersion = serverVersions[i];
  27. if(currentVersion && window.compareVersions(currentVersion.version, serverVersion.xxcVersion) >= 0)
  28. {
  29. break;
  30. }
  31. newVersions.push(serverVersion);
  32. }
  33. lastestVersion.newVersions = newVersions;
  34. }
  35. }
  36. else
  37. {
  38. isNewVersion = true;
  39. }
  40. }
  41. if(serverNotAvaliable) $('#errorBox').removeClass('hidden');
  42. else if(isNewVersion) $('#alreadyNewBox').removeClass('hidden');
  43. else
  44. {
  45. var $messageBox = $('#messageBox');
  46. $messageBox.removeClass('hidden');
  47. var $updateDetails = $('#updateDetails');
  48. $updateDetails.on('hide.zui.collapse', function()
  49. {
  50. $('#updateDetailsBtn>.icon').removeClass('icon-double-angle-up').addClass('icon-double-angle-down');
  51. }).on('show.zui.collapse', function()
  52. {
  53. $('#updateDetailsBtn>.icon').removeClass('icon-double-angle-down').addClass('icon-double-angle-up');
  54. });
  55. var $updateForm = $('#updateForm');
  56. $updateForm.on('show.zui.collapse', function()
  57. {
  58. $messageBox.find('.content>.actions').addClass('hidden');
  59. if($updateDetails.hasClass('in')) $updateDetails.collapse('hide');
  60. });
  61. $('.text-new-version').text(lastestVersion.xxcVersion);
  62. $('.text-version-summary').html(window.marked(lastestVersion.main));
  63. var $updateDetailsContent = $updateDetails.children('.article-content');
  64. var changeLogs = [];
  65. $.each(lastestVersion.newVersions, function(index, newVersion)
  66. {
  67. if(newVersion.xxcDesc)
  68. {
  69. if(lastestVersion.newVersions.length > 1)
  70. {
  71. $updateDetailsContent.append('<h3 class="text-important"' + (!index ? ' style="margin-top: 0"' : '') + '>v' + newVersion.xxcVersion + '</h3>');
  72. changeLogs.push('### v' + newVersion.xxcVersion);
  73. if(newVersion.main)
  74. {
  75. $updateDetailsContent.append('<div style=" padding: 5px 10px; background: rgba(255,255,255,.3); margin-bottom: 10px; color: #666; border-radius: 4px;">' + window.marked(newVersion.main) + '</div>');
  76. changeLogs.push(newVersion.main);
  77. }
  78. }
  79. $updateDetailsContent.append('<div>' + window.marked(newVersion.xxcDesc) + '</div>');
  80. changeLogs.push(newVersion.xxcDesc);
  81. }
  82. });
  83. lastestVersion.changeLog = changeLogs.join('\n\n');
  84. var $downloadBtn = $('#downloadBtn');
  85. var $downloadTypes = $('#downloadTypes [type="checkbox"]');
  86. var $strategies = $('#strategies');
  87. $strategies.find('[type="radio"]:first').prop('checked', 'checked');
  88. $.each($downloadTypes, function()
  89. {
  90. var $this = $(this);
  91. var downloadType = $this.val();
  92. if(!lastestVersion.xxcDownload[downloadType]) $this.closest('.checkbox').addClass('hidden').prop('checked', false).attr('disabled', 'disabled');
  93. });
  94. $downloadTypes.on('click', function ()
  95. {
  96. var checkboxStatus = false;
  97. $downloadTypes.each(function()
  98. {
  99. if($(this).prop("checked") === true) checkboxStatus = true;
  100. });
  101. $('#downloadBtn').attr('disabled', !checkboxStatus);
  102. })
  103. if(!$('#downloadTypes .checkbox:not(.hidden)').length) $messageBox.find('.content>.actions').addClass('hidden');
  104. var downloadInfos = [];
  105. var serverClient = null;
  106. var updateDownloads = function()
  107. {
  108. var count = downloadInfos.length;
  109. if (!count) return;
  110. var successCount = 0;
  111. var errorCount = 0;
  112. $.each(downloadInfos, function(index, downloadInfo)
  113. {
  114. var $checkbox = $downloadTypes.filter('[data-type="' + downloadInfo.type + '"]');
  115. var $info = $checkbox.closest('.checkbox').children('.info').empty();
  116. if(downloadInfo.status === 'downloaded')
  117. {
  118. successCount++;
  119. $info.html('<i class="icon icon-check-circle"></i> ' + downloadSuccessText).attr('class', 'info text-success');
  120. $checkbox.addClass('is-downloaded');
  121. }
  122. else if(downloadInfo.status === 'failed')
  123. {
  124. errorCount++;
  125. $info.html('<i class="icon icon-exclamation-sign"></i> ' + downloadFailText + '(' + downloadInfo.errorMessage + ')').attr('class', 'info text-error');
  126. $info.siblings('.errorInfo').show();
  127. }
  128. else if(downloadInfo.status === 'downloading')
  129. {
  130. $info.html('<i class="icon icon-spin icon-spinner-indicator"></i> ' + downloadingText).attr('class', 'info text-info');
  131. }
  132. });
  133. var finishCount = successCount + errorCount;
  134. if(errorCount === count)
  135. {
  136. $downloadBtn.text(downloadText).removeClass('disabled');
  137. $downloadTypes.attr('disabled', null);
  138. }
  139. else if(finishCount < count)
  140. {
  141. $downloadBtn.text(downloadingText + ' ' + Math.floor(100 * finishCount / count) + '%');
  142. }
  143. else
  144. {
  145. $downloadBtn.addClass('hidden');
  146. $('#updateBtns').removeClass('hidden');
  147. }
  148. };
  149. var downloadZip = function(downloadInfo)
  150. {
  151. var downloadUrl = downloadInfo.download;
  152. // var downloadMd5;
  153. if(typeof downloadUrl === 'object')
  154. {
  155. // downloadMd5 = downloadUrl.md5;
  156. downloadUrl = downloadUrl.url;
  157. }
  158. if (!downloadUrl)
  159. {
  160. downloadInfo.status = 'failed';
  161. updateDownloads();
  162. return;
  163. }
  164. downloadInfo.status = 'downloading';
  165. var url = createLink('client', 'download', 'version=' + lastestVersion.xxcVersion + '&link=' + Base64.encode(downloadUrl) + '&os=' + downloadInfo.type)
  166. $.ajax(
  167. {
  168. type: 'GET',
  169. url: url,
  170. timeout: 0,
  171. dataType: 'json',
  172. success: function(data)
  173. {
  174. if(data && data.result === 'success')
  175. {
  176. downloadInfo.status = 'downloaded';
  177. serverClient = $.extend({}, serverClient, data.client);
  178. }
  179. else
  180. {
  181. downloadInfo.status = 'failed';
  182. if(data && data.message) downloadInfo.errorMessage = data.message;
  183. else downloadInfo.errorMessage = 'Cannot download ' + downloadInfo.type + ' package from ' + downloadUrl;
  184. }
  185. updateDownloads();
  186. },
  187. error: function()
  188. {
  189. downloadInfo.status = 'failed';
  190. downloadInfo.errorMessage = downloadUrl;
  191. updateDownloads();
  192. }
  193. });
  194. updateDownloads();
  195. };
  196. $downloadBtn.on('click', function()
  197. {
  198. serverClient = {};
  199. downloadInfos = [];
  200. $downloadTypes.attr('disabled', 'disabled').filter(':checked').not('.is-downloaded').each(function()
  201. {
  202. var $checkbox = $(this);
  203. var downloadType = $checkbox.val();
  204. downloadInfos.push(
  205. {
  206. type: downloadType,
  207. download: lastestVersion.xxcDownload[downloadType],
  208. status: 'waiting'
  209. });
  210. });
  211. $('#downloadBtn').text(downloadingText).addClass('disabled');
  212. $.each(downloadInfos, function(index, downloadInfo)
  213. {
  214. setTimeout(function()
  215. {
  216. downloadZip(downloadInfo);
  217. }, index * 1000)
  218. });
  219. });
  220. var serverDownloads;
  221. var saveClientUpdate = function(isRelease)
  222. {
  223. if(isRelease !== undefined) serverClient.status = isRelease ? 'released' : 'wait';
  224. serverClient.desc = lastestVersion.main;
  225. serverClient.changeLog = lastestVersion.changeLog;
  226. serverClient.strategy = $strategies.find('[type="radio"]:checked').val();
  227. var downloads = serverDownloads || serverClient.downloads;
  228. serverDownloads = downloads;
  229. if(typeof downloads === 'string') downloads = $.parseJSON(downloads);
  230. delete serverClient.createdBy;
  231. delete serverClient.createdDate;
  232. delete serverClient.editedBy;
  233. delete serverClient.editedDate;
  234. delete serverClient.downloads;
  235. $downloadTypes.filter(':checked').each(function()
  236. {
  237. var $checkbox = $(this);
  238. var downloadType = $checkbox.val();
  239. serverClient['downloads[' + downloadType + ']'] = downloads[downloadType];
  240. });
  241. $('#updateBtns').addClass('disabled');
  242. var $btn = $(isRelease ? '#publishUpdateBtn' : '#saveUpdateBtn');
  243. var btnText = $btn.text();
  244. $btn.text(submittingText);
  245. $.ajax(
  246. {
  247. type: 'POST',
  248. url: createLink('client', 'edit', 'id=' + serverClient.id),
  249. dataType: 'json',
  250. data: serverClient,
  251. success: function(data)
  252. {
  253. if(data && data.result === 'success')
  254. {
  255. $btn.popover('show');
  256. setTimeout(function() {window.location.href = createLink('client', 'browse');}, 3000);
  257. }
  258. else
  259. {
  260. $.zui.messager.danger((data && data.message) || lang.timeout);
  261. $('#updateBtns').removeClass('disabled');
  262. }
  263. $btn.text(btnText);
  264. },
  265. error: function()
  266. {
  267. $.zui.messager.danger(lang.timeout);
  268. $('#updateBtns').removeClass('disabled');
  269. $btn.text(btnText);
  270. }
  271. });
  272. };
  273. $('#publishUpdateBtn').on('click', function() {saveClientUpdate(true);});
  274. $('#saveUpdateBtn').on('click', function() {saveClientUpdate(false);});
  275. }
  276. });