helper.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. /**
  19. * Remove spaces at beginning and ending of line.
  20. * @example
  21. * var str = " I'm a string "
  22. * str.trim() // return "I'm a string"
  23. * @method trim
  24. * @return {string}
  25. */
  26. String.prototype.trim = function () {
  27. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  28. };
  29. /**
  30. * Determines whether string end within another string.
  31. *
  32. * @method endsWith
  33. * @param suffix {string} substring for search
  34. * @return {boolean}
  35. */
  36. String.prototype.endsWith = function(suffix) {
  37. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  38. };
  39. /**
  40. * Determines whether string start within another string.
  41. *
  42. * @method startsWith
  43. * @param prefix {string} substring for search
  44. * @return {boolean}
  45. */
  46. String.prototype.startsWith = function (prefix){
  47. return this.indexOf(prefix) == 0;
  48. };
  49. /**
  50. * Determines whether string founded within another string.
  51. *
  52. * @method contains
  53. * @param substring {string} substring for search
  54. * @return {boolean}
  55. */
  56. String.prototype.contains = function(substring) {
  57. return this.indexOf(substring) != -1;
  58. };
  59. /**
  60. * Capitalize the first letter of string.
  61. * @method capitalize
  62. * @return {string}
  63. */
  64. String.prototype.capitalize = function () {
  65. return this.charAt(0).toUpperCase() + this.slice(1);
  66. };
  67. /**
  68. * Capitalize the first letter of string.
  69. * And set to lowercase other part of string
  70. * @method toCapital
  71. * @return {string}
  72. */
  73. String.prototype.toCapital = function () {
  74. return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
  75. };
  76. /**
  77. * Finds the value in an object where this string is a key.
  78. * Optionally, the index of the key can be provided where the
  79. * value of the nth key in the hierarchy is returned.
  80. *
  81. * Example:
  82. * var tofind = 'smart';
  83. * var person = {'name': 'Bob Bob', 'smart': 'no', 'age': '28', 'personality': {'smart': 'yes', 'funny': 'yes', 'emotion': 'happy'} };
  84. * tofind.findIn(person); // 'no'
  85. * tofind.findIn(person, 0); // 'no'
  86. * tofind.findIn(person, 1); // 'yes'
  87. * tofind.findIn(person, 2); // null
  88. *
  89. * @method findIn
  90. * @param multi {object}
  91. * @param index {number} Occurrence count of this key
  92. * @return {*} Value of key at given index
  93. */
  94. String.prototype.findIn = function(multi, index, _foundValues) {
  95. if (!index) {
  96. index = 0;
  97. }
  98. if (!_foundValues) {
  99. _foundValues = [];
  100. }
  101. multi = multi || '';
  102. var value = null;
  103. var str = this.valueOf();
  104. if (typeof multi == 'object') {
  105. for ( var key in multi) {
  106. if (value != null) {
  107. break;
  108. }
  109. if (key == str) {
  110. _foundValues.push(multi[key]);
  111. }
  112. if (_foundValues.length - 1 == index) {
  113. // Found the value
  114. return _foundValues[index];
  115. }
  116. if (typeof multi[key] == 'object') {
  117. value = value || this.findIn(multi[key], index, _foundValues);
  118. }
  119. }
  120. }
  121. return value;
  122. };
  123. /**
  124. * Replace {i} with argument. where i is number of argument to replace with.
  125. * @example
  126. * var str = "{0} world{1}";
  127. * str.format("Hello", "!") // return "Hello world!"
  128. *
  129. * @method format
  130. * @return {string}
  131. */
  132. String.prototype.format = function () {
  133. var args = arguments;
  134. return this.replace(/{(\d+)}/g, function (match, number) {
  135. return typeof args[number] != 'undefined' ? args[number] : match;
  136. });
  137. };
  138. /**
  139. * Wrap words in string within template.
  140. *
  141. * @method highlight
  142. * @param {string[]} words - words to wrap
  143. * @param {string} [highlightTemplate="<b>{0}</b>"] - template for wrapping
  144. * @return {string}
  145. */
  146. String.prototype.highlight = function (words, highlightTemplate) {
  147. var self = this;
  148. highlightTemplate = highlightTemplate ? highlightTemplate : "<b>{0}</b>";
  149. words.forEach(function (word) {
  150. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  151. self = self.replace(searchRegExp, function (found) {
  152. return highlightTemplate.format(found);
  153. });
  154. });
  155. return self;
  156. };
  157. /**
  158. * Convert time in milliseconds to object contained days, hours and minutes.
  159. * @typedef ConvertedTime
  160. * @type {Object}
  161. * @property {number} d - days
  162. * @property {number} h - hours
  163. * @property {string} m - minutes
  164. * @example
  165. * var time = 1000000000;
  166. * time.toDaysHoursMinutes() // {d: 11, h: 13, m: "46.67"}
  167. *
  168. * @method toDaysHoursMinutes
  169. * @return {object}
  170. */
  171. Number.prototype.toDaysHoursMinutes = function () {
  172. var formatted = {},
  173. dateDiff = this,
  174. secK = 1000, //ms
  175. minK = 60 * secK, // sec
  176. hourK = 60 * minK, // sec
  177. dayK = 24 * hourK;
  178. dateDiff = parseInt(dateDiff);
  179. formatted.d = Math.floor(dateDiff / dayK);
  180. dateDiff -= formatted.d * dayK;
  181. formatted.h = Math.floor(dateDiff / hourK);
  182. dateDiff -= formatted.h * hourK;
  183. formatted.m = (dateDiff / minK).toFixed(2);
  184. return formatted;
  185. };
  186. /**
  187. Sort an array by the key specified in the argument.
  188. Handle only native js objects as element of array, not the Ember's object.
  189. Can be used as alternative to sortProperty method of Ember library
  190. in order to speed up executing on large data volumes
  191. @method sortBy
  192. @param {String} path name(s) to sort on
  193. @return {Array} The sorted array.
  194. */
  195. Array.prototype.sortPropertyLight = function (path) {
  196. var realPath = (typeof path === "string") ? path.split('.') : [];
  197. this.sort(function (a, b) {
  198. var aProperty = a;
  199. var bProperty = b;
  200. realPath.forEach(function (key) {
  201. aProperty = aProperty[key];
  202. bProperty = bProperty[key];
  203. });
  204. if (aProperty > bProperty) return 1;
  205. if (aProperty < bProperty) return -1;
  206. return 0;
  207. });
  208. return this;
  209. };
  210. /** @namespace Em **/
  211. Em.CoreObject.reopen({
  212. t:function (key, attrs) {
  213. return Em.I18n.t(key, attrs)
  214. }
  215. });
  216. /** @namespace Em.Handlebars **/
  217. Em.Handlebars.registerHelper('log', function (variable) {
  218. console.log(variable);
  219. });
  220. Em.Handlebars.registerHelper('warn', function (variable) {
  221. console.warn(variable);
  222. });
  223. Em.Handlebars.registerHelper('highlight', function (property, words, fn) {
  224. var context = (fn.contexts && fn.contexts[0]) || this;
  225. property = Em.Handlebars.getPath(context, property, fn);
  226. words = words.split(";");
  227. // if (highlightTemplate == undefined) {
  228. var highlightTemplate = "<b>{0}</b>";
  229. // }
  230. words.forEach(function (word) {
  231. var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
  232. property = property.replace(searchRegExp, function (found) {
  233. return highlightTemplate.format(found);
  234. });
  235. });
  236. return new Em.Handlebars.SafeString(property);
  237. });
  238. /**
  239. * @namespace App
  240. */
  241. App = require('app');
  242. /**
  243. * Certain variables can have JSON in string
  244. * format, or in JSON format itself.
  245. *
  246. * @memberof App
  247. * @function parseJson
  248. * @param {string|object}
  249. * @return {object}
  250. */
  251. App.parseJSON = function (value) {
  252. if (typeof value == "string") {
  253. return jQuery.parseJSON(value);
  254. }
  255. return value;
  256. };
  257. /**
  258. * Check for empty <code>Object</code>, built in Em.isEmpty()
  259. * doesn't support <code>Object</code> type
  260. *
  261. * @memberof App
  262. * @method isEmptyObject
  263. * @param obj {Object}
  264. * @return {Boolean}
  265. */
  266. App.isEmptyObject = function(obj) {
  267. var empty = true;
  268. for (var prop in obj) { if (obj.hasOwnProperty(prop)) {empty = false; break;} }
  269. return empty;
  270. }
  271. /**
  272. * Returns object with defined keys only.
  273. *
  274. * @memberof App
  275. * @method permit
  276. * @param {Object} obj - input object
  277. * @param {String|Array} keys - allowed keys
  278. * @return {Object}
  279. */
  280. App.permit = function(obj, keys) {
  281. var result = {};
  282. if (typeof obj !== 'object' || App.isEmptyObject(obj)) return result;
  283. if (typeof keys == 'string') keys = Array(keys);
  284. keys.forEach(function(key) {
  285. if (obj.hasOwnProperty(key))
  286. result[key] = obj[key];
  287. });
  288. return result;
  289. };
  290. /**
  291. *
  292. * @namespace App
  293. * @namespace App.format
  294. */
  295. App.format = {
  296. /**
  297. * @memberof App.format
  298. * @type {object}
  299. * @property components
  300. */
  301. components: {
  302. 'APP_TIMELINE_SERVER': 'App Timeline Server',
  303. 'DATANODE': 'DataNode',
  304. 'DECOMMISSION_DATANODE': 'Update Exclude File',
  305. 'DRPC_SERVER': 'DRPC Server',
  306. 'FALCON': 'Falcon',
  307. 'FALCON_CLIENT': 'Falcon Client',
  308. 'FALCON_SERVER': 'Falcon Server',
  309. 'FALCON_SERVICE_CHECK': 'Falcon Service Check',
  310. 'FLUME_HANDLER': 'Flume Agent',
  311. 'FLUME_SERVICE_CHECK': 'Flume Service Check',
  312. 'GANGLIA_MONITOR': 'Ganglia Monitor',
  313. 'GANGLIA_SERVER': 'Ganglia Server',
  314. 'GLUSTERFS_CLIENT': 'GLUSTERFS Client',
  315. 'GLUSTERFS_SERVICE_CHECK': 'GLUSTERFS Service Check',
  316. 'GMETAD_SERVICE_CHECK': 'Gmetad Service Check',
  317. 'GMOND_SERVICE_CHECK': 'Gmond Service Check',
  318. 'HADOOP_CLIENT': 'Hadoop Client',
  319. 'HBASE_CLIENT': 'HBase Client',
  320. 'HBASE_MASTER': 'HBase Master',
  321. 'HBASE_REGIONSERVER': 'RegionServer',
  322. 'HBASE_SERVICE_CHECK': 'HBase Service Check',
  323. 'HCAT': 'HCat',
  324. 'HCAT_SERVICE_CHECK': 'HCat Service Check',
  325. 'HDFS_CLIENT': 'HDFS Client',
  326. 'HDFS_SERVICE_CHECK': 'HDFS Service Check',
  327. 'HISTORYSERVER': 'History Server',
  328. 'HIVE_CLIENT': 'Hive Client',
  329. 'HIVE_METASTORE': 'Hive Metastore',
  330. 'HIVE_SERVER': 'HiveServer2',
  331. 'HIVE_SERVICE_CHECK': 'Hive Service Check',
  332. 'HUE_SERVER': 'Hue Server',
  333. 'JAVA_JCE': 'Java JCE',
  334. 'JOBTRACKER': 'JobTracker',
  335. 'JOBTRACKER_SERVICE_CHECK': 'JobTracker Service Check',
  336. 'JOURNALNODE': 'JournalNode',
  337. 'KERBEROS_ADMIN_CLIENT': 'Kerberos Admin Client',
  338. 'KERBEROS_CLIENT': 'Kerberos Client',
  339. 'KERBEROS_SERVER': 'Kerberos Server',
  340. 'MAPREDUCE2_CLIENT': 'MapReduce2 Client',
  341. 'MAPREDUCE2_SERVICE_CHECK': 'MapReduce2 Service Check',
  342. 'MAPREDUCE_CLIENT': 'MapReduce Client',
  343. 'MAPREDUCE_SERVICE_CHECK': 'MapReduce Service Check',
  344. 'MYSQL_SERVER': 'MySQL Server',
  345. 'NAGIOS_SERVER': 'Nagios Server',
  346. 'NAMENODE': 'NameNode',
  347. 'NAMENODE_SERVICE_CHECK': 'NameNode Service Check',
  348. 'NIMBUS': 'Nimbus',
  349. 'NODEMANAGER': 'NodeManager',
  350. 'OOZIE_CLIENT': 'Oozie Client',
  351. 'OOZIE_SERVER': 'Oozie Server',
  352. 'OOZIE_SERVICE_CHECK': 'Oozie Service Check',
  353. 'PIG': 'Pig',
  354. 'PIG_SERVICE_CHECK': 'Pig Service Check',
  355. 'RESOURCEMANAGER': 'ResourceManager',
  356. 'SECONDARY_NAMENODE': 'SNameNode',
  357. 'SQOOP': 'Sqoop',
  358. 'SQOOP_SERVICE_CHECK': 'Sqoop Service Check',
  359. 'STORM_REST_API': 'Storm REST API Server',
  360. 'STORM_SERVICE_CHECK': 'Storm Service Check',
  361. 'STORM_UI_SERVER': 'Storm UI Server',
  362. 'SUPERVISOR': 'Supervisor',
  363. 'TASKTRACKER': 'TaskTracker',
  364. 'TEZ_CLIENT': 'Tez Client',
  365. 'WEBHCAT_SERVER': 'WebHCat Server',
  366. 'WEBHCAT_SERVICE_CHECK': 'WebHCat Service Check',
  367. 'YARN_CLIENT': 'YARN Client',
  368. 'YARN_SERVICE_CHECK': 'YARN Service Check',
  369. 'ZKFC': 'ZKFailoverController',
  370. 'ZOOKEEPER_CLIENT': 'ZooKeeper Client',
  371. 'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
  372. 'ZOOKEEPER_SERVER': 'ZooKeeper Server',
  373. 'ZOOKEEPER_SERVICE_CHECK': 'ZooKeeper Service Check',
  374. 'CLIENT': 'Client'
  375. },
  376. /**
  377. * @memberof App.format
  378. * @property command
  379. * @type {object}
  380. */
  381. command: {
  382. 'INSTALL': 'Install',
  383. 'UNINSTALL': 'Uninstall',
  384. 'START': 'Start',
  385. 'STOP': 'Stop',
  386. 'EXECUTE': 'Execute',
  387. 'ABORT': 'Abort',
  388. 'UPGRADE': 'Upgrade',
  389. 'RESTART': 'Restart',
  390. 'SERVICE_CHECK': 'Check',
  391. 'Excluded:': 'Decommission:',
  392. 'Included:': 'Recommission:'
  393. },
  394. /**
  395. * convert role to readable string
  396. *
  397. * @memberof App.format
  398. * @method role
  399. * @param {string} role
  400. * return {string}
  401. */
  402. role:function (role) {
  403. return this.components[role] ? this.components[role] : '';
  404. },
  405. /**
  406. * convert command_detail to readable string, show the string for all tasks name
  407. *
  408. * @memberof App.format
  409. * @method commandDetail
  410. * @param {string} command_detail
  411. * @return {string}
  412. */
  413. commandDetail: function (command_detail) {
  414. var detailArr = command_detail.split(' ');
  415. var self = this;
  416. var result = '';
  417. detailArr.forEach( function(item) {
  418. // if the item has the pattern SERVICE/COMPONENT, drop the SERVICE part
  419. if (item.contains('/')) {
  420. item = item.split('/')[1];
  421. }
  422. // ignore 'DECOMMISSION', command came from 'excluded/included'
  423. if (item == 'DECOMMISSION,') {
  424. item = '';
  425. }
  426. if (self.components[item]) {
  427. result = result + ' ' + self.components[item];
  428. } else if (self.command[item]) {
  429. result = result + ' ' + self.command[item];
  430. } else {
  431. result = result + ' ' + item;
  432. }
  433. });
  434. if (result === ' nagios_update_ignore ACTIONEXECUTE') {
  435. result = Em.I18n.t('common.maintenance.task');
  436. }
  437. return result;
  438. },
  439. /**
  440. * Convert uppercase status name to downcase.
  441. * <br>
  442. * <br>PENDING - Not queued yet for a host
  443. * <br>QUEUED - Queued for a host
  444. * <br>IN_PROGRESS - Host reported it is working
  445. * <br>COMPLETED - Host reported success
  446. * <br>FAILED - Failed
  447. * <br>TIMEDOUT - Host did not respond in time
  448. * <br>ABORTED - Operation was abandoned
  449. *
  450. * @memberof App.format
  451. * @method taskStatus
  452. * @param {string} _taskStatus
  453. * @return {string}
  454. *
  455. */
  456. taskStatus:function (_taskStatus) {
  457. return _taskStatus.toLowerCase();
  458. }
  459. };
  460. /**
  461. * wrapper to bootstrap popover
  462. * fix issue when popover stuck on view routing
  463. *
  464. * @memberof App
  465. * @method popover
  466. * @param {DOMElement} self
  467. * @param {object} options
  468. */
  469. App.popover = function (self, options) {
  470. self.popover(options);
  471. self.on("remove DOMNodeRemoved", function () {
  472. $(this).trigger('mouseleave');
  473. });
  474. };
  475. /**
  476. * wrapper to bootstrap tooltip
  477. * fix issue when tooltip stuck on view routing
  478. * @memberof App
  479. * @method tooltip
  480. * @param {DOMElement} self
  481. * @param {object} options
  482. */
  483. App.tooltip = function (self, options) {
  484. self.tooltip(options);
  485. /* istanbul ignore next */
  486. self.on("remove DOMNodeRemoved", function () {
  487. $(this).trigger('mouseleave');
  488. });
  489. };
  490. /**
  491. * wrapper to Date().getTime()
  492. * fix issue when client clock and server clock not sync
  493. *
  494. * @memberof App
  495. * @method dateTime
  496. * @return {Number} timeStamp of current server clock
  497. */
  498. App.dateTime = function() {
  499. return new Date().getTime() + App.clockDistance;
  500. };
  501. /**
  502. * Helper function for bound property helper registration
  503. * @memberof App
  504. * @method registerBoundHelper
  505. * @param name {String} name of helper
  506. * @param view {Em.View} view
  507. */
  508. App.registerBoundHelper = function(name, view) {
  509. Em.Handlebars.registerHelper(name, function(property, options) {
  510. options.hash.contentBinding = property;
  511. return Em.Handlebars.helpers.view.call(this, view, options);
  512. });
  513. };
  514. /*
  515. * Return singular or plural word based on Em.I18n property key.
  516. *
  517. * Example: {{pluralize hostsCount singular="t:host" plural="t:hosts"}}
  518. */
  519. App.registerBoundHelper('pluralize', Em.View.extend({
  520. tagName: 'span',
  521. template: Em.Handlebars.compile('{{view.wordOut}}'),
  522. wordOut: function() {
  523. var count, singular, plural;
  524. count = this.get('content');
  525. singular = this.get('singular');
  526. plural = this.get('plural');
  527. return this.getWord(count, singular, plural);
  528. }.property('content'),
  529. getWord: function(count, singular, plural) {
  530. singular = this.tDetect(singular);
  531. plural = this.tDetect(plural);
  532. if (singular && plural) {
  533. if (count > 1) {
  534. return plural;
  535. } else {
  536. return singular;
  537. }
  538. }
  539. return '';
  540. },
  541. /*
  542. * Detect for Em.I18n.t reference call
  543. * @params word {String}
  544. * return {String}
  545. */
  546. tDetect: function(word) {
  547. var splitted = word.split(':');
  548. if (splitted.length > 1 && splitted[0] == 't') {
  549. return Em.I18n.t(splitted[1]);
  550. } else {
  551. return splitted[0];
  552. }
  553. }
  554. })
  555. );
  556. /**
  557. * Return defined string instead of empty if value is null/undefined
  558. * by default is `n/a`.
  559. *
  560. * @param empty {String} - value instead of empty string (not required)
  561. * can be used with Em.I18n pass value started with't:'
  562. *
  563. * Examples:
  564. *
  565. * default value will be returned
  566. * {{formatNull service.someValue}}
  567. *
  568. * <code>empty<code> will be returned
  569. * {{formatNull service.someValue empty="I'm empty"}}
  570. *
  571. * Em.I18n translation will be returned
  572. * {{formatNull service.someValue empty="t:my.key.to.translate"
  573. */
  574. App.registerBoundHelper('formatNull', Em.View.extend({
  575. tagName: 'span',
  576. template: Em.Handlebars.compile('{{view.result}}'),
  577. result: function() {
  578. var emptyValue = this.get('empty') ? this.get('empty') : Em.I18n.t('services.service.summary.notAvailable');
  579. emptyValue = emptyValue.startsWith('t:') ? Em.I18n.t(emptyValue.substr(2, emptyValue.length)) : emptyValue;
  580. return (this.get('content') || this.get('content') == 0) ? this.get('content') : emptyValue;
  581. }.property('content')
  582. }));
  583. /**
  584. * Return formatted string with inserted <code>wbr</code>-tag after each dot
  585. *
  586. * @param {String} content
  587. *
  588. * Examples:
  589. *
  590. * returns 'apple'
  591. * {{formatWordBreak 'apple'}}
  592. *
  593. * returns 'apple.<wbr />banana'
  594. * {{formatWordBreak 'apple.banana'}}
  595. *
  596. * returns 'apple.<wbr />banana.<wbr />uranium'
  597. * {{formatWordBreak 'apple.banana.uranium'}}
  598. */
  599. App.registerBoundHelper('formatWordBreak', Em.View.extend({
  600. tagName: 'span',
  601. template: Em.Handlebars.compile('{{{view.result}}}'),
  602. /**
  603. * @type {string}
  604. */
  605. result: function() {
  606. return this.get('content') && this.get('content').replace(/\./g, '.<wbr />');
  607. }.property('content')
  608. }));
  609. /**
  610. * Ambari overrides the default date transformer.
  611. * This is done because of the non-standard data
  612. * sent. For example Nagios sends date as "12345678".
  613. * The problem is that it is a String and is represented
  614. * only in seconds whereas Javascript's Date needs
  615. * milliseconds representation.
  616. */
  617. DS.attr.transforms.date = {
  618. from: function (serialized) {
  619. var type = typeof serialized;
  620. if (type === Em.I18n.t('common.type.string')) {
  621. serialized = parseInt(serialized);
  622. type = typeof serialized;
  623. }
  624. if (type === Em.I18n.t('common.type.number')) {
  625. if (!serialized ){ //serialized timestamp = 0;
  626. return 0;
  627. }
  628. // The number could be seconds or milliseconds.
  629. // If seconds, then the length is 10
  630. // If milliseconds, the length is 13
  631. if (serialized.toString().length < 13) {
  632. serialized = serialized * 1000;
  633. }
  634. return new Date(serialized);
  635. } else if (serialized === null || serialized === undefined) {
  636. // if the value is not present in the data,
  637. // return undefined, not null.
  638. return serialized;
  639. } else {
  640. return null;
  641. }
  642. },
  643. to: function (deserialized) {
  644. if (deserialized instanceof Date) {
  645. return deserialized.getTime();
  646. } else if (deserialized === undefined) {
  647. return undefined;
  648. } else {
  649. return null;
  650. }
  651. }
  652. };
  653. DS.attr.transforms.object = {
  654. from: function(serialized) {
  655. return Ember.none(serialized) ? null : Object(serialized);
  656. },
  657. to: function(deserialized) {
  658. return Ember.none(deserialized) ? null : Object(deserialized);
  659. }
  660. };
  661. /**
  662. * Allows EmberData models to have array properties.
  663. *
  664. * Declare the property as <code>
  665. * operations: DS.attr('array'),
  666. * </code> and
  667. * during load provide a JSON array for value.
  668. *
  669. * This transform simply assigns the same array in both directions.
  670. */
  671. DS.attr.transforms.array = {
  672. from : function(serialized) {
  673. return serialized;
  674. },
  675. to : function(deserialized) {
  676. return deserialized;
  677. }
  678. };