mobiscroll.core.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*!
  2. * Mobiscroll v2.15.1
  3. * http://mobiscroll.com
  4. *
  5. * Copyright 2010-2015, Acid Media
  6. * Licensed under the MIT license.
  7. *
  8. */
  9. (function ($, undefined) {
  10. function testProps(props) {
  11. var i;
  12. for (i in props) {
  13. if (mod[props[i]] !== undefined) {
  14. return true;
  15. }
  16. }
  17. return false;
  18. }
  19. function testPrefix() {
  20. var prefixes = ['Webkit', 'Moz', 'O', 'ms'],
  21. p;
  22. for (p in prefixes) {
  23. if (testProps([prefixes[p] + 'Transform'])) {
  24. return '-' + prefixes[p].toLowerCase() + '-';
  25. }
  26. }
  27. return '';
  28. }
  29. function init(that, options, args) {
  30. var ret = that;
  31. // Init
  32. if (typeof options === 'object') {
  33. return that.each(function () {
  34. if (instances[this.id]) {
  35. instances[this.id].destroy();
  36. }
  37. new $.mobiscroll.classes[options.component || 'Scroller'](this, options);
  38. });
  39. }
  40. // Method call
  41. if (typeof options === 'string') {
  42. that.each(function () {
  43. var r,
  44. inst = instances[this.id];
  45. if (inst && inst[options]) {
  46. r = inst[options].apply(this, Array.prototype.slice.call(args, 1));
  47. if (r !== undefined) {
  48. ret = r;
  49. return false;
  50. }
  51. }
  52. });
  53. }
  54. return ret;
  55. }
  56. var id = +new Date(),
  57. instances = {},
  58. extend = $.extend,
  59. mod = document.createElement('modernizr').style,
  60. has3d = testProps(['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective']),
  61. hasFlex = testProps(['flex', 'msFlex', 'WebkitBoxDirection']),
  62. prefix = testPrefix(),
  63. pr = prefix.replace(/^\-/, '').replace(/\-$/, '').replace('moz', 'Moz');
  64. $.fn.mobiscroll = function (method) {
  65. extend(this, $.mobiscroll.components);
  66. return init(this, method, arguments);
  67. };
  68. $.mobiscroll = $.mobiscroll || {
  69. version: '2.15.1',
  70. util: {
  71. prefix: prefix,
  72. jsPrefix: pr,
  73. has3d: has3d,
  74. hasFlex: hasFlex,
  75. testTouch: function (e, elm) {
  76. if (e.type == 'touchstart') {
  77. $(elm).attr('data-touch', '1');
  78. } else if ($(elm).attr('data-touch')) {
  79. $(elm).removeAttr('data-touch');
  80. return false;
  81. }
  82. return true;
  83. },
  84. objectToArray: function (obj) {
  85. var arr = [],
  86. i;
  87. for (i in obj) {
  88. arr.push(obj[i]);
  89. }
  90. return arr;
  91. },
  92. arrayToObject: function (arr) {
  93. var obj = {},
  94. i;
  95. if (arr) {
  96. for (i = 0; i < arr.length; i++) {
  97. obj[arr[i]] = arr[i];
  98. }
  99. }
  100. return obj;
  101. },
  102. isNumeric: function (a) {
  103. return a - parseFloat(a) >= 0;
  104. },
  105. isString: function (s) {
  106. return typeof s === 'string';
  107. },
  108. getCoord: function (e, c) {
  109. var ev = e.originalEvent || e;
  110. return ev.changedTouches ? ev.changedTouches[0]['page' + c] : e['page' + c];
  111. },
  112. getPosition: function (t, vertical) {
  113. var style = window.getComputedStyle ? getComputedStyle(t[0]) : t[0].style,
  114. matrix,
  115. px;
  116. if (has3d) {
  117. $.each(['t', 'webkitT', 'MozT', 'OT', 'msT'], function (i, v) {
  118. if (style[v + 'ransform'] !== undefined) {
  119. matrix = style[v + 'ransform'];
  120. return false;
  121. }
  122. });
  123. matrix = matrix.split(')')[0].split(', ');
  124. px = vertical ? (matrix[13] || matrix[5]) : (matrix[12] || matrix[4]);
  125. } else {
  126. px = vertical ? style.top.replace('px', '') : style.left.replace('px', '');
  127. }
  128. return px;
  129. },
  130. constrain: function (val, min, max) {
  131. return Math.max(min, Math.min(val, max));
  132. },
  133. vibrate: function (time) {
  134. if ('vibrate' in navigator) {
  135. navigator.vibrate(time || 50);
  136. }
  137. }
  138. },
  139. tapped: false,
  140. autoTheme: 'mobiscroll',
  141. presets: {
  142. scroller: {},
  143. numpad: {},
  144. listview: {},
  145. menustrip: {}
  146. },
  147. themes: {
  148. frame: {},
  149. listview: {},
  150. menustrip: {}
  151. },
  152. i18n: {},
  153. instances: instances,
  154. classes: {},
  155. components: {},
  156. defaults: {
  157. context: 'body',
  158. mousewheel: true,
  159. vibrate: true
  160. },
  161. setDefaults: function (o) {
  162. extend(this.defaults, o);
  163. },
  164. presetShort: function (name, c, p) {
  165. this.components[name] = function (s) {
  166. return init(this, extend(s, { component: c, preset: p === false ? undefined : name }), arguments);
  167. };
  168. }
  169. };
  170. $.mobiscroll.classes.Base = function (el, settings) {
  171. var lang,
  172. preset,
  173. s,
  174. theme,
  175. themeName,
  176. defaults,
  177. ms = $.mobiscroll,
  178. that = this;
  179. that.settings = {};
  180. that._presetLoad = function () {};
  181. that._init = function (ss) {
  182. s = that.settings;
  183. // Update original user settings
  184. extend(settings, ss);
  185. // Load user defaults
  186. if (that._hasDef) {
  187. defaults = ms.defaults;
  188. }
  189. // Create settings object
  190. extend(s, that._defaults, defaults, settings);
  191. // Get theme defaults
  192. if (that._hasTheme) {
  193. themeName = s.theme;
  194. if (themeName == 'auto' || !themeName) {
  195. themeName = ms.autoTheme;
  196. }
  197. if (themeName == 'default') {
  198. themeName = 'mobiscroll';
  199. }
  200. settings.theme = themeName;
  201. theme = ms.themes[that._class][themeName];
  202. }
  203. // Get language defaults
  204. if (that._hasLang) {
  205. lang = ms.i18n[s.lang];
  206. }
  207. if (that._hasTheme) {
  208. that.trigger('onThemeLoad', [lang, settings]);
  209. }
  210. // Update settings object
  211. extend(s, theme, lang, defaults, settings);
  212. // Load preset settings
  213. if (that._hasPreset) {
  214. that._presetLoad(s);
  215. preset = ms.presets[that._class][s.preset];
  216. if (preset) {
  217. preset = preset.call(el, that);
  218. extend(s, preset, settings);
  219. }
  220. }
  221. };
  222. that._destroy = function () {
  223. that.trigger('onDestroy', []);
  224. // Delete scroller instance
  225. delete instances[el.id];
  226. that = null;
  227. };
  228. /**
  229. * Triggers an event
  230. */
  231. that.trigger = function (name, args) {
  232. var ret;
  233. args.push(that);
  234. $.each([defaults, theme, preset, settings], function (i, v) {
  235. if (v && v[name]) { // Call preset event
  236. ret = v[name].apply(el, args);
  237. }
  238. });
  239. return ret;
  240. };
  241. /**
  242. * Sets one ore more options.
  243. */
  244. that.option = function (opt, value) {
  245. var obj = {};
  246. if (typeof opt === 'object') {
  247. obj = opt;
  248. } else {
  249. obj[opt] = value;
  250. }
  251. that.init(obj);
  252. };
  253. /**
  254. * Returns the mobiscroll instance.
  255. */
  256. that.getInst = function () {
  257. return that;
  258. };
  259. settings = settings || {};
  260. // Autogenerate id
  261. if (!el.id) {
  262. el.id = 'mobiscroll' + (++id);
  263. }
  264. // Save instance
  265. instances[el.id] = that;
  266. };
  267. })(jQuery);