ember_computed.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var computed = Em.computed;
  19. var get = Em.get;
  20. var makeArray = Em.makeArray;
  21. var slice = [].slice;
  22. var dataUtils = require('utils/data_manipulation');
  23. /**
  24. * Returns hash with values of name properties from the context
  25. * If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
  26. * If some <code>propertyName</code> starts with '!' its value will be inverted
  27. *
  28. * @param {object} self current context
  29. * @param {string[]} propertyNames needed properties
  30. * @returns {object} hash with needed values
  31. */
  32. function getProperties(self, propertyNames) {
  33. var ret = {};
  34. for (var i = 0; i < propertyNames.length; i++) {
  35. var propertyName = propertyNames[i];
  36. var shouldBeInverted = propertyName.startsWith('!');
  37. propertyName = shouldBeInverted ? propertyName.substr(1) : propertyName;
  38. var isApp = propertyName.startsWith('App.');
  39. var name = isApp ? propertyName.replace('App.', '') : propertyName;
  40. var value = isApp ? App.get(name) : self.get(name);
  41. value = shouldBeInverted ? !value : value;
  42. ret[propertyName] = value;
  43. }
  44. return ret;
  45. }
  46. /**
  47. * Returns value of named property from the context
  48. * If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
  49. *
  50. * @param {object} self current context
  51. * @param {string} propertyName needed property
  52. * @returns {*} needed value
  53. */
  54. function smartGet(self, propertyName) {
  55. var isApp = propertyName.startsWith('App.');
  56. var name = isApp ? propertyName.replace('App.', '') : propertyName;
  57. return isApp ? App.get(name) : self.get(name);
  58. }
  59. /**
  60. * Returns list with values of name properties from the context
  61. * If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
  62. *
  63. * @param {object} self current context
  64. * @param {string[]} propertyNames needed properties
  65. * @returns {array} list of needed values
  66. */
  67. function getValues(self, propertyNames) {
  68. return propertyNames.map(function (propertyName) {
  69. return smartGet(self, propertyName);
  70. });
  71. }
  72. function generateComputedWithKey(macro) {
  73. return function () {
  74. var properties = slice.call(arguments, 1);
  75. var key = arguments[0];
  76. var computedFunc = computed(function () {
  77. var values = getValues(this, properties);
  78. return macro.call(this, key, values);
  79. });
  80. return computedFunc.property.apply(computedFunc, properties);
  81. }
  82. }
  83. function generateComputedWithProperties(macro) {
  84. return function () {
  85. var properties = slice.call(arguments);
  86. var computedFunc = computed(function () {
  87. return macro.apply(this, [getProperties(this, properties)]);
  88. });
  89. var realProperties = properties.slice().invoke('replace', '!', '');
  90. return computedFunc.property.apply(computedFunc, realProperties);
  91. };
  92. }
  93. function generateComputedWithValues(macro) {
  94. return function () {
  95. var properties = slice.call(arguments);
  96. var computedFunc = computed(function () {
  97. return macro.apply(this, [getValues(this, properties)]);
  98. });
  99. return computedFunc.property.apply(computedFunc, properties);
  100. };
  101. }
  102. /**
  103. *
  104. * A computed property that returns true if the provided dependent property
  105. * is equal to the given value.
  106. * App.*-keys are supported
  107. * Example*
  108. * ```javascript
  109. * var Hamster = Ember.Object.extend({
  110. * napTime: Ember.computed.equal('state', 'sleepy')
  111. * });
  112. * var hamster = Hamster.create();
  113. * hamster.get('napTime'); // false
  114. * hamster.set('state', 'sleepy');
  115. * hamster.get('napTime'); // true
  116. * hamster.set('state', 'hungry');
  117. * hamster.get('napTime'); // false
  118. * ```
  119. * @method equal
  120. * @param {String} dependentKey
  121. * @param {String|Number|Object} value
  122. * @return {Ember.ComputedProperty} computed property which returns true if
  123. * the original value for property is equal to the given value.
  124. * @public
  125. */
  126. computed.equal = function (dependentKey, value) {
  127. return computed(dependentKey, function () {
  128. return smartGet(this, dependentKey) === value;
  129. }).cacheable();
  130. };
  131. /**
  132. * A computed property that returns true if the provided dependent property is not equal to the given value
  133. * App.*-keys are supported
  134. * <pre>
  135. * var o = Em.Object.create({
  136. * p1: 'a',
  137. * p2: Em.computed.notEqual('p1', 'a')
  138. * });
  139. * console.log(o.get('p2')); // false
  140. * o.set('p1', 'b');
  141. * console.log(o.get('p2')); // true
  142. * </pre>
  143. *
  144. * @method notEqual
  145. * @param {string} dependentKey
  146. * @param {*} value
  147. * @returns {Ember.ComputedProperty}
  148. */
  149. computed.notEqual = function (dependentKey, value) {
  150. return computed(dependentKey, function () {
  151. return smartGet(this, dependentKey) !== value;
  152. });
  153. };
  154. /**
  155. * A computed property that returns true if provided dependent properties are equal to the each other
  156. * App.*-keys are supported
  157. * <pre>
  158. * var o = Em.Object.create({
  159. * p1: 'a',
  160. * p2: 'b',
  161. * p3: Em.computed.equalProperties('p1', 'p2')
  162. * });
  163. * console.log(o.get('p3')); // false
  164. * o.set('p1', 'b');
  165. * console.log(o.get('p3')); // true
  166. * </pre>
  167. *
  168. * @method equalProperties
  169. * @param {string} dependentKey1
  170. * @param {string} dependentKey2
  171. * @returns {Ember.ComputedProperty}
  172. */
  173. computed.equalProperties = function (dependentKey1, dependentKey2) {
  174. return computed(dependentKey1, dependentKey2, function () {
  175. return smartGet(this, dependentKey1) === smartGet(this, dependentKey2);
  176. });
  177. };
  178. /**
  179. * A computed property that returns true if provided dependent properties are not equal to the each other
  180. * App.*-keys are supported
  181. * <pre>
  182. * var o = Em.Object.create({
  183. * p1: 'a',
  184. * p2: 'b',
  185. * p3: Em.computed.notEqualProperties('p1', 'p2')
  186. * });
  187. * console.log(o.get('p3')); // true
  188. * o.set('p1', 'b');
  189. * console.log(o.get('p3')); // false
  190. * </pre>
  191. *
  192. * @method notEqualProperties
  193. * @param {string} dependentKey1
  194. * @param {string} dependentKey2
  195. * @returns {Ember.ComputedProperty}
  196. */
  197. computed.notEqualProperties = function (dependentKey1, dependentKey2) {
  198. return computed(dependentKey1, dependentKey2, function () {
  199. return smartGet(this, dependentKey1) !== smartGet(this, dependentKey2);
  200. });
  201. };
  202. /**
  203. * A computed property that returns grouped collection's items by propertyName-value
  204. *
  205. * @method groupBy
  206. * @param {string} collectionKey
  207. * @param {string} propertyName
  208. * @returns {Ember.ComputedProperty}
  209. */
  210. computed.groupBy = function (collectionKey, propertyName) {
  211. return computed(collectionKey + '.@each.' + propertyName, function () {
  212. var collection = get(this, collectionKey);
  213. return dataUtils.groupPropertyValues(collection, propertyName);
  214. });
  215. };
  216. /**
  217. * A computed property that returns filtered collection by propertyName values-list
  218. * Wrapper to filterProperty-method that allows using list of values to filter
  219. *
  220. * @method filterByMany
  221. * @param {string} collectionKey
  222. * @param {string} propertyName
  223. * @param {array} valuesToFilter
  224. * @returns {Ember.ComputedProperty}
  225. */
  226. computed.filterByMany = function (collectionKey, propertyName, valuesToFilter) {
  227. return computed(collectionKey + '.@each.' + propertyName, function () {
  228. var collection = get(this, collectionKey);
  229. return dataUtils.filterPropertyValues(collection, propertyName, makeArray(valuesToFilter));
  230. });
  231. };
  232. /**
  233. * A computed property that returns collection without elements with value that is in <code>valuesToReject</code>
  234. * Exclude objects from <code>collection</code> if its <code>key</code> exist in <code>valuesToReject</code>
  235. *
  236. * @method rejectMany
  237. * @param {string} collectionKey
  238. * @param {string} propertyName
  239. * @param {array} valuesToReject
  240. * @returns {Ember.ComputedProperty}
  241. */
  242. computed.rejectMany = function (collectionKey, propertyName, valuesToReject) {
  243. return computed(collectionKey + '.@each.' + propertyName, function () {
  244. var collection = get(this, collectionKey);
  245. return dataUtils.rejectPropertyValues(collection, propertyName, makeArray(valuesToReject));
  246. });
  247. };
  248. /**
  249. * A computed property that returns trueValue if dependent value is true and falseValue otherwise
  250. * App.*-keys are supported
  251. * <pre>
  252. * var o = Em.Object.create({
  253. * p1: true,
  254. * p2: Em.computed.ifThenElse('p1', 'abc', 'cba')
  255. * });
  256. * console.log(o.get('p2')); // 'abc'
  257. * o.set('p1', false);
  258. * console.log(o.get('p2')); // 'cba'
  259. * </pre>
  260. *
  261. * @method ifThenElse
  262. * @param {string} dependentKey
  263. * @param {*} trueValue
  264. * @param {*} falseValue
  265. * @returns {Ember.ComputedProperty}
  266. */
  267. computed.ifThenElse = function (dependentKey, trueValue, falseValue) {
  268. return computed(dependentKey, function () {
  269. return smartGet(this, dependentKey) ? trueValue : falseValue;
  270. });
  271. };
  272. /**
  273. * A computed property that is equal to the logical 'and'
  274. * Takes any number of arguments
  275. * Returns true if all of them are truly, false - otherwise
  276. * App.*-keys are supported
  277. * <pre>
  278. * var o = Em.Object.create({
  279. * p1: true,
  280. * p2: true,
  281. * p3: true,
  282. * p4: Em.computed.and('p1', 'p2', 'p3')
  283. * });
  284. * console.log(o.get('p4')); // true
  285. * o.set('p1', false);
  286. * console.log(o.get('p4')); // false
  287. * </pre>
  288. *
  289. * @method and
  290. * @param {...string} dependentKeys
  291. * @returns {Ember.ComputedProperty}
  292. */
  293. computed.and = generateComputedWithProperties(function (properties) {
  294. var value;
  295. for (var key in properties) {
  296. value = !!properties[key];
  297. if (properties.hasOwnProperty(key) && !value) {
  298. return false;
  299. }
  300. }
  301. return value;
  302. });
  303. /**
  304. * A computed property that is equal to the logical 'or'
  305. * Takes any number of arguments
  306. * Returns true if at least one of them is truly, false - otherwise
  307. * App.*-keys are supported
  308. * <pre>
  309. * var o = Em.Object.create({
  310. * p1: false,
  311. * p2: false,
  312. * p3: false,
  313. * p4: Em.computed.or('p1', 'p2', 'p3')
  314. * });
  315. * console.log(o.get('p4')); // false
  316. * o.set('p1', true);
  317. * console.log(o.get('p4')); // true
  318. * </pre>
  319. *
  320. * @method or
  321. * @param {...string} dependentKeys
  322. * @returns {Ember.ComputedProperty}
  323. */
  324. computed.or = generateComputedWithProperties(function (properties) {
  325. var value;
  326. for (var key in properties) {
  327. value = !!properties[key];
  328. if (properties.hasOwnProperty(key) && value) {
  329. return value;
  330. }
  331. }
  332. return value;
  333. });
  334. /**
  335. * A computed property that returns sum on the dependent properties values
  336. * Takes any number of arguments
  337. * App.*-keys are supported
  338. * <pre>
  339. * var o = Em.Object.create({
  340. * p1: 1,
  341. * p2: 2,
  342. * p3: 3,
  343. * p4: Em.computed.sumProperties('p1', 'p2', 'p3')
  344. * });
  345. * console.log(o.get('p4')); // 6
  346. * o.set('p1', 2);
  347. * console.log(o.get('p4')); // 7
  348. * </pre>
  349. *
  350. * @method sumProperties
  351. * @param {...string} dependentKeys
  352. * @returns {Ember.ComputedProperty}
  353. */
  354. computed.sumProperties = generateComputedWithProperties(function (properties) {
  355. var sum = 0;
  356. for (var key in properties) {
  357. if (properties.hasOwnProperty(key)) {
  358. sum += Number(properties[key]);
  359. }
  360. }
  361. return sum;
  362. });
  363. /**
  364. * A computed property that returns true if dependent value is greater or equal to the needed value
  365. * App.*-keys are supported
  366. * <pre>
  367. * var o = Em.Object.create({
  368. * p1: 4,
  369. * p2: Em.computed.gte('p1', 1)
  370. * });
  371. * console.log(o.get('p2')); // true
  372. * o.set('p1', 4);
  373. * console.log(o.get('p2')); // true
  374. * o.set('p1', 5);
  375. * console.log(o.get('p2')); // false
  376. * </pre>
  377. *
  378. * @method gte
  379. * @param {string} dependentKey
  380. * @param {*} value
  381. * @returns {Ember.ComputedProperty}
  382. */
  383. computed.gte = function (dependentKey, value) {
  384. return computed(dependentKey, function () {
  385. return smartGet(this, dependentKey) >= value;
  386. });
  387. };
  388. /**
  389. * A computed property that returns true if first dependent property is greater or equal to the second dependent property
  390. * App.*-keys are supported
  391. * <pre>
  392. * var o = Em.Object.create({
  393. * p1: 4,
  394. * p2: 1,
  395. * p3: Em.computed.gteProperties('p1', 'p2')
  396. * });
  397. * console.log(o.get('p3')); // true
  398. * o.set('p2', 4);
  399. * console.log(o.get('p3')); // true
  400. * o.set('p2', 5);
  401. * console.log(o.get('p3')); // false
  402. * </pre>
  403. *
  404. * @method gteProperties
  405. * @param {string} dependentKey1
  406. * @param {string} dependentKey2
  407. * @returns {Ember.ComputedProperty}
  408. */
  409. computed.gteProperties = function (dependentKey1, dependentKey2) {
  410. return computed(dependentKey1, dependentKey2, function () {
  411. return smartGet(this, dependentKey1) >= smartGet(this, dependentKey2);
  412. });
  413. };
  414. /**
  415. * A computed property that returns true if dependent property is less or equal to the needed value
  416. * App.*-keys are supported
  417. * <pre>
  418. * var o = Em.Object.create({
  419. * p1: 4,
  420. * p2: Em.computed.lte('p1', 1)
  421. * });
  422. * console.log(o.get('p2')); // false
  423. * o.set('p1', 4);
  424. * console.log(o.get('p2')); // true
  425. * o.set('p1', 5);
  426. * console.log(o.get('p2')); // true
  427. * </pre>
  428. *
  429. * @method lte
  430. * @param {string} dependentKey
  431. * @param {*} value
  432. * @returns {Ember.ComputedProperty}
  433. */
  434. computed.lte = function (dependentKey, value) {
  435. return computed(dependentKey, function () {
  436. return smartGet(this, dependentKey) <= value;
  437. });
  438. };
  439. /**
  440. * A computed property that returns true if first dependent property is less or equal to the second dependent property
  441. * App.*-keys are supported
  442. * <pre>
  443. * var o = Em.Object.create({
  444. * p1: 4,
  445. * p2: 1,
  446. * p3: Em.computed.lteProperties('p1', 'p2')
  447. * });
  448. * console.log(o.get('p3')); // false
  449. * o.set('p2', 4);
  450. * console.log(o.get('p3')); // true
  451. * o.set('p2', 5);
  452. * console.log(o.get('p3')); // true
  453. * </pre>
  454. *
  455. * @method lteProperties
  456. * @param {string} dependentKey1
  457. * @param {string} dependentKey2
  458. * @returns {Ember.ComputedProperty}
  459. */
  460. computed.lteProperties = function (dependentKey1, dependentKey2) {
  461. return computed(dependentKey1, dependentKey2, function () {
  462. return smartGet(this, dependentKey1) <= smartGet(this, dependentKey2);
  463. });
  464. };
  465. /**
  466. * A computed property that returns true if dependent value is greater than the needed value
  467. * App.*-keys are supported
  468. * <pre>
  469. * var o = Em.Object.create({
  470. * p1: 4,
  471. * p2: Em.computed.gt('p1', 1)
  472. * });
  473. * console.log(o.get('p2')); // true
  474. * o.set('p1', 4);
  475. * console.log(o.get('p2')); // false
  476. * o.set('p1', 5);
  477. * console.log(o.get('p2')); // false
  478. * </pre>
  479. *
  480. * @method gt
  481. * @param {string} dependentKey
  482. * @param {*} value
  483. * @returns {Ember.ComputedProperty}
  484. */
  485. computed.gt = function (dependentKey, value) {
  486. return computed(dependentKey, function () {
  487. return smartGet(this, dependentKey) > value;
  488. });
  489. };
  490. /**
  491. * A computed property that returns true if first dependent property is greater than the second dependent property
  492. * App.*-keys are supported
  493. * <pre>
  494. * var o = Em.Object.create({
  495. * p1: 4,
  496. * p2: 1,
  497. * p3: Em.computed.gteProperties('p1', 'p2')
  498. * });
  499. * console.log(o.get('p3')); // true
  500. * o.set('p2', 4);
  501. * console.log(o.get('p3')); // false
  502. * o.set('p2', 5);
  503. * console.log(o.get('p3')); // false
  504. * </pre>
  505. *
  506. * @method gtProperties
  507. * @param {string} dependentKey1
  508. * @param {string} dependentKey2
  509. * @returns {Ember.ComputedProperty}
  510. */
  511. computed.gtProperties = function (dependentKey1, dependentKey2) {
  512. return computed(dependentKey1, dependentKey2, function () {
  513. return smartGet(this, dependentKey1) > smartGet(this, dependentKey2);
  514. });
  515. };
  516. /**
  517. * A computed property that returns true if dependent value is less than the needed value
  518. * App.*-keys are supported
  519. * <pre>
  520. * var o = Em.Object.create({
  521. * p1: 4,
  522. * p2: Em.computed.lt('p1', 1)
  523. * });
  524. * console.log(o.get('p2')); // false
  525. * o.set('p1', 4);
  526. * console.log(o.get('p2')); // false
  527. * o.set('p1', 5);
  528. * console.log(o.get('p2')); // true
  529. * </pre>
  530. *
  531. * @method lt
  532. * @param {string} dependentKey
  533. * @param {*} value
  534. * @returns {Ember.ComputedProperty}
  535. */
  536. computed.lt = function (dependentKey, value) {
  537. return computed(dependentKey, function () {
  538. return smartGet(this, dependentKey) < value;
  539. });
  540. };
  541. /**
  542. * A computed property that returns true if first dependent property is less than the second dependent property
  543. * App.*-keys are supported
  544. * <pre>
  545. * var o = Em.Object.create({
  546. * p1: 4,
  547. * p2: 1,
  548. * p3: Em.computed.ltProperties('p1', 'p2')
  549. * });
  550. * console.log(o.get('p3')); // false
  551. * o.set('p2', 4);
  552. * console.log(o.get('p3')); // false
  553. * o.set('p2', 5);
  554. * console.log(o.get('p3')); // true
  555. * </pre>
  556. *
  557. * @method gtProperties
  558. * @param {string} dependentKey1
  559. * @param {string} dependentKey2
  560. * @returns {Ember.ComputedProperty}
  561. */
  562. computed.ltProperties = function (dependentKey1, dependentKey2) {
  563. return computed(dependentKey1, dependentKey2, function () {
  564. return smartGet(this, dependentKey1) < smartGet(this, dependentKey2);
  565. });
  566. };
  567. /**
  568. * A computed property that returns true if dependent property is match to the needed regular expression
  569. * <pre>
  570. * var o = Em.Object.create({
  571. * p1: 'abc',
  572. * p2: Em.computed.match('p1', /^a/)
  573. * });
  574. * console.log(o.get('p2')); // true
  575. * o.set('p1', 'bc');
  576. * console.log(o.get('p2')); // false
  577. * </pre>
  578. *
  579. * @method match
  580. * @param {string} dependentKey
  581. * @param {RegExp} regexp
  582. * @returns {Ember.ComputedProperty}
  583. */
  584. computed.match = function (dependentKey, regexp) {
  585. return computed(dependentKey, function () {
  586. var value = get(this, dependentKey);
  587. if (!regexp) {
  588. return false;
  589. }
  590. return regexp.test(value);
  591. });
  592. };
  593. /**
  594. * A computed property that returns true of some collection's item has property with needed value
  595. * <pre>
  596. * var o = Em.Object.create({
  597. * p1: [{a: 1}, {a: 2}, {a: 3}],
  598. * p2: Em.computed.someBy('p1', 'a', 1)
  599. * });
  600. * console.log(o.get('p2')); // true
  601. * o.set('p1.0.a', 2);
  602. * console.log(o.get('p2')); // false
  603. * </pre>
  604. *
  605. * @method someBy
  606. * @param {string} collectionKey
  607. * @param {string} propertyName
  608. * @param {*} neededValue
  609. * @returns {Ember.ComputedProperty}
  610. */
  611. computed.someBy = function (collectionKey, propertyName, neededValue) {
  612. return computed(collectionKey + '.@each.' + propertyName, function () {
  613. var collection = smartGet(this, collectionKey);
  614. if (!collection) {
  615. return false;
  616. }
  617. return collection.someProperty(propertyName, neededValue);
  618. });
  619. };
  620. /**
  621. * A computed property that returns true of all collection's items have property with needed value
  622. * <pre>
  623. * var o = Em.Object.create({
  624. * p1: [{a: 1}, {a: 1}, {a: 1}],
  625. * p2: Em.computed.everyBy('p1', 'a', 1)
  626. * });
  627. * console.log(o.get('p2')); // true
  628. * o.set('p1.0.a', 2);
  629. * console.log(o.get('p2')); // false
  630. * </pre>
  631. *
  632. * @method everyBy
  633. * @param {string} collectionKey
  634. * @param {string} propertyName
  635. * @param {*} neededValue
  636. * @returns {Ember.ComputedProperty}
  637. */
  638. computed.everyBy = function (collectionKey, propertyName, neededValue) {
  639. return computed(collectionKey + '.@each.' + propertyName, function () {
  640. var collection = smartGet(this, collectionKey);
  641. if (!collection) {
  642. return false;
  643. }
  644. return collection.everyProperty(propertyName, neededValue);
  645. });
  646. };
  647. /**
  648. * A computed property that returns array with values of named property on all items in the collection
  649. * <pre>
  650. * var o = Em.Object.create({
  651. * p1: [{a: 1}, {a: 2}, {a: 3}],
  652. * p2: Em.computed.everyBy('p1', 'a')
  653. * });
  654. * console.log(o.get('p2')); // [1, 2, 3]
  655. * o.set('p1.0.a', 2);
  656. * console.log(o.get('p2')); // [2, 2, 3]
  657. * </pre>
  658. *
  659. * @method mapBy
  660. * @param {string} collectionKey
  661. * @param {string} propertyName
  662. * @returns {Ember.ComputedProperty}
  663. */
  664. computed.mapBy = function (collectionKey, propertyName) {
  665. return computed(collectionKey + '.@each.' + propertyName, function () {
  666. var collection = smartGet(this, collectionKey);
  667. if (!collection) {
  668. return [];
  669. }
  670. return collection.mapProperty(propertyName);
  671. });
  672. };
  673. /**
  674. * A computed property that returns array with collection's items that have needed property value
  675. * <pre>
  676. * var o = Em.Object.create({
  677. * p1: [{a: 1}, {a: 2}, {a: 3}],
  678. * p2: Em.computed.filterBy('p1', 'a', 2)
  679. * });
  680. * console.log(o.get('p2')); // [{a: 2}]
  681. * o.set('p1.0.a', 2);
  682. * console.log(o.get('p2')); // [{a: 2}, {a: 2}]
  683. * </pre>
  684. *
  685. * @method filterBy
  686. * @param {string} collectionKey
  687. * @param {string} propertyName
  688. * @param {*} neededValue
  689. * @returns {Ember.ComputedProperty}
  690. */
  691. computed.filterBy = function (collectionKey, propertyName, neededValue) {
  692. return computed(collectionKey + '.@each.' + propertyName, function () {
  693. var collection = smartGet(this, collectionKey);
  694. if (!collection) {
  695. return [];
  696. }
  697. return collection.filterProperty(propertyName, neededValue);
  698. });
  699. };
  700. /**
  701. * A computed property that returns first collection's item that has needed property value
  702. * <pre>
  703. * var o = Em.Object.create({
  704. * p1: [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}],
  705. * p2: Em.computed.findBy('p1', 'a', 2)
  706. * });
  707. * console.log(o.get('p2')); // [{a: 2, b: 2}]
  708. * o.set('p1.0.a', 2);
  709. * console.log(o.get('p2')); // [{a: 2, b: 1}]
  710. * </pre>
  711. *
  712. * @method findBy
  713. * @param {string} collectionKey
  714. * @param {string} propertyName
  715. * @param {*} neededValue
  716. * @returns {Ember.ComputedProperty}
  717. */
  718. computed.findBy = function (collectionKey, propertyName, neededValue) {
  719. return computed(collectionKey + '.@each.' + propertyName, function () {
  720. var collection = smartGet(this, collectionKey);
  721. if (!collection) {
  722. return null;
  723. }
  724. return collection.findProperty(propertyName, neededValue);
  725. });
  726. };
  727. /**
  728. * A computed property that returns value equal to the dependent
  729. * Should be used as 'short-name' for deeply-nested values
  730. * App.*-keys are supported
  731. * <pre>
  732. * var o = Em.Object.create({
  733. * p1: {a: {b: {c: 2}}},
  734. * p2: Em.computed.alias('p1.a.b.c')
  735. * });
  736. * console.log(o.get('p2')); // 2
  737. * o.set('p1.a.b.c', 4);
  738. * console.log(o.get('p2')); // 4
  739. * </pre>
  740. *
  741. * @method alias
  742. * @param {string} dependentKey
  743. * @returns {Ember.ComputedProperty}
  744. */
  745. computed.alias = function (dependentKey) {
  746. return computed(dependentKey, function () {
  747. return smartGet(this, dependentKey);
  748. });
  749. };
  750. /**
  751. * A computed property that returns true if dependent property exists in the needed values
  752. * <pre>
  753. * var o = Em.Object.create({
  754. * p1: 2,
  755. * p2: Em.computed.existsIn('p1', [1, 2, 3])
  756. * });
  757. * console.log(o.get('p2')); // true
  758. * o.set('p1', 4);
  759. * console.log(o.get('p2')); // false
  760. * </pre>
  761. *
  762. * @method existsIn
  763. * @param {string} dependentKey
  764. * @param {array} neededValues
  765. * @returns {Ember.ComputedProperty}
  766. */
  767. computed.existsIn = function (dependentKey, neededValues) {
  768. return computed(dependentKey, function () {
  769. var value = smartGet(this, dependentKey);
  770. return makeArray(neededValues).contains(value);
  771. });
  772. };
  773. /**
  774. * A computed property that returns true if dependent property doesn't exist in the needed values
  775. * <pre>
  776. * var o = Em.Object.create({
  777. * p1: 2,
  778. * p2: Em.computed.notExistsIn('p1', [1, 2, 3])
  779. * });
  780. * console.log(o.get('p2')); // false
  781. * o.set('p1', 4);
  782. * console.log(o.get('p2')); // true
  783. * </pre>
  784. *
  785. * @method notExistsIn
  786. * @param {string} dependentKey
  787. * @param {array} neededValues
  788. * @returns {Ember.ComputedProperty}
  789. */
  790. computed.notExistsIn = function (dependentKey, neededValues) {
  791. return computed(dependentKey, function () {
  792. var value = smartGet(this, dependentKey);
  793. return !makeArray(neededValues).contains(value);
  794. });
  795. };
  796. /**
  797. * A computed property that returns result of calculation <code>(dependentProperty1/dependentProperty2 * 100)</code>
  798. * If accuracy is 0 (by default), result is rounded to integer
  799. * Otherwise - result is float with provided accuracy
  800. * App.*-keys are supported
  801. * <pre>
  802. * var o = Em.Object.create({
  803. * p1: 2,
  804. * p2: 4,
  805. * p3: Em.computed.percents('p1', 'p2')
  806. * });
  807. * console.log(o.get('p3')); // 50
  808. * o.set('p2', 5);
  809. * console.log(o.get('p3')); // 40
  810. * </pre>
  811. *
  812. * @method percents
  813. * @param {string} dependentKey1
  814. * @param {string} dependentKey2
  815. * @param {number} [accuracy=0]
  816. * @returns {Ember.ComputedProperty}
  817. */
  818. computed.percents = function (dependentKey1, dependentKey2, accuracy) {
  819. if (arguments.length < 3) {
  820. accuracy = 0;
  821. }
  822. return computed(dependentKey1, dependentKey2, function () {
  823. var v1 = Number(smartGet(this, dependentKey1));
  824. var v2 = Number(smartGet(this, dependentKey2));
  825. var result = v1 / v2 * 100;
  826. if (0 === accuracy) {
  827. return Math.round(result);
  828. }
  829. return parseFloat(result.toFixed(accuracy));
  830. });
  831. };
  832. /**
  833. * A computed property that returns result of <code>App.format.role</code> for dependent value
  834. * <pre>
  835. * var o = Em.Object.create({
  836. * p1: 'SECONDARY_NAMENODE',
  837. * p3: Em.computed.formatRole('p1', false)
  838. * });
  839. * console.log(o.get('p2')); // 'SNameNode'
  840. * o.set('p1', 'FLUME_HANDLER);
  841. * console.log(o.get('p2')); // 'Flume'
  842. * </pre>
  843. *
  844. * @method formatRole
  845. * @param {string} dependentKey
  846. * @param {boolean} isServiceRole
  847. * @returns {Ember.ComputedProperty}
  848. */
  849. computed.formatRole = function (dependentKey, isServiceRole) {
  850. return computed(dependentKey, function () {
  851. var value = get(this, dependentKey);
  852. return App.format.role(value, isServiceRole);
  853. });
  854. };
  855. /**
  856. * A computed property that returns sum of the named property in the each collection's item
  857. * <pre>
  858. * var o = Em.Object.create({
  859. * p1: [{a: 1}, {a: 2}, {a: 3}],
  860. * p2: Em.computed.sumBy('p1', 'a')
  861. * });
  862. * console.log(o.get('p2')); // 6
  863. * o.set('p1.0.a', 2);
  864. * console.log(o.get('p2')); // 7
  865. * </pre>
  866. *
  867. * @method sumBy
  868. * @param {string} collectionKey
  869. * @param {string} propertyName
  870. * @returns {Ember.ComputedProperty}
  871. */
  872. computed.sumBy = function (collectionKey, propertyName) {
  873. return computed(collectionKey + '.@each.' + propertyName, function () {
  874. var collection = smartGet(this, collectionKey);
  875. if (Em.isEmpty(collection)) {
  876. return 0;
  877. }
  878. var sum = 0;
  879. collection.forEach(function (item) {
  880. sum += Number(get(item, propertyName));
  881. });
  882. return sum;
  883. });
  884. };
  885. /**
  886. * A computed property that returns I18n-string formatted with dependent properties
  887. * Takes at least one argument
  888. * App.*-keys are supported
  889. *
  890. * @param {string} key key in the I18n-messages
  891. * @param {...string} dependentKeys
  892. * @method i18nFormat
  893. * @returns {Ember.ComputedProperty}
  894. */
  895. computed.i18nFormat = generateComputedWithKey(function (key, dependentValues) {
  896. var str = Em.I18n.t(key);
  897. if (!str) {
  898. return '';
  899. }
  900. return str.format.apply(str, dependentValues);
  901. });
  902. /**
  903. * A computed property that returns string formatted with dependent properties
  904. * Takes at least one argument
  905. * App.*-keys are supported
  906. * <pre>
  907. * var o = Em.Object.create({
  908. * p1: 'abc',
  909. * p2: 'cba',
  910. * p3: Em.computed.format('{0} => {1}', 'p1', 'p2')
  911. * });
  912. * console.log(o.get('p3')); // 'abc => cba'
  913. * o.set('p1', 'aaa');
  914. * console.log(o.get('p3')); // 'aaa => cba'
  915. * </pre>
  916. *
  917. * @param {string} str string to format
  918. * @param {...string} dependentKeys
  919. * @method format
  920. * @returns {Ember.ComputedProperty}
  921. */
  922. computed.format = generateComputedWithKey(function (str, dependentValues) {
  923. if (!str) {
  924. return '';
  925. }
  926. return str.format.apply(str, dependentValues);
  927. });
  928. /**
  929. * A computed property that returns dependent values joined with separator
  930. * Takes at least one argument
  931. * App.*-keys are supported
  932. * <pre>
  933. * var o = Em.Object.create({
  934. * p1: 'abc',
  935. * p2: 'cba',
  936. * p3: Em.computed.concat('|', 'p1', 'p2')
  937. * });
  938. * console.log(o.get('p3')); // 'abc|cba'
  939. * o.set('p1', 'aaa');
  940. * console.log(o.get('p3')); // 'aaa|cba'
  941. * </pre>
  942. *
  943. * @param {string} separator
  944. * @param {...string} dependentKeys
  945. * @method concat
  946. * @return {Ember.ComputedProperty}
  947. */
  948. computed.concat = generateComputedWithKey(function (separator, dependentValues) {
  949. return dependentValues.join(separator);
  950. });
  951. /**
  952. * A computed property that returns first not blank value from dependent values
  953. * Based on <code>Ember.isBlank</code>
  954. * Takes at least 1 argument
  955. * Dependent values order affects the result
  956. * App.*-keys are supported
  957. * <pre>
  958. * var o = Em.Object.create({
  959. * p1: null,
  960. * p2: '',
  961. * p3: 'abc'
  962. * p4: Em.computed.firstNotBlank('p1', 'p2', 'p3')
  963. * });
  964. * console.log(o.get('p4')); // 'abc'
  965. * o.set('p1', 'aaa');
  966. * console.log(o.get('p4')); // 'aaa'
  967. * </pre>
  968. *
  969. * @param {...string} dependentKeys
  970. * @method firstNotBlank
  971. * @return {Ember.ComputedProperty}
  972. */
  973. computed.firstNotBlank = generateComputedWithValues(function (values) {
  974. for (var i = 0; i < values.length; i++) {
  975. if (!Em.isBlank(values[i])) {
  976. return values[i];
  977. }
  978. }
  979. return null;
  980. });
  981. /**
  982. * A computed property that returns dependent value if it is truly or ('0'|0)
  983. * Returns <code>'n/a'</code> otherwise
  984. * App.*-keys are supported
  985. * <pre>
  986. * var o = Em.Object.create({
  987. * p1: 0,
  988. * p2: Em.computed.formatUnavailable('p1')
  989. * });
  990. * console.log(o.get('p2')); // 0
  991. * o.set('p1', 12);
  992. * console.log(o.get('p2')); // 12
  993. * o.set('p1', 'some string');
  994. * console.log(o.get('p2')); // 'n/a'
  995. * </pre>
  996. *
  997. * @param {string} dependentKey
  998. * @method formatUnavailable
  999. * @returns {Ember.ComputedProperty}
  1000. */
  1001. computed.formatUnavailable = function(dependentKey) {
  1002. return computed(dependentKey, function () {
  1003. var value = smartGet(this, dependentKey);
  1004. return (value || value == 0) ? value : Em.I18n.t('services.service.summary.notAvailable');
  1005. });
  1006. };
  1007. /**
  1008. * A computed property that returns one of provided values basing on dependent value
  1009. * If dependent value is 0, <code>zeroMsg</code> is returned
  1010. * If dependent value is 1, <code>oneMsg</code> is returned
  1011. * If dependent value is greater than 1, <code>manyMsg</code> is returned
  1012. * App.*-keys are supported
  1013. * <pre>
  1014. * var o = Em.Object.create({
  1015. * p1: 0,
  1016. * p2: Em.computed.formatUnavailable('p1', '0msg', '1msg', '2+msg')
  1017. * });
  1018. * console.log(o.get('p2')); // '0msg'
  1019. * o.set('p1', 1);
  1020. * console.log(o.get('p2')); // '1msg'
  1021. * o.set('p1', 100500);
  1022. * console.log(o.get('p2')); // '2+msg'
  1023. * </pre>
  1024. *
  1025. * @param {string} dependentKey
  1026. * @param {string} zeroMsg
  1027. * @param {string} oneMsg
  1028. * @param {string} manyMsg
  1029. * @returns {Ember.ComputedProperty}
  1030. * @method countBasedMessage
  1031. */
  1032. computed.countBasedMessage = function (dependentKey, zeroMsg, oneMsg, manyMsg) {
  1033. return computed(dependentKey, function () {
  1034. var value = Number(smartGet(this, dependentKey));
  1035. if (value === 0) {
  1036. return zeroMsg;
  1037. }
  1038. if (value > 1) {
  1039. return manyMsg;
  1040. }
  1041. return oneMsg;
  1042. });
  1043. };