MediaList.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //.CommonJS
  2. var CSSOM = {};
  3. ///CommonJS
  4. /**
  5. * @constructor
  6. * @see http://dev.w3.org/csswg/cssom/#the-medialist-interface
  7. */
  8. CSSOM.MediaList = function MediaList(){
  9. this.length = 0;
  10. };
  11. CSSOM.MediaList.prototype = {
  12. constructor: CSSOM.MediaList,
  13. /**
  14. * @return {string}
  15. */
  16. get mediaText() {
  17. return Array.prototype.join.call(this, ", ");
  18. },
  19. /**
  20. * @param {string} value
  21. */
  22. set mediaText(value) {
  23. if (typeof value === "string") {
  24. var values = value.split(",").filter(function(text){
  25. return !!text;
  26. });
  27. var length = this.length = values.length;
  28. for (var i=0; i<length; i++) {
  29. this[i] = values[i].trim();
  30. }
  31. } else if (value === null) {
  32. var length = this.length;
  33. for (var i = 0; i < length; i++) {
  34. delete this[i];
  35. }
  36. this.length = 0;
  37. }
  38. },
  39. /**
  40. * @param {string} medium
  41. */
  42. appendMedium: function(medium) {
  43. if (Array.prototype.indexOf.call(this, medium) === -1) {
  44. this[this.length] = medium;
  45. this.length++;
  46. }
  47. },
  48. /**
  49. * @param {string} medium
  50. */
  51. deleteMedium: function(medium) {
  52. var index = Array.prototype.indexOf.call(this, medium);
  53. if (index !== -1) {
  54. Array.prototype.splice.call(this, index, 1);
  55. }
  56. },
  57. item: function(index) {
  58. return this[index] || null;
  59. },
  60. toString: function() {
  61. return this.mediaText;
  62. }
  63. };
  64. //.CommonJS
  65. exports.MediaList = CSSOM.MediaList;
  66. ///CommonJS