Lungo.Core.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  13. * Console system to display messages when you are in debug mode.
  14. *
  15. * @method log
  16. *
  17. * @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
  18. * @param {string} Message to show in console
  19. */
  20. var log = function(severity, message) {
  21. if (!lng.Core.isMobile()) {
  22. console[(severity === 1) ? 'log' : (severity === 2) ? 'warn' : 'error'](message);
  23. } else {
  24. // @todo : send to the server
  25. }
  26. };
  27. /**
  28. * Executes callbacks based on the parameters received.
  29. *
  30. * @method execute
  31. *
  32. * @param {Function} callback to execute
  33. */
  34. var execute = function() {
  35. var args = toArray(arguments);
  36. var callback = args.shift();
  37. if (toType(callback) === 'function') {
  38. callback.apply(null, args);
  39. }
  40. };
  41. /**
  42. * Creates a new function that, when called, itself calls this function in
  43. * the context of the provided this value, with a given sequence of arguments
  44. * preceding any provided when the new function was called.
  45. *
  46. * @method bind
  47. *
  48. * @param {object} object to which the 'this' can refer in the new function when the new function is called.
  49. * @param {Function} method A function object.
  50. */
  51. var bind = function(object, method) {
  52. return function() {
  53. return method.apply(object, toArray(arguments));
  54. };
  55. };
  56. /**
  57. * Copy from any number of objects and mix them all into a new object.
  58. * The implementation is simple; just loop through arguments and
  59. * copy every property of every object passed to the function.
  60. *
  61. * @method mix
  62. *
  63. * @param {object} arguments to mix them all into a new object.
  64. * @return {object} child a new object with all the objects from the arguments mixed.
  65. */
  66. var mix = function() {
  67. var child = child || {};
  68. for (var arg = 0, len = arguments.length; arg < len; arg++) {
  69. var argument = arguments[arg];
  70. for (var prop in argument) {
  71. if (isOwnProperty(argument, prop)) {
  72. child[prop] = argument[prop];
  73. }
  74. }
  75. }
  76. return child;
  77. };
  78. /**
  79. * Every object descended from Object inherits the hasOwnProperty method.
  80. * This method can be used to determine whether an object has the specified property
  81. * as a direct property of that object.
  82. *
  83. * @param {object} object to test for a property's existence inside itself.
  84. * @param {string} property the name of the property to test.
  85. * @return {boolean} indicating whether the object has the specified property.
  86. */
  87. var isOwnProperty = function(object, property) {
  88. return $$.isOwnProperty(object, property);
  89. };
  90. /**
  91. * Determine the internal JavaScript [[Class]] of an object.
  92. *
  93. * @param {object} obj to get the real type of itself.
  94. * @return {string} with the internal JavaScript [[Class]] of itself.
  95. */
  96. var toType = function(obj) {
  97. return $$.toType(obj);
  98. };
  99. /**
  100. * Convert an array-like object into a true JavaScript array.
  101. *
  102. * @param {object} obj Any object to turn into a native Array.
  103. * @return {object} The object is now a plain array.
  104. */
  105. var toArray = function(obj) {
  106. return ARRAY_PROTO.slice.call(obj, 0);
  107. };
  108. /**
  109. * Determine if the current environment is a mobile environment
  110. *
  111. * @method isMobile
  112. *
  113. * @return {boolean} true if is mobile environment, false if not.
  114. */
  115. var isMobile = function() {
  116. return $$.isMobile();
  117. };
  118. /**
  119. * Returns information of execute environment
  120. *
  121. * @method environment
  122. *
  123. * @return {object} Environment information
  124. */
  125. var environment = function() {
  126. return $$.environment();
  127. };
  128. /**
  129. * Returns information of execute environment
  130. *
  131. * @method environment
  132. *
  133. * @return {object} Environment information
  134. */
  135. var orderByProperty = function(data, property, order) {
  136. var order_operator = (order === 'desc') ? -1 : 1;
  137. return data.sort(function(a, b) {
  138. return (a[property] < b[property]) ? - order_operator :
  139. (a[property] > b[property])
  140. ?
  141. order_operator : 0;
  142. }
  143. );
  144. };
  145. return {
  146. log: log,
  147. execute: execute,
  148. bind: bind,
  149. mix: mix,
  150. isOwnProperty: isOwnProperty,
  151. toType: toType,
  152. toArray: toArray,
  153. isMobile: isMobile,
  154. environment: environment,
  155. orderByProperty: orderByProperty
  156. };
  157. })(LUNGO, Quo);