Lungo.Core.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Contains all the common functions used in Lungo.
  3. *
  4. * @namespace LUNGO
  5. * @class Core
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.Core = (function(lng, $$, undefined) {
  11. var ARRAY_PROTO = Array.prototype;
  12. var HASHTAG_CHARACTER = '#';
  13. /**
  14. * Console system to display messages when you are in debug mode.
  15. *
  16. * @method log
  17. *
  18. * @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
  19. * @param {string} Message to show in console
  20. */
  21. var log = function(severity, message) {
  22. if (!lng.Core.isMobile()) {
  23. console[(severity === 1) ? 'log' : (severity === 2) ? 'warn' : 'error'](message);
  24. } else {
  25. // @todo : send to the server
  26. }
  27. };
  28. /**
  29. * Executes callbacks based on the parameters received.
  30. *
  31. * @method execute
  32. *
  33. * @param {Function} callback to execute
  34. */
  35. var execute = function() {
  36. var args = toArray(arguments);
  37. var callback = args.shift();
  38. if (toType(callback) === 'function') {
  39. callback.apply(null, args);
  40. }
  41. };
  42. /**
  43. * Creates a new function that, when called, itself calls this function in
  44. * the context of the provided this value, with a given sequence of arguments
  45. * preceding any provided when the new function was called.
  46. *
  47. * @method bind
  48. *
  49. * @param {object} object to which the 'this' can refer in the new function when the new function is called.
  50. * @param {Function} method A function object.
  51. */
  52. var bind = function(object, method) {
  53. return function() {
  54. return method.apply(object, toArray(arguments));
  55. };
  56. };
  57. /**
  58. * Copy from any number of objects and mix them all into a new object.
  59. * The implementation is simple; just loop through arguments and
  60. * copy every property of every object passed to the function.
  61. *
  62. * @method mix
  63. *
  64. * @param {object} arguments to mix them all into a new object.
  65. * @return {object} child a new object with all the objects from the arguments mixed.
  66. */
  67. var mix = function() {
  68. var child = child || {};
  69. for (var arg = 0, len = arguments.length; arg < len; arg++) {
  70. var argument = arguments[arg];
  71. for (var prop in argument) {
  72. if (isOwnProperty(argument, prop)) {
  73. child[prop] = argument[prop];
  74. }
  75. }
  76. }
  77. return child;
  78. };
  79. /**
  80. * Every object descended from Object inherits the hasOwnProperty method.
  81. * This method can be used to determine whether an object has the specified property
  82. * as a direct property of that object.
  83. *
  84. * @param {object} object to test for a property's existence inside itself.
  85. * @param {string} property the name of the property to test.
  86. * @return {boolean} indicating whether the object has the specified property.
  87. */
  88. var isOwnProperty = function(object, property) {
  89. return $$.isOwnProperty(object, property);
  90. };
  91. /**
  92. * Determine the internal JavaScript [[Class]] of an object.
  93. *
  94. * @param {object} obj to get the real type of itself.
  95. * @return {string} with the internal JavaScript [[Class]] of itself.
  96. */
  97. var toType = function(obj) {
  98. return $$.toType(obj);
  99. };
  100. /**
  101. * Convert an array-like object into a true JavaScript array.
  102. *
  103. * @param {object} obj Any object to turn into a native Array.
  104. * @return {object} The object is now a plain array.
  105. */
  106. var toArray = function(obj) {
  107. return ARRAY_PROTO.slice.call(obj, 0);
  108. };
  109. /**
  110. * Determine if the current environment is a mobile environment
  111. *
  112. * @method isMobile
  113. *
  114. * @return {boolean} true if is mobile environment, false if not.
  115. */
  116. var isMobile = function() {
  117. return $$.isMobile();
  118. };
  119. /**
  120. * Returns information of execute environment
  121. *
  122. * @method environment
  123. *
  124. * @return {object} Environment information
  125. */
  126. var environment = function() {
  127. return $$.environment();
  128. };
  129. /**
  130. * Returns a ordered list of objects by a property
  131. *
  132. * @method orderByProperty
  133. *
  134. * @param {list} List of objects
  135. * @param {string} Name of property
  136. * @param {string} Type of order: asc (ascendent) or desc (descendent)
  137. * @return {list} Ordered list
  138. */
  139. var orderByProperty = function(data, property, order) {
  140. var order_operator = (order === 'desc') ? -1 : 1;
  141. return data.sort(function(a, b) {
  142. return (a[property] < b[property]) ? - order_operator :
  143. (a[property] > b[property]) ? order_operator : 0;
  144. }
  145. );
  146. };
  147. /**
  148. * Returns a correct URL using hashtag character
  149. *
  150. * @method parseUrl
  151. *
  152. * @param {string} Url
  153. * @return {string} Url parsed
  154. */
  155. var parseUrl = function(href) {
  156. var href_hashtag = href.lastIndexOf(HASHTAG_CHARACTER);
  157. if (href_hashtag > 0) {
  158. href = href.substring(href_hashtag);
  159. } else if (href_hashtag === -1) {
  160. href = HASHTAG_CHARACTER + href ;
  161. }
  162. return href;
  163. };
  164. /**
  165. * Returns a Object in a list by a property value
  166. *
  167. * @method objectInListByProperty
  168. *
  169. * @param {list} List of objects
  170. * @param {string} Name of property
  171. * @param {var} Value for comparision
  172. * @return {object} Instance of object founded (if exists)
  173. */
  174. var findByProperty = function(list, property, value) {
  175. var search = null;
  176. for (var i = 0, len = list.length; i < len; i++) {
  177. var element = list[i];
  178. if (element[property] == value) {
  179. search = element;
  180. break;
  181. }
  182. }
  183. return search;
  184. };
  185. return {
  186. log: log,
  187. execute: execute,
  188. bind: bind,
  189. mix: mix,
  190. isOwnProperty: isOwnProperty,
  191. toType: toType,
  192. toArray: toArray,
  193. isMobile: isMobile,
  194. environment: environment,
  195. orderByProperty: orderByProperty,
  196. parseUrl: parseUrl,
  197. findByProperty: findByProperty
  198. };
  199. })(LUNGO, Quo);