PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bower_components/CanJS/can.map.sort.js

https://gitlab.com/alekhya484/example-project
JavaScript | 312 lines | 293 code | 4 blank | 15 comment | 69 complexity | 834d5daf7da91106585ca76aba30f1e4 MD5 | raw file
  1. /*!
  2. * CanJS - 2.3.23
  3. * http://canjs.com/
  4. * Copyright (c) 2016 Bitovi
  5. * Fri, 08 Apr 2016 17:58:15 GMT
  6. * Licensed MIT
  7. */
  8. /*[global-shim-start]*/
  9. (function (exports, global){
  10. var origDefine = global.define;
  11. var get = function(name){
  12. var parts = name.split("."),
  13. cur = global,
  14. i;
  15. for(i = 0 ; i < parts.length; i++){
  16. if(!cur) {
  17. break;
  18. }
  19. cur = cur[parts[i]];
  20. }
  21. return cur;
  22. };
  23. var modules = (global.define && global.define.modules) ||
  24. (global._define && global._define.modules) || {};
  25. var ourDefine = global.define = function(moduleName, deps, callback){
  26. var module;
  27. if(typeof deps === "function") {
  28. callback = deps;
  29. deps = [];
  30. }
  31. var args = [],
  32. i;
  33. for(i =0; i < deps.length; i++) {
  34. args.push( exports[deps[i]] ? get(exports[deps[i]]) : ( modules[deps[i]] || get(deps[i]) ) );
  35. }
  36. // CJS has no dependencies but 3 callback arguments
  37. if(!deps.length && callback.length) {
  38. module = { exports: {} };
  39. var require = function(name) {
  40. return exports[name] ? get(exports[name]) : modules[name];
  41. };
  42. args.push(require, module.exports, module);
  43. }
  44. // Babel uses the exports and module object.
  45. else if(!args[0] && deps[0] === "exports") {
  46. module = { exports: {} };
  47. args[0] = module.exports;
  48. if(deps[1] === "module") {
  49. args[1] = module;
  50. }
  51. } else if(!args[0] && deps[0] === "module") {
  52. args[0] = { id: moduleName };
  53. }
  54. global.define = origDefine;
  55. var result = callback ? callback.apply(null, args) : undefined;
  56. global.define = ourDefine;
  57. // Favor CJS module.exports over the return value
  58. modules[moduleName] = module && module.exports ? module.exports : result;
  59. };
  60. global.define.orig = origDefine;
  61. global.define.modules = modules;
  62. global.define.amd = true;
  63. ourDefine("@loader", [], function(){
  64. // shim for @@global-helpers
  65. var noop = function(){};
  66. return {
  67. get: function(){
  68. return { prepareGlobal: noop, retrieveGlobal: noop };
  69. },
  70. global: global,
  71. __exec: function(__load){
  72. eval("(function() { " + __load.source + " \n }).call(global);");
  73. }
  74. };
  75. });
  76. })({},window)
  77. /*can@2.3.23#list/sort/sort*/
  78. define('can/list/sort/sort', [
  79. 'can/util/util',
  80. 'can/list/list'
  81. ], function (can) {
  82. var oldBubbleRule = can.List._bubbleRule;
  83. can.List._bubbleRule = function (eventName, list) {
  84. var oldBubble = oldBubbleRule.apply(this, arguments);
  85. if (list.comparator && can.inArray('change', oldBubble) === -1) {
  86. oldBubble.push('change');
  87. }
  88. return oldBubble;
  89. };
  90. var proto = can.List.prototype, _changes = proto._changes || function () {
  91. }, setup = proto.setup, unbind = proto.unbind;
  92. can.extend(proto, {
  93. setup: function (instances, options) {
  94. setup.apply(this, arguments);
  95. this.bind('change', can.proxy(this._changes, this));
  96. this._comparatorBound = false;
  97. this.bind('comparator', can.proxy(this._comparatorUpdated, this));
  98. delete this._init;
  99. if (this.comparator) {
  100. this.sort();
  101. }
  102. },
  103. _comparatorUpdated: function (ev, newValue) {
  104. if (newValue || newValue === 0) {
  105. this.sort();
  106. if (this._bindings > 0 && !this._comparatorBound) {
  107. this.bind('change', this._comparatorBound = function () {
  108. });
  109. }
  110. } else if (this._comparatorBound) {
  111. unbind.call(this, 'change', this._comparatorBound);
  112. this._comparatorBound = false;
  113. }
  114. },
  115. unbind: function (ev, handler) {
  116. var res = unbind.apply(this, arguments);
  117. if (this._comparatorBound && this._bindings === 1) {
  118. unbind.call(this, 'change', this._comparatorBound);
  119. this._comparatorBound = false;
  120. }
  121. return res;
  122. },
  123. _comparator: function (a, b) {
  124. var comparator = this.comparator;
  125. if (comparator && typeof comparator === 'function') {
  126. return comparator(a, b);
  127. }
  128. if (typeof a === 'string' && typeof b === 'string' && ''.localeCompare) {
  129. return a.localeCompare(b);
  130. }
  131. return a === b ? 0 : a < b ? -1 : 1;
  132. },
  133. _changes: function (ev, attr, how, newVal, oldVal) {
  134. var dotIndex = ('' + attr).indexOf('.');
  135. if (this.comparator && dotIndex !== -1) {
  136. if (ev.batchNum) {
  137. if (ev.batchNum === this._lastProcessedBatchNum) {
  138. return;
  139. } else {
  140. this.sort();
  141. this._lastProcessedBatchNum = ev.batchNum;
  142. return;
  143. }
  144. }
  145. var currentIndex = +attr.substr(0, dotIndex);
  146. var item = this[currentIndex];
  147. var changedAttr = attr.substr(dotIndex + 1);
  148. if (typeof item !== 'undefined' && (typeof this.comparator !== 'string' || this.comparator.indexOf(changedAttr) === 0)) {
  149. var newIndex = this._getRelativeInsertIndex(item, currentIndex);
  150. if (newIndex !== currentIndex) {
  151. this._swapItems(currentIndex, newIndex);
  152. can.batch.trigger(this, 'length', [this.length]);
  153. }
  154. }
  155. }
  156. _changes.apply(this, arguments);
  157. },
  158. _getInsertIndex: function (item, lowerBound, upperBound) {
  159. var insertIndex = 0;
  160. var a = this._getComparatorValue(item);
  161. var b, dir, comparedItem, testIndex;
  162. lowerBound = typeof lowerBound === 'number' ? lowerBound : 0;
  163. upperBound = typeof upperBound === 'number' ? upperBound : this.length - 1;
  164. while (lowerBound <= upperBound) {
  165. testIndex = (lowerBound + upperBound) / 2 | 0;
  166. comparedItem = this[testIndex];
  167. b = this._getComparatorValue(comparedItem);
  168. dir = this._comparator(a, b);
  169. if (dir < 0) {
  170. upperBound = testIndex - 1;
  171. } else if (dir >= 0) {
  172. lowerBound = testIndex + 1;
  173. insertIndex = lowerBound;
  174. }
  175. }
  176. return insertIndex;
  177. },
  178. _getRelativeInsertIndex: function (item, currentIndex) {
  179. var naiveInsertIndex = this._getInsertIndex(item);
  180. var nextItemIndex = currentIndex + 1;
  181. var a = this._getComparatorValue(item);
  182. var b;
  183. if (naiveInsertIndex >= currentIndex) {
  184. naiveInsertIndex -= 1;
  185. }
  186. if (currentIndex < naiveInsertIndex && nextItemIndex < this.length) {
  187. b = this._getComparatorValue(this[nextItemIndex]);
  188. if (this._comparator(a, b) === 0) {
  189. return currentIndex;
  190. }
  191. }
  192. return naiveInsertIndex;
  193. },
  194. _getComparatorValue: function (item) {
  195. var comparator = this.comparator;
  196. if (item && comparator && typeof comparator === 'string') {
  197. item = typeof item[comparator] === 'function' ? item[comparator]() : item.attr(comparator);
  198. }
  199. return item;
  200. },
  201. _getComparatorValues: function () {
  202. var self = this;
  203. var a = [];
  204. this.each(function (item, index) {
  205. a.push(self._getComparatorValue(item));
  206. });
  207. return a;
  208. },
  209. sort: function (comparator) {
  210. if (arguments.length) {
  211. this.attr('comparator', comparator);
  212. } else {
  213. this._sort();
  214. }
  215. return this;
  216. },
  217. _sort: function () {
  218. var a, b, c, isSorted;
  219. for (var i, iMin, j = 0, n = this.length; j < n - 1; j++) {
  220. iMin = j;
  221. isSorted = true;
  222. c = undefined;
  223. for (i = j + 1; i < n; i++) {
  224. a = this._getComparatorValue(this.attr(i));
  225. b = this._getComparatorValue(this.attr(iMin));
  226. if (this._comparator.call(this, a, b) < 0) {
  227. isSorted = false;
  228. iMin = i;
  229. }
  230. if (c && this._comparator.call(this, a, c) < 0) {
  231. isSorted = false;
  232. }
  233. c = a;
  234. }
  235. if (isSorted) {
  236. break;
  237. }
  238. if (iMin !== j) {
  239. this._swapItems(iMin, j);
  240. }
  241. }
  242. can.batch.trigger(this, 'length', [this.length]);
  243. return this;
  244. },
  245. _swapItems: function (oldIndex, newIndex) {
  246. var temporaryItemReference = this[oldIndex];
  247. [].splice.call(this, oldIndex, 1);
  248. [].splice.call(this, newIndex, 0, temporaryItemReference);
  249. can.batch.trigger(this, 'move', [
  250. temporaryItemReference,
  251. newIndex,
  252. oldIndex
  253. ]);
  254. }
  255. });
  256. can.each({
  257. push: 'length',
  258. unshift: 0
  259. }, function (where, name) {
  260. var proto = can.List.prototype, old = proto[name];
  261. proto[name] = function () {
  262. if (this.comparator && arguments.length) {
  263. var args = can.makeArray(arguments);
  264. var length = args.length;
  265. var i = 0;
  266. var newIndex, val;
  267. while (i < length) {
  268. val = can.bubble.set(this, i, this.__type(args[i], i));
  269. newIndex = this._getInsertIndex(val);
  270. Array.prototype.splice.apply(this, [
  271. newIndex,
  272. 0,
  273. val
  274. ]);
  275. this._triggerChange('' + newIndex, 'add', [val], undefined);
  276. i++;
  277. }
  278. can.batch.trigger(this, 'reset', [args]);
  279. return this;
  280. } else {
  281. return old.apply(this, arguments);
  282. }
  283. };
  284. });
  285. (function () {
  286. var proto = can.List.prototype;
  287. var oldSplice = proto.splice;
  288. proto.splice = function (index, howMany) {
  289. var args = can.makeArray(arguments);
  290. if (!this.comparator) {
  291. return oldSplice.apply(this, args);
  292. }
  293. oldSplice.call(this, index, howMany);
  294. args.splice(0, 2);
  295. proto.push.apply(this, args);
  296. };
  297. }());
  298. return can.Map;
  299. });
  300. /*can@2.3.23#map/sort/sort*/
  301. define('can/map/sort/sort', ['can/list/sort/sort'], function (sortPlugin) {
  302. return sortPlugin;
  303. });
  304. /*[global-shim-end]*/
  305. (function (){
  306. window._define = window.define;
  307. window.define = window.define.orig;
  308. })();