Lungo.Core.js 4.4 KB

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