JSLint extensions

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2695 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Brett Zamir
2014-02-12 09:38:38 +00:00
parent d97e21b604
commit d6cc464ba5
17 changed files with 767 additions and 664 deletions

View File

@@ -1,3 +1,5 @@
/*globals svgEditor, svgCanvas, svgedit, $*/
/*jslint vars: true, eqeq: true, todo: true */
/*
* ext-polygon.js
*
@@ -6,23 +8,29 @@
* All rights reserved
*
*/
svgEditor.addExtension("polygon", function(S){
svgEditor.addExtension("polygon", function(S) {'use strict';
var NS = svgedit.NS,
svgcontent = S.svgcontent,
addElem = S.addSvgElementFromJson,
selElems,
editingitex = false, svgdoc = S.svgroot.parentNode.ownerDocument, started, newFO, edg = 0, newFOG, newFOGParent, newDef, newImageName, newMaskID, undoCommand = "Not image", modeChangeG;
var // NS = svgedit.NS,
// svgcontent = S.svgcontent,
// addElem = S.addSvgElementFromJson,
selElems,
editingitex = false,
// svgdoc = S.svgroot.parentNode.ownerDocument,
// newFOG, newFOGParent, newDef, newImageName, newMaskID, modeChangeG,
// edg = 0,
// undoCommand = "Not image";
started, newFO;
var ccZoom;
var wEl, hEl;
var wOffset, hOffset;
var ccRBG, ccRgbEl;
var ccOpacity;
var brushW, brushH, shape;
var ccDebug = document.getElementById('debugpanel');
// var ccZoom;
// var wEl, hEl;
// var wOffset, hOffset;
// var ccRBG;
var ccRgbEl;
// var ccOpacity;
// var brushW, brushH;
var shape;
// var ccDebug = document.getElementById('debugpanel');
/* var properlySourceSizeTextArea = function(){
// TODO: remove magic numbers here and get values from CSS
@@ -38,10 +46,12 @@ svgEditor.addExtension("polygon", function(S){
$('#polygon_panel').toggle(on);
}
/*
function toggleSourceButtons(on){
$('#tool_source_save, #tool_source_cancel').toggle(!on);
$('#polygon_save, #polygon_cancel').toggle(on);
}
*/
function setAttr(attr, val){
svgCanvas.changeSelectedAttribute(attr, val);
@@ -50,14 +60,57 @@ svgEditor.addExtension("polygon", function(S){
function cot(n){
return 1 / Math.tan(n);
};
}
function sec(n){
return 1 / Math.cos(n);
};
}
/**
* Obtained from http://code.google.com/p/passenger-top/source/browse/instiki/public/svg-edit/editor/extensions/ext-itex.js?r=3
* This function sets the content of of the currently-selected foreignObject element,
* based on the itex contained in string.
* @param {string} tex The itex text.
* @returns This function returns false if the set was unsuccessful, true otherwise.
*/
/*
function setItexString(tex) {
var mathns = 'http://www.w3.org/1998/Math/MathML',
xmlnsns = 'http://www.w3.org/2000/xmlns/',
ajaxEndpoint = '../../itex';
var elt = selElems[0];
try {
var math = svgdoc.createElementNS(mathns, 'math');
math.setAttributeNS(xmlnsns, 'xmlns', mathns);
math.setAttribute('display', 'inline');
var semantics = document.createElementNS(mathns, 'semantics');
var annotation = document.createElementNS(mathns, 'annotation');
annotation.setAttribute('encoding', 'application/x-tex');
annotation.textContent = tex;
var mrow = document.createElementNS(mathns, 'mrow');
semantics.appendChild(mrow);
semantics.appendChild(annotation);
math.appendChild(semantics);
// make an AJAX request to the server, to get the MathML
$.post(ajaxEndpoint, {'tex': tex, 'display': 'inline'}, function(data){
var children = data.documentElement.childNodes;
while (children.length > 0) {
mrow.appendChild(svgdoc.adoptNode(children[0], true));
}
S.sanitizeSvg(math);
S.call("changed", [elt]);
});
elt.replaceChild(math, elt.firstChild);
S.call("changed", [elt]);
svgCanvas.clearSelection();
} catch(e) {
console.log(e);
return false;
}
return true;
}
*/
return {
name: "polygon",
svgicons: svgEditor.curConfig.extPath + "polygon-icons.svg",
@@ -95,20 +148,22 @@ svgEditor.addExtension("polygon", function(S){
$('#polygon_panel').hide();
var endChanges = function(){
}
};
// TODO: Needs to be done after orig icon loads
setTimeout(function(){
// Create source save/cancel buttons
var save = $('#tool_source_save').clone().hide().attr('id', 'polygon_save').unbind().appendTo("#tool_source_back").click(function(){
if (!editingitex)
return;
if (!editingitex) {
return;
}
// Todo: Uncomment the setItexString() function above and handle ajaxEndpoint?
if (!setItexString($('#svg_source_textarea').val())) {
$.confirm("Errors found. Revert to original?", function(ok){
if (!ok)
return false;
if (!ok) {
return false;
}
endChanges();
});
}
@@ -125,13 +180,13 @@ svgEditor.addExtension("polygon", function(S){
}, 3000);
},
mouseDown: function(opts){
var e = opts.event;
rgb = svgCanvas.getColor("fill");
// var e = opts.event;
var rgb = svgCanvas.getColor("fill");
ccRgbEl = rgb.substring(1, rgb.length);
var sRgb = svgCanvas.getColor("stroke");
// ccSRgbEl = sRgb.substring(1, rgb.length);
sRgb = svgCanvas.getColor("stroke");
ccSRgbEl = sRgb.substring(1, rgb.length);
sRgb = svgCanvas.getColor("stroke");
sWidth = svgCanvas.getStrokeWidth();
var sWidth = svgCanvas.getStrokeWidth();
if (svgCanvas.getMode() == "polygon") {
started = true;
@@ -154,27 +209,31 @@ svgEditor.addExtension("polygon", function(S){
return {
started: true
}
};
}
},
mouseMove: function(opts){
if (!started)
if (!started) {
return;
}
if (svgCanvas.getMode() == "polygon") {
var e = opts.event;
// var e = opts.event;
var x = opts.mouse_x;
var y = opts.mouse_y;
var c = $(newFO).attr(["cx", "cy", "sides", "orient", "fill", "strokecolor", "strokeWidth"]);
var cx = c.cx, cy = c.cy, fill = c.fill, strokecolor = c.strokecolor, strokewidth = c.strokeWidth, sides = c.sides, orient = c.orient, edg = (Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))) / 1.5;
var cx = c.cx, cy = c.cy, fill = c.fill, strokecolor = c.strokecolor, strokewidth = c.strokeWidth, sides = c.sides,
// orient = c.orient,
edg = (Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))) / 1.5;
newFO.setAttributeNS(null, "edge", edg);
var inradius = (edg / 2) * cot(Math.PI / sides);
var circumradius = inradius * sec(Math.PI / sides);
var points = '';
for (var s = 0; sides >= s; s++) {
var s;
for (s = 0; sides >= s; s++) {
var angle = 2.0 * Math.PI * s / sides;
var x = (circumradius * Math.cos(angle)) + cx;
var y = (circumradius * Math.sin(angle)) + cy;
x = (circumradius * Math.cos(angle)) + cx;
y = (circumradius * Math.sin(angle)) + cy;
points += x + ',' + y + ' ';
}
@@ -190,7 +249,7 @@ svgEditor.addExtension("polygon", function(S){
//DrawPoly(cx, cy, sides, edg, orient);
return {
started: true
}
};
}
},
@@ -198,12 +257,12 @@ svgEditor.addExtension("polygon", function(S){
mouseUp: function(opts){
if (svgCanvas.getMode() == "polygon") {
var attrs = $(newFO).attr("edge");
keep = (attrs.edge != 0);
var keep = (attrs.edge != 0);
// svgCanvas.addToSelection([newFO], true);
return {
keep: keep,
element: newFO
}
};
}
},
@@ -215,7 +274,7 @@ svgEditor.addExtension("polygon", function(S){
while (i--) {
var elem = selElems[i];
if (elem && elem.getAttributeNS(null, 'shape') == "regularPoly") {
if (elem && elem.getAttributeNS(null, 'shape') === 'regularPoly') {
if (opts.selectedElement && !opts.multiselected) {
$('#polySides').val(elem.getAttribute("sides"));
@@ -231,7 +290,7 @@ svgEditor.addExtension("polygon", function(S){
}
},
elementChanged: function(opts){
var elem = opts.elems[0];
// var elem = opts.elems[0];
}
};
});