Skip to content

Commit

Permalink
Move core (non-DOM) svg library to glift-core.
Browse files Browse the repository at this point in the history
Also, while I'm here, fix the broken images on the testers.
  • Loading branch information
artasparks committed Nov 2, 2016
1 parent 273d819 commit 1e4d0d9
Show file tree
Hide file tree
Showing 29 changed files with 510 additions and 471 deletions.
11 changes: 9 additions & 2 deletions deps/glift-core/glift.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ goog.provide('glift');

(function(w) {

var glift = w.glift || {};
var g;
if (typeof glift !== 'undefined') {
g = glift;
} else if (typeof w.glift !== 'undefined') {
g = w.glift
} else {
g = {};
}
if (w) {
// expose Glift as a global.
w.glift = glift;
w.glift = g ;
}
})(window);
63 changes: 63 additions & 0 deletions deps/glift-core/svg/pathutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
goog.provide('glift.svg.pathutils');

glift.svg.pathutils = {
/**
* Move the current position to X,Y. Usually used in the context of creating a
* path.
* @param {number} x
* @param {number} y
* @return {string}
*/
move: function(x, y) {
return "M" + x + " " + y;
},

/**
* Like move, but with a glift point.
* @param {!glift.Point} pt
* @return {string}
*/
movePt: function(pt) {
return glift.svg.pathutils.move(pt.x(), pt.y());
},

/**
* Create a relative SVG line, starting from the 'current' position. I.e.,
* the (0,0) point is that last place drawn-to or moved-to.
* @param {number} x
* @param {number} y
* @return {string}
*/
lineRel: function(x, y) {
return "l" + x + " " + y;
},

/**
* Like lineRel, but with a pt.
* @param {!glift.Point} pt
* @return {string}
*/
lineRelPt: function(pt) {
return glift.svg.pathutils.lineRel(pt.x(), pt.y());
},

/**
* Create an absolute SVG line -- different from lower case.
* This form is usually preferred.
* @param {number} x
* @param {number} y
* @return {string}
*/
lineAbs: function(x, y) {
return "L" + x + " " + y;
},

/**
* Like lineAbs, but with a pt.
* @param {!glift.Point} pt
* @return {string}
*/
lineAbsPt: function(pt) {
return glift.svg.pathutils.lineAbs(pt.x(), pt.y());
},
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
module("glift.displays.svg.pathUtilsTest");
var path = glift.displays.svg.pathutils;
module("glift.svg.pathUtilsTest");
var path = glift.svg.pathutils;
var pt = glift.util.point(3, 5);

test("Test path move", function() {
Expand Down
7 changes: 7 additions & 0 deletions deps/glift-core/svg/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
goog.provide('glift.svg');

/**
* SVG utilities. Arguably, this should be in the Glift ui. But the utilities,
* modulo some dom-utilities, are agnostic to any rendering engine.
*/
glift.svg = {};
289 changes: 289 additions & 0 deletions deps/glift-core/svg/svgobj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
goog.provide('glift.svg.SvgObj');

/**
* Creats a SVG Wrapper object.
*
* @param {string} type Svg element type.
* @param {!Object<string>=} opt_attrObj optional attribute object.
*/
glift.svg.createObj = function(type, opt_attrObj) {
return new glift.svg.SvgObj(type, opt_attrObj);
};

/**
* Creates a root SVG object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.svg = function(opt_attrObj) {
return new glift.svg.SvgObj('svg', opt_attrObj)
.setAttr('version', '1.1')
.setAttr('xmlns', 'http://www.w3.org/2000/svg');
};

/**
* Creates a circle svg object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.circle = function(opt_attrObj) {
return new glift.svg.SvgObj('circle', opt_attrObj);
};

/**
* Creates a path svg object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.path = function(opt_attrObj) {
return new glift.svg.SvgObj('path', opt_attrObj);
};

/**
* Creates an rectangle svg object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.rect = function(opt_attrObj) {
return new glift.svg.SvgObj('rect', opt_attrObj);
};

/**
* Creates an image svg object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.image = function(opt_attrObj) {
return new glift.svg.SvgObj('image', opt_attrObj);
};

/**
* Creates a text svg object.
* @param {!Object<string>=} opt_attrObj optional attribute object.
* @return {!glift.svg.SvgObj}
*/
glift.svg.text = function(opt_attrObj) {
return new glift.svg.SvgObj('text', opt_attrObj);
};

/**
* Create a group object (without any attributes)
* @return {!glift.svg.SvgObj}
*/
glift.svg.group = function() {
return new glift.svg.SvgObj('g');
};

/**
* SVG Wrapper object.
* @constructor @final @struct
*
* @param {string} type Svg element type.
* @param {Object<string>=} opt_attrObj optional attribute object.
*/
glift.svg.SvgObj = function(type, opt_attrObj) {
/** @private {string} */
this.type_ = type;
/** @private {!Object<string>} */
this.attrMap_ = opt_attrObj || {};
/** @private {!Array<!glift.svg.SvgObj>} */
this.children_ = [];
/** @private {!Object<!glift.svg.SvgObj>} */
this.idMap_ = {};
/** @private {string} */
this.text_ = '';
/** @private {?Object} */
this.data_ = null;
};

glift.svg.SvgObj.prototype = {
/**
* Return the string form of the svg object.
* @return {string}
*/
render: function() {
var base = '<' + this.type_;
for (var key in this.attrMap_) {
base += ' ' + key + '="' + this.attrMap_[key] + '"';
}
base += '>' + this.text_;
if (this.children_.length > 0) {
base += '\n';
for (var i = 0; i < this.children_.length; i++) {
base += this.children_[i].render() + '\n';
}
base += '</' + this.type_ + '>';
} else {
base += '</' + this.type_ + '>';
}
return base;
},

/** @return {string} A value in the attribute map. */
attr: function(key) {
return this.attrMap_[key];
},

/**
* Sets an SVG attribute.
* @param {string} key The key of an object in the map.
* @param {string|number} value The value to set in the map.
* @return {!glift.svg.SvgObj} This object.
*/
setAttr: function(key, value) {
this.attrMap_[key] = value + '';
return this;
},

/** @return {?string} the Id of this object or null. */
id: function() {
return /** @type {?string} */ (this.attrMap_['id'] || null);
},

/**
* Convenience method to avoid null ID type.
* @return {string}
*/
idOrThrow: function() {
if (this.id() == null) {
throw new Error('ID was null; expected to be non-null');
}
return /** @type {string} */ (this.id());
},

/**
* Sets the ID (using the Attribute object as a store).
* @param {string} id
* @return {!glift.svg.SvgObj} This object.
*/
setId: function(id) {
if (id) {
this.attrMap_['id'] = id;
}
return this;
},

/** @return {!Object<string>} The attribute object. */
attrObj: function() {
return this.attrMap_;
},

/**
* Sets the entire attribute object.
* @param {!Object<string>} attrObj
* @return {!glift.svg.SvgObj} This object.
*/
setAttrObj: function(attrObj) {
if (glift.util.typeOf(attrObj) !== 'object') {
throw new Error('Attr obj must be of type object');
}
this.attrMap_ = attrObj;
return this;
},

/** @return {?Object} The node's data */
data: function() {
return this.data_
},

/**
* Set some internal data. Note: this data is not attached when the element is
* generated.
* @param {!Object} data
* @return {!glift.svg.SvgObj} This object.
*/
setData: function(data) {
this.data_ = data;
return this;
},

/** @return {string} The text on the node. */
text: function() {
return this.text_;
},

/**
* Append some text. Usually only for text elements.
* @param {string} text
* @return {!glift.svg.SvgObj} This object.
*/
setText: function(text) {
this.text_ = text;
return this;
},

/** @return {string} The type of this object. */
type: function() {
return this.type_;
},

/**
* Get child from an Id.
* @return {!glift.svg.SvgObj} The child obj.
*/
child: function(id) {
return this.idMap_[id];
},

/**
* Remove child, based on id.
* @return {!glift.svg.SvgObj} This object.
*/
rmChild: function(id) {
delete this.idMap_[id];
return this;
},

/**
* Get all the Children.
* @return {!Array<!glift.svg.SvgObj>}
*/
children: function() {
return this.children_;
},

/**
* Empty out all the children.
* @return {!glift.svg.SvgObj} this object.
*/
emptyChildren: function() {
this.children_ = [];
return this;
},

/**
* Add an already existing child.
* @param {!glift.svg.SvgObj} obj Object to add.
* @return {!glift.svg.SvgObj} This object.
*/
append: function(obj) {
if (obj.id() !== undefined) {
this.idMap_[obj.id()] = obj;
}
this.children_.push(obj);
return this;
},

/**
* Add a new svg object child.
* @param {string} type
* @param {!Object<string>} attrObj
* @return {!glift.svg.SvgObj} This object.
*/
appendNew: function(type, attrObj) {
var obj = glift.svg.createObj(type, attrObj);
return this.append(obj);
},

/**
* Create a copy of the object without any children
* @return {!glift.svg.SvgObj} The new object.
*/
copyNoChildren: function() {
var newAttr = {};
for (var key in this.attrMap_) {
newAttr[key] = this.attrMap_[key];
}
return glift.svg.createObj(this.type_, newAttr);
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
module('glift.displays.svg.svgObjTest');
var svg = glift.displays.svg;
module('glift.svg.svgObjTest');
var svg = glift.svg;

test('Test svg creation', function() {
deepEqual(svg.svg().render(),
Expand Down
Loading

0 comments on commit 1e4d0d9

Please sign in to comment.