Version.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-Version'>/**
  19. </span> * @author Jacky Nguyen &lt;jacky@sencha.com&gt;
  20. * @docauthor Jacky Nguyen &lt;jacky@sencha.com&gt;
  21. * @class Ext.Version
  22. *
  23. * A utility class that wrap around a string version number and provide convenient
  24. * method to perform comparison. See also: {@link Ext.Version#compare compare}. Example:
  25. *
  26. * var version = new Ext.Version('1.0.2beta');
  27. * console.log(&quot;Version is &quot; + version); // Version is 1.0.2beta
  28. *
  29. * console.log(version.getMajor()); // 1
  30. * console.log(version.getMinor()); // 0
  31. * console.log(version.getPatch()); // 2
  32. * console.log(version.getBuild()); // 0
  33. * console.log(version.getRelease()); // beta
  34. *
  35. * console.log(version.isGreaterThan('1.0.1')); // True
  36. * console.log(version.isGreaterThan('1.0.2alpha')); // True
  37. * console.log(version.isGreaterThan('1.0.2RC')); // False
  38. * console.log(version.isGreaterThan('1.0.2')); // False
  39. * console.log(version.isLessThan('1.0.2')); // True
  40. *
  41. * console.log(version.match(1.0)); // True
  42. * console.log(version.match('1.0.2')); // True
  43. *
  44. */
  45. (function() {
  46. // Current core version
  47. var version = '4.1.1', Version;
  48. Ext.Version = Version = Ext.extend(Object, {
  49. <span id='Ext-Version-method-constructor'> /**
  50. </span> * @param {String/Number} version The version number in the following standard format:
  51. *
  52. * major[.minor[.patch[.build[release]]]]
  53. *
  54. * Examples:
  55. *
  56. * 1.0
  57. * 1.2.3beta
  58. * 1.2.3.4RC
  59. *
  60. * @return {Ext.Version} this
  61. */
  62. constructor: function(version) {
  63. var parts, releaseStartIndex;
  64. if (version instanceof Version) {
  65. return version;
  66. }
  67. this.version = this.shortVersion = String(version).toLowerCase().replace(/_/g, '.').replace(/[\-+]/g, '');
  68. releaseStartIndex = this.version.search(/([^\d\.])/);
  69. if (releaseStartIndex !== -1) {
  70. this.release = this.version.substr(releaseStartIndex, version.length);
  71. this.shortVersion = this.version.substr(0, releaseStartIndex);
  72. }
  73. this.shortVersion = this.shortVersion.replace(/[^\d]/g, '');
  74. parts = this.version.split('.');
  75. this.major = parseInt(parts.shift() || 0, 10);
  76. this.minor = parseInt(parts.shift() || 0, 10);
  77. this.patch = parseInt(parts.shift() || 0, 10);
  78. this.build = parseInt(parts.shift() || 0, 10);
  79. return this;
  80. },
  81. <span id='Ext-Version-method-toString'> /**
  82. </span> * Override the native toString method
  83. * @private
  84. * @return {String} version
  85. */
  86. toString: function() {
  87. return this.version;
  88. },
  89. <span id='Ext-Version-method-valueOf'> /**
  90. </span> * Override the native valueOf method
  91. * @private
  92. * @return {String} version
  93. */
  94. valueOf: function() {
  95. return this.version;
  96. },
  97. <span id='Ext-Version-method-getMajor'> /**
  98. </span> * Returns the major component value
  99. * @return {Number} major
  100. */
  101. getMajor: function() {
  102. return this.major || 0;
  103. },
  104. <span id='Ext-Version-method-getMinor'> /**
  105. </span> * Returns the minor component value
  106. * @return {Number} minor
  107. */
  108. getMinor: function() {
  109. return this.minor || 0;
  110. },
  111. <span id='Ext-Version-method-getPatch'> /**
  112. </span> * Returns the patch component value
  113. * @return {Number} patch
  114. */
  115. getPatch: function() {
  116. return this.patch || 0;
  117. },
  118. <span id='Ext-Version-method-getBuild'> /**
  119. </span> * Returns the build component value
  120. * @return {Number} build
  121. */
  122. getBuild: function() {
  123. return this.build || 0;
  124. },
  125. <span id='Ext-Version-method-getRelease'> /**
  126. </span> * Returns the release component value
  127. * @return {Number} release
  128. */
  129. getRelease: function() {
  130. return this.release || '';
  131. },
  132. <span id='Ext-Version-method-isGreaterThan'> /**
  133. </span> * Returns whether this version if greater than the supplied argument
  134. * @param {String/Number} target The version to compare with
  135. * @return {Boolean} True if this version if greater than the target, false otherwise
  136. */
  137. isGreaterThan: function(target) {
  138. return Version.compare(this.version, target) === 1;
  139. },
  140. <span id='Ext-Version-method-isGreaterThanOrEqual'> /**
  141. </span> * Returns whether this version if greater than or equal to the supplied argument
  142. * @param {String/Number} target The version to compare with
  143. * @return {Boolean} True if this version if greater than or equal to the target, false otherwise
  144. */
  145. isGreaterThanOrEqual: function(target) {
  146. return Version.compare(this.version, target) &gt;= 0;
  147. },
  148. <span id='Ext-Version-method-isLessThan'> /**
  149. </span> * Returns whether this version if smaller than the supplied argument
  150. * @param {String/Number} target The version to compare with
  151. * @return {Boolean} True if this version if smaller than the target, false otherwise
  152. */
  153. isLessThan: function(target) {
  154. return Version.compare(this.version, target) === -1;
  155. },
  156. <span id='Ext-Version-method-isLessThanOrEqual'> /**
  157. </span> * Returns whether this version if less than or equal to the supplied argument
  158. * @param {String/Number} target The version to compare with
  159. * @return {Boolean} True if this version if less than or equal to the target, false otherwise
  160. */
  161. isLessThanOrEqual: function(target) {
  162. return Version.compare(this.version, target) &lt;= 0;
  163. },
  164. <span id='Ext-Version-method-equals'> /**
  165. </span> * Returns whether this version equals to the supplied argument
  166. * @param {String/Number} target The version to compare with
  167. * @return {Boolean} True if this version equals to the target, false otherwise
  168. */
  169. equals: function(target) {
  170. return Version.compare(this.version, target) === 0;
  171. },
  172. <span id='Ext-Version-method-match'> /**
  173. </span> * Returns whether this version matches the supplied argument. Example:
  174. *
  175. * var version = new Ext.Version('1.0.2beta');
  176. * console.log(version.match(1)); // True
  177. * console.log(version.match(1.0)); // True
  178. * console.log(version.match('1.0.2')); // True
  179. * console.log(version.match('1.0.2RC')); // False
  180. *
  181. * @param {String/Number} target The version to compare with
  182. * @return {Boolean} True if this version matches the target, false otherwise
  183. */
  184. match: function(target) {
  185. target = String(target);
  186. return this.version.substr(0, target.length) === target;
  187. },
  188. <span id='Ext-Version-method-toArray'> /**
  189. </span> * Returns this format: [major, minor, patch, build, release]. Useful for comparison
  190. * @return {Number[]}
  191. */
  192. toArray: function() {
  193. return [this.getMajor(), this.getMinor(), this.getPatch(), this.getBuild(), this.getRelease()];
  194. },
  195. <span id='Ext-Version-method-getShortVersion'> /**
  196. </span> * Returns shortVersion version without dots and release
  197. * @return {String}
  198. */
  199. getShortVersion: function() {
  200. return this.shortVersion;
  201. },
  202. <span id='Ext-Version-method-gt'> /**
  203. </span> * Convenient alias to {@link Ext.Version#isGreaterThan isGreaterThan}
  204. * @param {String/Number} target
  205. * @return {Boolean}
  206. */
  207. gt: function() {
  208. return this.isGreaterThan.apply(this, arguments);
  209. },
  210. <span id='Ext-Version-method-lt'> /**
  211. </span> * Convenient alias to {@link Ext.Version#isLessThan isLessThan}
  212. * @param {String/Number} target
  213. * @return {Boolean}
  214. */
  215. lt: function() {
  216. return this.isLessThan.apply(this, arguments);
  217. },
  218. <span id='Ext-Version-method-gtEq'> /**
  219. </span> * Convenient alias to {@link Ext.Version#isGreaterThanOrEqual isGreaterThanOrEqual}
  220. * @param {String/Number} target
  221. * @return {Boolean}
  222. */
  223. gtEq: function() {
  224. return this.isGreaterThanOrEqual.apply(this, arguments);
  225. },
  226. <span id='Ext-Version-method-ltEq'> /**
  227. </span> * Convenient alias to {@link Ext.Version#isLessThanOrEqual isLessThanOrEqual}
  228. * @param {String/Number} target
  229. * @return {Boolean}
  230. */
  231. ltEq: function() {
  232. return this.isLessThanOrEqual.apply(this, arguments);
  233. }
  234. });
  235. Ext.apply(Version, {
  236. // @private
  237. releaseValueMap: {
  238. 'dev': -6,
  239. 'alpha': -5,
  240. 'a': -5,
  241. 'beta': -4,
  242. 'b': -4,
  243. 'rc': -3,
  244. '#': -2,
  245. 'p': -1,
  246. 'pl': -1
  247. },
  248. <span id='Ext-Version-static-method-getComponentValue'> /**
  249. </span> * Converts a version component to a comparable value
  250. *
  251. * @static
  252. * @param {Object} value The value to convert
  253. * @return {Object}
  254. */
  255. getComponentValue: function(value) {
  256. return !value ? 0 : (isNaN(value) ? this.releaseValueMap[value] || value : parseInt(value, 10));
  257. },
  258. <span id='Ext-Version-static-method-compare'> /**
  259. </span> * Compare 2 specified versions, starting from left to right. If a part contains special version strings,
  260. * they are handled in the following order:
  261. * 'dev' &lt; 'alpha' = 'a' &lt; 'beta' = 'b' &lt; 'RC' = 'rc' &lt; '#' &lt; 'pl' = 'p' &lt; 'anything else'
  262. *
  263. * @static
  264. * @param {String} current The current version to compare to
  265. * @param {String} target The target version to compare to
  266. * @return {Number} Returns -1 if the current version is smaller than the target version, 1 if greater, and 0 if they're equivalent
  267. */
  268. compare: function(current, target) {
  269. var currentValue, targetValue, i;
  270. current = new Version(current).toArray();
  271. target = new Version(target).toArray();
  272. for (i = 0; i &lt; Math.max(current.length, target.length); i++) {
  273. currentValue = this.getComponentValue(current[i]);
  274. targetValue = this.getComponentValue(target[i]);
  275. if (currentValue &lt; targetValue) {
  276. return -1;
  277. } else if (currentValue &gt; targetValue) {
  278. return 1;
  279. }
  280. }
  281. return 0;
  282. }
  283. });
  284. <span id='Ext'> /**
  285. </span> * @class Ext
  286. */
  287. Ext.apply(Ext, {
  288. <span id='Ext-property-versions'> /**
  289. </span> * @private
  290. */
  291. versions: {},
  292. <span id='Ext-property-lastRegisteredVersion'> /**
  293. </span> * @private
  294. */
  295. lastRegisteredVersion: null,
  296. <span id='Ext-method-setVersion'> /**
  297. </span> * Set version number for the given package name.
  298. *
  299. * @param {String} packageName The package name, for example: 'core', 'touch', 'extjs'
  300. * @param {String/Ext.Version} version The version, for example: '1.2.3alpha', '2.4.0-dev'
  301. * @return {Ext}
  302. */
  303. setVersion: function(packageName, version) {
  304. Ext.versions[packageName] = new Version(version);
  305. Ext.lastRegisteredVersion = Ext.versions[packageName];
  306. return this;
  307. },
  308. <span id='Ext-method-getVersion'> /**
  309. </span> * Get the version number of the supplied package name; will return the last registered version
  310. * (last Ext.setVersion call) if there's no package name given.
  311. *
  312. * @param {String} packageName (Optional) The package name, for example: 'core', 'touch', 'extjs'
  313. * @return {Ext.Version} The version
  314. */
  315. getVersion: function(packageName) {
  316. if (packageName === undefined) {
  317. return Ext.lastRegisteredVersion;
  318. }
  319. return Ext.versions[packageName];
  320. },
  321. <span id='Ext-method-deprecate'> /**
  322. </span> * Create a closure for deprecated code.
  323. *
  324. * // This means Ext.oldMethod is only supported in 4.0.0beta and older.
  325. * // If Ext.getVersion('extjs') returns a version that is later than '4.0.0beta', for example '4.0.0RC',
  326. * // the closure will not be invoked
  327. * Ext.deprecate('extjs', '4.0.0beta', function() {
  328. * Ext.oldMethod = Ext.newMethod;
  329. *
  330. * ...
  331. * });
  332. *
  333. * @param {String} packageName The package name
  334. * @param {String} since The last version before it's deprecated
  335. * @param {Function} closure The callback function to be executed with the specified version is less than the current version
  336. * @param {Object} scope The execution scope (`this`) if the closure
  337. */
  338. deprecate: function(packageName, since, closure, scope) {
  339. if (Version.compare(Ext.getVersion(packageName), since) &lt; 1) {
  340. closure.call(scope);
  341. }
  342. }
  343. }); // End Versioning
  344. Ext.setVersion('core', version);
  345. }());
  346. </pre>
  347. </body>
  348. </html>