029.jquery.agent.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc User Agent jQuery Plugin
  4. * @package KCFinder
  5. * @version 3.12
  6. * @author Pavel Tzonkov <sunhater@sunhater.com>
  7. * @copyright 2010-2014 KCFinder Project
  8. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  9. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  10. * @link http://kcfinder.sunhater.com
  11. */
  12. (function($) {
  13. $.agent = {};
  14. var agent = " " + navigator.userAgent,
  15. patterns = [
  16. {
  17. expr: / [a-z]+\/[0-9a-z\.]+/ig,
  18. delim: "/"
  19. }, {
  20. expr: / [a-z]+:[0-9a-z\.]+/ig,
  21. delim: ":",
  22. keys: ["rv", "version"]
  23. }, {
  24. expr: / [a-z]+\s+[0-9a-z\.]+/ig,
  25. delim: /\s+/,
  26. keys: ["opera", "msie", "firefox", "android"]
  27. }, {
  28. expr: /[ \/\(]([a-z0-9_]+)[ ;\)\/]/ig,
  29. keys: "i386|i486|i586|i686|x86|x64|x86_64|intel|ppc|powerpc|windows|macintosh|darwin|unix|linux|sunos|android|iphone|ipad|ipod|amiga|amigaos|beos|wii|playstation|gentoo|fedora|slackware|ubuntu|archlinux|debian|mint|mageia|mandriva|freebsd|openbsd|netbsd|solaris|opensolaris|x11|mobile|phone".split('|'),
  30. sub: "platform"
  31. }
  32. ];
  33. $.each(patterns, function(i, pattern) {
  34. var elements = agent.match(pattern.expr);
  35. if (elements === null)
  36. return;
  37. $.each(elements, function(j, ag) {
  38. ag = ag.replace(/^\s+/, "").toLowerCase();
  39. var key = ag.replace(pattern.expr, "$1"),
  40. val = true;
  41. if (typeof pattern.delim != "undefined") {
  42. ag = ag.split(pattern.delim);
  43. key = ag[0];
  44. val = ag[1];
  45. }
  46. if (typeof pattern.keys != "undefined") {
  47. var exists = false, k = 0;
  48. for (; k < pattern.keys.length; k++)
  49. if (pattern.keys[k] == key) {
  50. exists = true;
  51. break;
  52. }
  53. if (!exists)
  54. return;
  55. }
  56. if (typeof pattern.sub != "undefined") {
  57. if (typeof $.agent[pattern.sub] != "object")
  58. $.agent[pattern.sub] = {};
  59. if (typeof $.agent[pattern.sub][key] == "undefined")
  60. $.agent[pattern.sub][key] = val;
  61. } else if (typeof $.agent[key] == "undefined")
  62. $.agent[key] = val;
  63. });
  64. });
  65. if (!$.agent.platform)
  66. $.agent.platform = {};
  67. // Check for mobile device
  68. $.mobile = false;
  69. var keys = "mobile|android|iphone|ipad|ipod|iemobile|phone".split('|');
  70. a = $.agent;
  71. $.each([a, a.platform], function(i, p) {
  72. for (var j = 0; j < keys.length; j++) {
  73. if (p[keys[j]]) {
  74. $.mobile = true;
  75. return false;
  76. }
  77. }
  78. });
  79. })(jQuery);