admin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. smf_AdminIndex(oOptions)
  3. {
  4. public init()
  5. public loadAdminIndex()
  6. public setAnnouncements()
  7. public showCurrentVersion()
  8. public checkUpdateAvailable()
  9. }
  10. smf_ViewVersions(oOptions)
  11. {
  12. public init()
  13. public loadViewVersions
  14. public swapOption(oSendingElement, sName)
  15. public compareVersions(sCurrent, sTarget)
  16. public determineVersions()
  17. }
  18. */
  19. // Handle the JavaScript surrounding the admin and moderation center.
  20. function smf_AdminIndex(oOptions)
  21. {
  22. this.opt = oOptions;
  23. this.init();
  24. }
  25. smf_AdminIndex.prototype.init = function ()
  26. {
  27. window.adminIndexInstanceRef = this;
  28. var fHandlePageLoaded = function () {
  29. window.adminIndexInstanceRef.loadAdminIndex();
  30. }
  31. addLoadEvent(fHandlePageLoaded);
  32. }
  33. smf_AdminIndex.prototype.loadAdminIndex = function ()
  34. {
  35. // Load the text box containing the latest news items.
  36. if (this.opt.bLoadAnnouncements)
  37. this.setAnnouncements();
  38. // Load the current SMF and your SMF version numbers.
  39. if (this.opt.bLoadVersions)
  40. this.showCurrentVersion();
  41. // Load the text box that sais there's a new version available.
  42. if (this.opt.bLoadUpdateNotification)
  43. this.checkUpdateAvailable();
  44. }
  45. smf_AdminIndex.prototype.setAnnouncements = function ()
  46. {
  47. if (!('smfAnnouncements' in window) || !('length' in window.smfAnnouncements))
  48. return;
  49. var sMessages = '';
  50. for (var i = 0; i < window.smfAnnouncements.length; i++)
  51. sMessages += this.opt.sAnnouncementMessageTemplate.replace('%href%', window.smfAnnouncements[i].href).replace('%subject%', window.smfAnnouncements[i].subject).replace('%time%', window.smfAnnouncements[i].time).replace('%message%', window.smfAnnouncements[i].message);
  52. setInnerHTML(document.getElementById(this.opt.sAnnouncementContainerId), this.opt.sAnnouncementTemplate.replace('%content%', sMessages));
  53. }
  54. smf_AdminIndex.prototype.showCurrentVersion = function ()
  55. {
  56. if (!('smfVersion' in window))
  57. return;
  58. var oSmfVersionContainer = document.getElementById(this.opt.sSmfVersionContainerId);
  59. var oYourVersionContainer = document.getElementById(this.opt.sYourVersionContainerId);
  60. setInnerHTML(oSmfVersionContainer, window.smfVersion);
  61. var sCurrentVersion = getInnerHTML(oYourVersionContainer);
  62. if (sCurrentVersion != window.smfVersion)
  63. setInnerHTML(oYourVersionContainer, this.opt.sVersionOutdatedTemplate.replace('%currentVersion%', sCurrentVersion));
  64. }
  65. smf_AdminIndex.prototype.checkUpdateAvailable = function ()
  66. {
  67. if (!('smfUpdatePackage' in window))
  68. return;
  69. var oContainer = document.getElementById(this.opt.sUpdateNotificationContainerId);
  70. // Are we setting a custom title and message?
  71. var sTitle = 'smfUpdateTitle' in window ? window.smfUpdateTitle : this.opt.sUpdateNotificationDefaultTitle;
  72. var sMessage = 'smfUpdateNotice' in window ? window.smfUpdateNotice : this.opt.sUpdateNotificationDefaultMessage;
  73. setInnerHTML(oContainer, this.opt.sUpdateNotificationTemplate.replace('%title%', sTitle).replace('%message%', sMessage));
  74. // Parse in the package download URL if it exists in the string.
  75. document.getElementById('update-link').href = this.opt.sUpdateNotificationLink.replace('%package%', window.smfUpdatePackage);
  76. // If we decide to override life into "red" mode, do it.
  77. if ('smfUpdateCritical' in window)
  78. {
  79. document.getElementById('update_table').style.backgroundColor = '#aa2222';
  80. document.getElementById('update_title').style.backgroundColor = '#dd2222';
  81. document.getElementById('update_title').style.color = 'white';
  82. document.getElementById('update_message').style.backgroundColor = '#eebbbb';
  83. document.getElementById('update_message').style.color = 'black';
  84. }
  85. }
  86. function smf_ViewVersions (oOptions)
  87. {
  88. this.opt = oOptions;
  89. this.oSwaps = {};
  90. this.init();
  91. }
  92. smf_ViewVersions.prototype.init = function ()
  93. {
  94. // Load this on loading of the page.
  95. window.viewVersionsInstanceRef = this;
  96. var fHandlePageLoaded = function () {
  97. window.viewVersionsInstanceRef.loadViewVersions();
  98. }
  99. addLoadEvent(fHandlePageLoaded);
  100. }
  101. smf_ViewVersions.prototype.loadViewVersions = function ()
  102. {
  103. this.determineVersions();
  104. }
  105. smf_ViewVersions.prototype.swapOption = function (oSendingElement, sName)
  106. {
  107. // If it is undefined, or currently off, turn it on - otherwise off.
  108. this.oSwaps[sName] = !(sName in this.oSwaps) || !this.oSwaps[sName];
  109. document.getElementById(sName).style.display = this.oSwaps[sName] ? '' : 'none';
  110. // Unselect the link and return false.
  111. oSendingElement.blur();
  112. return false;
  113. }
  114. smf_ViewVersions.prototype.compareVersions = function (sCurrent, sTarget)
  115. {
  116. var aVersions = aParts = new Array();
  117. var aCompare = new Array(sCurrent, sTarget);
  118. for (var i = 0; i < 2; i++)
  119. {
  120. // Clean the version and extract the version parts.
  121. var sClean = aCompare[i].toLowerCase().replace(/ /g, '').replace(/2.0rc1-1/, '2.0rc1.1');
  122. aParts = sClean.match(/(\d+)(?:\.(\d+|))?(?:\.)?(\d+|)(?:(alpha|beta|rc)(\d+|)(?:\.)?(\d+|))?(?:(dev))?(\d+|)/);
  123. // No matches?
  124. if (aParts == null)
  125. return false;
  126. // Build an array of parts.
  127. aVersions[i] = [
  128. aParts[1] > 0 ? parseInt(aParts[1]) : 0,
  129. aParts[2] > 0 ? parseInt(aParts[2]) : 0,
  130. aParts[3] > 0 ? parseInt(aParts[3]) : 0,
  131. typeof(aParts[4]) == 'undefined' ? 'stable' : aParts[4],
  132. aParts[5] > 0 ? parseInt(aParts[5]) : 0,
  133. aParts[6] > 0 ? parseInt(aParts[6]) : 0,
  134. typeof(aParts[7]) != 'undefined',
  135. ];
  136. }
  137. // Loop through each category.
  138. for (i = 0; i < 7; i++)
  139. {
  140. // Is there something for us to calculate?
  141. if (aVersions[0][i] != aVersions[1][i])
  142. {
  143. // Dev builds are a problematic exception.
  144. // (stable) dev < (stable) but (unstable) dev = (unstable)
  145. if (i == 3)
  146. return aVersions[0][i] < aVersions[1][i] ? !aVersions[1][6] : aVersions[0][6];
  147. else if (i == 6)
  148. return aVersions[0][6] ? aVersions[1][3] == 'stable' : false;
  149. // Otherwise a simple comparison.
  150. else
  151. return aVersions[0][i] < aVersions[1][i];
  152. }
  153. }
  154. // They are the same!
  155. return false;
  156. }
  157. smf_ViewVersions.prototype.determineVersions = function ()
  158. {
  159. var oHighYour = {
  160. Sources: '??',
  161. Default: '??',
  162. Languages: '??',
  163. Templates: '??'
  164. };
  165. var oHighCurrent = {
  166. Sources: '??',
  167. Default: '??',
  168. Languages: '??',
  169. Templates: '??'
  170. };
  171. var oLowVersion = {
  172. Sources: false,
  173. Default: false,
  174. Languages: false,
  175. Templates: false
  176. };
  177. var sSections = [
  178. 'Sources',
  179. 'Default',
  180. 'Languages',
  181. 'Templates'
  182. ];
  183. for (var i = 0, n = sSections.length; i < n; i++)
  184. {
  185. // Collapse all sections.
  186. var oSection = document.getElementById(sSections[i]);
  187. if (typeof(oSection) == 'object' && oSection != null)
  188. oSection.style.display = 'none';
  189. // Make all section links clickable.
  190. var oSectionLink = document.getElementById(sSections[i] + '-link');
  191. if (typeof(oSectionLink) == 'object' && oSectionLink != null)
  192. {
  193. oSectionLink.instanceRef = this;
  194. oSectionLink.sSection = sSections[i];
  195. oSectionLink.onclick = function () {
  196. this.instanceRef.swapOption(this, this.sSection);
  197. return false;
  198. };
  199. }
  200. }
  201. if (!('smfVersions' in window))
  202. window.smfVersions = {};
  203. for (var sFilename in window.smfVersions)
  204. {
  205. if (!document.getElementById('current' + sFilename))
  206. continue;
  207. var sYourVersion = getInnerHTML(document.getElementById('your' + sFilename));
  208. var sCurVersionType;
  209. for (var sVersionType in oLowVersion)
  210. if (sFilename.substr(0, sVersionType.length) == sVersionType)
  211. {
  212. sCurVersionType = sVersionType;
  213. break;
  214. }
  215. if (typeof(sCurVersionType) != 'undefined')
  216. {
  217. if ((this.compareVersions(oHighYour[sCurVersionType], sYourVersion) || oHighYour[sCurVersionType] == '??') && !oLowVersion[sCurVersionType])
  218. oHighYour[sCurVersionType] = sYourVersion;
  219. if (this.compareVersions(oHighCurrent[sCurVersionType], smfVersions[sFilename]) || oHighCurrent[sCurVersionType] == '??')
  220. oHighCurrent[sCurVersionType] = smfVersions[sFilename];
  221. if (this.compareVersions(sYourVersion, smfVersions[sFilename]))
  222. {
  223. oLowVersion[sCurVersionType] = sYourVersion;
  224. document.getElementById('your' + sFilename).style.color = 'red';
  225. }
  226. }
  227. else if (this.compareVersions(sYourVersion, smfVersions[sFilename]))
  228. oLowVersion[sCurVersionType] = sYourVersion;
  229. setInnerHTML(document.getElementById('current' + sFilename), smfVersions[sFilename]);
  230. setInnerHTML(document.getElementById('your' + sFilename), sYourVersion);
  231. }
  232. if (!('smfLanguageVersions' in window))
  233. window.smfLanguageVersions = {};
  234. for (sFilename in window.smfLanguageVersions)
  235. {
  236. for (var i = 0; i < this.opt.aKnownLanguages.length; i++)
  237. {
  238. if (!document.getElementById('current' + sFilename + this.opt.aKnownLanguages[i]))
  239. continue;
  240. setInnerHTML(document.getElementById('current' + sFilename + this.opt.aKnownLanguages[i]), smfLanguageVersions[sFilename]);
  241. sYourVersion = getInnerHTML(document.getElementById('your' + sFilename + this.opt.aKnownLanguages[i]));
  242. setInnerHTML(document.getElementById('your' + sFilename + this.opt.aKnownLanguages[i]), sYourVersion);
  243. if ((this.compareVersions(oHighYour.Languages, sYourVersion) || oHighYour.Languages == '??') && !oLowVersion.Languages)
  244. oHighYour.Languages = sYourVersion;
  245. if (this.compareVersions(oHighCurrent.Languages, smfLanguageVersions[sFilename]) || oHighCurrent.Languages == '??')
  246. oHighCurrent.Languages = smfLanguageVersions[sFilename];
  247. if (this.compareVersions(sYourVersion, smfLanguageVersions[sFilename]))
  248. {
  249. oLowVersion.Languages = sYourVersion;
  250. document.getElementById('your' + sFilename + this.opt.aKnownLanguages[i]).style.color = 'red';
  251. }
  252. }
  253. }
  254. setInnerHTML(document.getElementById('yourSources'), oLowVersion.Sources ? oLowVersion.Sources : oHighYour.Sources);
  255. setInnerHTML(document.getElementById('currentSources'), oHighCurrent.Sources);
  256. if (oLowVersion.Sources)
  257. document.getElementById('yourSources').style.color = 'red';
  258. setInnerHTML(document.getElementById('yourDefault'), oLowVersion.Default ? oLowVersion.Default : oHighYour.Default);
  259. setInnerHTML(document.getElementById('currentDefault'), oHighCurrent.Default);
  260. if (oLowVersion.Default)
  261. document.getElementById('yourDefault').style.color = 'red';
  262. if (document.getElementById('Templates'))
  263. {
  264. setInnerHTML(document.getElementById('yourTemplates'), oLowVersion.Templates ? oLowVersion.Templates : oHighYour.Templates);
  265. setInnerHTML(document.getElementById('currentTemplates'), oHighCurrent.Templates);
  266. if (oLowVersion.Templates)
  267. document.getElementById('yourTemplates').style.color = 'red';
  268. }
  269. setInnerHTML(document.getElementById('yourLanguages'), oLowVersion.Languages ? oLowVersion.Languages : oHighYour.Languages);
  270. setInnerHTML(document.getElementById('currentLanguages'), oHighCurrent.Languages);
  271. if (oLowVersion.Languages)
  272. document.getElementById('yourLanguages').style.color = 'red';
  273. }