Margin.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Generated by CoffeeScript 1.9.3
  2. var Margin, MarginBottom, MarginLeft, MarginRight, MarginTop, _Declaration,
  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. _Declaration = require('./_Declaration');
  6. MarginTop = require('./MarginTop');
  7. MarginLeft = require('./MarginLeft');
  8. MarginRight = require('./MarginRight');
  9. MarginBottom = require('./MarginBottom');
  10. module.exports = Margin = (function(superClass) {
  11. var self;
  12. extend(Margin, superClass);
  13. function Margin() {
  14. return Margin.__super__.constructor.apply(this, arguments);
  15. }
  16. self = Margin;
  17. Margin.setOnto = function(declarations, prop, originalValue) {
  18. var append, val, vals;
  19. append = '';
  20. val = _Declaration.sanitizeValue(originalValue);
  21. if (_Declaration.importantClauseRx.test(String(val))) {
  22. append = ' !important';
  23. val = val.replace(_Declaration.importantClauseRx, '');
  24. }
  25. val = val.trim();
  26. if (val.length === 0) {
  27. return self._setAllDirections(declarations, append, append, append, append);
  28. }
  29. vals = val.split(" ").map(function(val) {
  30. return val + append;
  31. });
  32. if (vals.length === 1) {
  33. return self._setAllDirections(declarations, vals[0], vals[0], vals[0], vals[0]);
  34. } else if (vals.length === 2) {
  35. return self._setAllDirections(declarations, vals[0], vals[1], vals[0], vals[1]);
  36. } else if (vals.length === 3) {
  37. return self._setAllDirections(declarations, vals[0], vals[1], vals[2], vals[1]);
  38. } else if (vals.length === 4) {
  39. return self._setAllDirections(declarations, vals[0], vals[1], vals[2], vals[3]);
  40. } else {
  41. throw Error("Can't understand value for margin: `" + originalValue + "`");
  42. }
  43. };
  44. Margin._setAllDirections = function(declarations, top, right, bottom, left) {
  45. MarginTop.setOnto(declarations, 'marginTop', top);
  46. MarginTop.setOnto(declarations, 'marginRight', right);
  47. MarginTop.setOnto(declarations, 'marginBottom', bottom);
  48. MarginTop.setOnto(declarations, 'marginLeft', left);
  49. };
  50. return Margin;
  51. })(_Declaration);