API Docs for: 3.13.0
Show:

File: yui/js/features.js

  1. var feature_tests = {};
  2.  
  3. /**
  4. Contains the core of YUI's feature test architecture.
  5. @module features
  6. */
  7.  
  8. /**
  9. * Feature detection
  10. * @class Features
  11. * @static
  12. */
  13.  
  14. Y.mix(Y.namespace('Features'), {
  15.  
  16. /**
  17. * Object hash of all registered feature tests
  18. * @property tests
  19. * @type Object
  20. */
  21. tests: feature_tests,
  22.  
  23. /**
  24. * Add a test to the system
  25. *
  26. * ```
  27. * Y.Features.add("load", "1", {});
  28. * ```
  29. *
  30. * @method add
  31. * @param {String} cat The category, right now only 'load' is supported
  32. * @param {String} name The number sequence of the test, how it's reported in the URL or config: 1, 2, 3
  33. * @param {Object} o Object containing test properties
  34. * @param {String} o.name The name of the test
  35. * @param {Function} o.test The test function to execute, the only argument to the function is the `Y` instance
  36. * @param {String} o.trigger The module that triggers this test.
  37. */
  38. add: function(cat, name, o) {
  39. feature_tests[cat] = feature_tests[cat] || {};
  40. feature_tests[cat][name] = o;
  41. },
  42. /**
  43. * Execute all tests of a given category and return the serialized results
  44. *
  45. * ```
  46. * caps=1:1;2:1;3:0
  47. * ```
  48. * @method all
  49. * @param {String} cat The category to execute
  50. * @param {Array} args The arguments to pass to the test function
  51. * @return {String} A semi-colon separated string of tests and their success/failure: 1:1;2:1;3:0
  52. */
  53. all: function(cat, args) {
  54. var cat_o = feature_tests[cat],
  55. // results = {};
  56. result = [];
  57. if (cat_o) {
  58. Y.Object.each(cat_o, function(v, k) {
  59. result.push(k + ':' + (Y.Features.test(cat, k, args) ? 1 : 0));
  60. });
  61. }
  62.  
  63. return (result.length) ? result.join(';') : '';
  64. },
  65. /**
  66. * Run a sepecific test and return a Boolean response.
  67. *
  68. * ```
  69. * Y.Features.test("load", "1");
  70. * ```
  71. *
  72. * @method test
  73. * @param {String} cat The category of the test to run
  74. * @param {String} name The name of the test to run
  75. * @param {Array} args The arguments to pass to the test function
  76. * @return {Boolean} True or false if the test passed/failed.
  77. */
  78. test: function(cat, name, args) {
  79. args = args || [];
  80. var result, ua, test,
  81. cat_o = feature_tests[cat],
  82. feature = cat_o && cat_o[name];
  83.  
  84. if (!feature) {
  85. Y.log('Feature test ' + cat + ', ' + name + ' not found');
  86. } else {
  87.  
  88. result = feature.result;
  89.  
  90. if (Y.Lang.isUndefined(result)) {
  91.  
  92. ua = feature.ua;
  93. if (ua) {
  94. result = (Y.UA[ua]);
  95. }
  96.  
  97. test = feature.test;
  98. if (test && ((!ua) || result)) {
  99. result = test.apply(Y, args);
  100. }
  101.  
  102. feature.result = result;
  103. }
  104. }
  105.  
  106. return result;
  107. }
  108. });
  109.  
  110. // Y.Features.add("load", "1", {});
  111. // Y.Features.test("load", "1");
  112. // caps=1:1;2:0;3:1;
  113.  
  114.