flex-flow.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function() {
  2. var Declaration, FlexFlow, flexSpec,
  3. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  4. hasProp = {}.hasOwnProperty;
  5. flexSpec = require('./flex-spec');
  6. Declaration = require('../declaration');
  7. FlexFlow = (function(superClass) {
  8. extend(FlexFlow, superClass);
  9. function FlexFlow() {
  10. return FlexFlow.__super__.constructor.apply(this, arguments);
  11. }
  12. FlexFlow.names = ['flex-flow', 'box-direction', 'box-orient'];
  13. FlexFlow.prototype.insert = function(decl, prefix, prefixes) {
  14. var already, cloned, dir, orient, ref, spec, value, values;
  15. ref = flexSpec(prefix), spec = ref[0], prefix = ref[1];
  16. if (spec !== 2009) {
  17. return FlexFlow.__super__.insert.apply(this, arguments);
  18. } else {
  19. values = decl.value.split(/\s+/).filter(function(i) {
  20. return i !== 'wrap' && i !== 'nowrap' && 'wrap-reverse';
  21. });
  22. if (values.length === 0) {
  23. return;
  24. }
  25. already = decl.parent.some(function(i) {
  26. return i.prop === prefix + 'box-orient' || i.prop === prefix + 'box-direction';
  27. });
  28. if (already) {
  29. return;
  30. }
  31. value = values[0];
  32. orient = value.indexOf('row') !== -1 ? 'horizontal' : 'vertical';
  33. dir = value.indexOf('reverse') !== -1 ? 'reverse' : 'normal';
  34. cloned = this.clone(decl);
  35. cloned.prop = prefix + 'box-orient';
  36. cloned.value = orient;
  37. if (this.needCascade(decl)) {
  38. cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
  39. }
  40. decl.parent.insertBefore(decl, cloned);
  41. cloned = this.clone(decl);
  42. cloned.prop = prefix + 'box-direction';
  43. cloned.value = dir;
  44. if (this.needCascade(decl)) {
  45. cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
  46. }
  47. return decl.parent.insertBefore(decl, cloned);
  48. }
  49. };
  50. return FlexFlow;
  51. })(Declaration);
  52. module.exports = FlexFlow;
  53. }).call(this);