Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aui-data-set-debug.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. AUI.add('aui-data-set', function(A) {
  2. var Lang = A.Lang;
  3. var DataSet = function() {
  4. DataSet.superclass.constructor.apply(this, arguments);
  5. };
  6. DataSet.NAME = 'dataset';
  7. DataSet.ATTRS = {
  8. keys: {
  9. getter: function(value) {
  10. var instance = this;
  11. return instance.keys;
  12. }
  13. },
  14. first: {
  15. getter: function() {
  16. var instance = this;
  17. var values = instance.values;
  18. return values[0];
  19. }
  20. },
  21. includeFunctions: {
  22. value: false
  23. },
  24. items: {
  25. value: null,
  26. getter: function() {
  27. var instance = this;
  28. return instance.collection || {};
  29. }
  30. },
  31. last: {
  32. getter: function() {
  33. var instance = this;
  34. var values = instance.values;
  35. return values[values.length - 1];
  36. }
  37. },
  38. getKey: {
  39. lazyAdd: false,
  40. value: null,
  41. getter: function(value) {
  42. var instance = this;
  43. return value || instance.getKey;
  44. },
  45. setter: function(value) {
  46. var instance = this;
  47. if (Lang.isFunction(value)) {
  48. instance.getKey = value;
  49. }
  50. return instance.getKey;
  51. }
  52. },
  53. values: {
  54. getter: function(value) {
  55. var instance = this;
  56. return instance.values;
  57. },
  58. readOnly: true
  59. }
  60. };
  61. A.extend(
  62. DataSet,
  63. A.Base,
  64. {
  65. initializer: function() {
  66. var instance = this;
  67. instance.collection = {};
  68. instance.keys = [];
  69. instance.values = [];
  70. instance.length = 0;
  71. instance.publish(
  72. 'add',
  73. {
  74. defaultFn: instance._defaultAddFn
  75. }
  76. );
  77. instance.publish(
  78. 'clear',
  79. {
  80. defaultFn: instance._defaultClearFn
  81. }
  82. );
  83. instance.publish(
  84. 'remove',
  85. {
  86. defaultFn: instance._defaultRemoveFn
  87. }
  88. );
  89. instance.publish(
  90. 'replace',
  91. {
  92. defaultFn: instance._defaultReplaceFn
  93. }
  94. );
  95. instance.publish(
  96. 'sort',
  97. {
  98. defaultFn: instance._defaultSortFn
  99. }
  100. );
  101. },
  102. add: function(key, obj) {
  103. var instance = this;
  104. if (arguments.length == 1) {
  105. obj = key;
  106. key = instance.getKey(obj);
  107. }
  108. if (!Lang.isNull(key) && !Lang.isUndefined(key)) {
  109. var prevVal = instance.collection[key];
  110. if (!Lang.isUndefined(prevVal)) {
  111. return instance.replace(key, obj);
  112. }
  113. }
  114. var length = instance.length;
  115. instance.fire(
  116. 'add',
  117. {
  118. index: length,
  119. attrName: key,
  120. item: obj,
  121. newVal: obj
  122. }
  123. );
  124. },
  125. addAll: function(obj) {
  126. var instance = this;
  127. var args = arguments;
  128. var length = args.length;
  129. if (length == 1) {
  130. args = obj;
  131. }
  132. if (length > 1 || Lang.isArray(obj)) {
  133. length = args.length;
  134. for (var i = 0; i < length; i++) {
  135. instance.add(args[i]);
  136. }
  137. }
  138. else {
  139. for (var i in obj) {
  140. var item = obj[i];
  141. instance.add(i, item);
  142. }
  143. }
  144. },
  145. clear: function() {
  146. var instance = this;
  147. instance.fire('clear');
  148. },
  149. clone: function() {
  150. var instance = this;
  151. var clone = new DataSet();
  152. var keys = instance.keys;
  153. var values = instance.values;
  154. var length = values.length;
  155. for (var i = 0; i < length; i++) {
  156. clone.add(keys[i], values[i]);
  157. }
  158. clone.set('getKey', instance.get('getKey'));
  159. return clone;
  160. },
  161. contains: function(obj) {
  162. var instance = this;
  163. return instance.indexOf(obj) > -1;
  164. },
  165. containsKey: function(key) {
  166. var instance = this;
  167. return !(Lang.isUndefined(instance.collection[key]));
  168. },
  169. each: function(fn, context) {
  170. var instance = this;
  171. return instance._each(instance.values, fn, context);
  172. },
  173. eachKey: function(fn, context) {
  174. var instance = this;
  175. var keys = instance.keys;
  176. return instance._each(keys, fn, context);
  177. },
  178. filter: function(fn, context) {
  179. var instance = this;
  180. var filtered = new DataSet();
  181. filtered.set('getKey', instance.get('getKey'));
  182. var collection = instance.collection;
  183. var keys = instance.keys;
  184. var values = instance.values;
  185. context = context || instance;
  186. var filteredDataSet = filtered.collection;
  187. var filteredValues = filtered.values;
  188. var length = values.length;
  189. var item;
  190. for (var i = 0; i < length; i++) {
  191. item = values[i];
  192. if (fn.call(context, item, i, collection)) {
  193. filtered.add(keys[i], item);
  194. }
  195. }
  196. filtered.length = filteredValues.length;
  197. return filtered;
  198. },
  199. filterBy: function(key, value, startsWith, caseSensitive) {
  200. var instance = this;
  201. if (Lang.isUndefined(value) ||
  202. Lang.isNull(value) ||
  203. ((Lang.isArray(value) ||
  204. Lang.isString(value)) && !value.length)) {
  205. return instance.clone();
  206. }
  207. value = instance._generateRegEx(value, startsWith, caseSensitive);
  208. var keyFilter = A.bind(instance._keyFilter, instance, key, value);
  209. return instance.filter(keyFilter);
  210. },
  211. find: function(fn, context) {
  212. var instance = this;
  213. return A.Array.find(instance.values, fn, context);
  214. },
  215. findIndex: function(fn, context, start) {
  216. var instance = this;
  217. var collection = instance.collection;
  218. var values = instance.values;
  219. var length = instance.length;
  220. start = start || 0;
  221. for (var i = start; i < length; i++) {
  222. if (fn.call(context, values[i], i, collection)) {
  223. return i;
  224. }
  225. }
  226. return -1;
  227. },
  228. findIndexBy: function(key, value, start, startsWith, caseSensitive) {
  229. var instance = this;
  230. if (Lang.isUndefined(value) ||
  231. Lang.isNull(value) ||
  232. ((Lang.isArray(value) ||
  233. Lang.isString(value)) && !value.length)) {
  234. return -1;
  235. }
  236. value = instance._generateRegEx(value, startsWith, caseSensitive);
  237. var keyFilter = A.bind(instance._keyFilter, instance, key, value);
  238. return instance.findIndex(keyFilter, null, start);
  239. },
  240. getKey: function(obj) {
  241. var instance = this;
  242. return (obj.get && obj.get('id')) || obj.id;
  243. },
  244. indexOf: function(obj) {
  245. var instance = this;
  246. return A.Array.indexOf(instance.values, obj);
  247. },
  248. indexOfKey: function(key) {
  249. var instance = this;
  250. return A.Array.indexOf(instance.keys, key);
  251. },
  252. insert: function(index, key, obj) {
  253. var instance = this;
  254. if (arguments.length == 2) {
  255. obj = arguments[1];
  256. key = instance.getKey(obj);
  257. }
  258. if (instance.containsKey(key)) {
  259. instance.removeKey(key);
  260. }
  261. instance.fire(
  262. 'add',
  263. {
  264. index: index,
  265. attrName: key,
  266. item: obj,
  267. newVal: obj
  268. }
  269. );
  270. },
  271. invoke: function(method, args) {
  272. var instance = this;
  273. var values = instance.values;
  274. var length = values.length;
  275. if (!args) {
  276. args = [];
  277. }
  278. else {
  279. args = [].concat(args);
  280. }
  281. for (var i = 0; i < length; i++) {
  282. var item = values[i];
  283. var itemMethod = item && item[method];
  284. if (Lang.isFunction(itemMethod)) {
  285. itemMethod.apply(item, args);
  286. }
  287. }
  288. return instance;
  289. },
  290. item: function(key) {
  291. var instance = this;
  292. var item;
  293. if (Lang.isNumber(key)) {
  294. var values = instance.values;
  295. item = values[key];
  296. }
  297. else {
  298. item = instance.collection[key];
  299. }
  300. return item;
  301. },
  302. keySort: function(direction, fn) {
  303. var instance = this;
  304. instance.fire(
  305. 'sort',
  306. {
  307. direction: direction,
  308. fn: fn || instance._keySorter,
  309. type: 'key'
  310. }
  311. );
  312. },
  313. remove: function(obj) {
  314. var instance = this;
  315. var index = instance.indexOf(obj);
  316. return instance.removeAt(index);
  317. },
  318. removeAt: function(index) {
  319. var instance = this;
  320. if (index < instance.length && index >= 0) {
  321. var obj = instance.values[index];
  322. var key = instance.keys[index];
  323. instance.fire(
  324. 'remove',
  325. {
  326. index: index,
  327. attrName: key,
  328. item: obj,
  329. prevVal: obj
  330. }
  331. );
  332. }
  333. },
  334. removeKey: function(key) {
  335. var instance = this;
  336. var index = instance.indexOfKey(key);
  337. return instance.removeAt(index);
  338. },
  339. replace: function(key, obj) {
  340. var instance = this;
  341. if (arguments.length == 1) {
  342. obj = key;
  343. key = instance.getKey(obj);
  344. }
  345. var prevVal = instance.collection[key];
  346. if (Lang.isUndefined(key) || Lang.isNull(key) || Lang.isUndefined(prevVal)) {
  347. return instance.add(key, obj);
  348. }
  349. var index = instance.indexOfKey(key);
  350. instance.fire(
  351. 'replace',
  352. {
  353. attrName: key,
  354. index: index,
  355. item: obj,
  356. prevVal: prevVal,
  357. newVal: obj
  358. }
  359. );
  360. },
  361. size: function() {
  362. var instance = this;
  363. return instance.length;
  364. },
  365. slice: function(start, end) {
  366. var instance = this;
  367. var values = instance.values;
  368. return values.slice.apply(values, arguments);
  369. },
  370. sort: function(direction, fn) {
  371. var instance = this;
  372. instance.fire(
  373. 'sort',
  374. {
  375. direction: direction,
  376. fn: fn,
  377. type: 'value'
  378. }
  379. );
  380. },
  381. _defaultAddFn: function(event) {
  382. var instance = this;
  383. var key = event.attrName;
  384. var obj = event.item;
  385. var index = event.index;
  386. if (!Lang.isNull(key) && !Lang.isUndefined(key)) {
  387. if (instance.get('includeFunctions') || !Lang.isFunction(obj)) {
  388. instance.collection[key] = obj;
  389. }
  390. }
  391. instance.keys.splice(index, 0, key);
  392. instance.values.splice(index, 0, obj);
  393. ++instance.length;
  394. },
  395. _defaultClearFn: function(event) {
  396. var instance = this;
  397. instance.collection = {};
  398. instance.keys = [];
  399. instance.values = [];
  400. instance.length = 0;
  401. },
  402. _defaultRemoveFn: function(event) {
  403. var instance = this;
  404. var index = event.index;
  405. var obj = event.item;
  406. var key = event.attrName;
  407. var collection = instance.collection;
  408. var keys = instance.keys;
  409. instance.values.splice(index, 1);
  410. if (!Lang.isUndefined(key)) {
  411. delete collection[key];
  412. }
  413. keys.splice(index, 1);
  414. instance.length--;
  415. },
  416. _defaultReplaceFn: function(event) {
  417. var instance = this;
  418. var key = event.attrName;
  419. var obj = event.item;
  420. instance.collection[key] = obj;
  421. },
  422. _defaultSortFn: function(event) {
  423. var instance = this;
  424. instance._sortBy(event.type, event.direction, event.fn);
  425. },
  426. _each: function(arr, fn, context) {
  427. var instance = this;
  428. var values = arr.slice(0);
  429. var length = values.length;
  430. context = context || instance;
  431. for (var i = 0; i < length; i++) {
  432. if (fn.call(context, values[i], i, values) === false) {
  433. return false;
  434. }
  435. }
  436. return true;
  437. },
  438. _generateRegEx: function(value, startsWith, caseSensitive) {
  439. var instance = this;
  440. if (!(value instanceof RegExp)) {
  441. value = String(value);
  442. var regExBuffer = [];
  443. if (startsWith !== false) {
  444. regExBuffer.push('^');
  445. }
  446. regExBuffer.push(value);
  447. var options;
  448. if (!caseSensitive) {
  449. options = 'i';
  450. }
  451. value = new RegExp(regExBuffer.join(''), options);
  452. }
  453. return value;
  454. },
  455. _keyFilter: function(key, value, item, index, collection) {
  456. var instance = this;
  457. return item && value.test(item[key]);
  458. },
  459. _keySorter: function(a, b) {
  460. var instance = this;
  461. var keyA = String(a).toLowerCase();
  462. var keyB = String(b).toLowerCase();
  463. var returnValue = 0;
  464. if (keyA > keyB) {
  465. returnValue = 1;
  466. }
  467. else if (keyA < keyB) {
  468. returnValue = -1;
  469. }
  470. return returnValue;
  471. },
  472. _sortBy: function(property, direction, fn) {
  473. var instance = this;
  474. var asc = 1;
  475. var tempValues = [];
  476. var keys = instance.keys;
  477. var values = instance.values;
  478. var length = values.length;
  479. fn = fn || A.Array.numericSort;
  480. if (String(direction).toLowerCase() == 'desc') {
  481. asc = -1;
  482. }
  483. for (var i = 0; i < length; i++) {
  484. tempValues.push(
  485. {
  486. key: keys[i],
  487. value: values[i],
  488. index: i
  489. }
  490. );
  491. }
  492. tempValues.sort(
  493. function(a, b) {
  494. var value = fn(a[property], b[property]) * asc;
  495. if (value === 0) {
  496. value = 1;
  497. if (a.index < b.index) {
  498. value = -1;
  499. }
  500. }
  501. return value;
  502. }
  503. );
  504. length = tempValues.length;
  505. var collection = {};
  506. for (var i = 0; i < length; i++) {
  507. var item = tempValues[i];
  508. var key = item.key;
  509. var value = item.value;
  510. collection[key] = value;
  511. keys[i] = key;
  512. values[i] = value;
  513. }
  514. instance.collection = collection;
  515. }
  516. }
  517. );
  518. A.DataSet = DataSet;
  519. }, '@VERSION@' ,{requires:['oop','collection','base'], skinnable:false});