PageRenderTime 3387ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 2ms

/editor/svg-editor.js

http://svg-edit.googlecode.com/
JavaScript | 5180 lines | 3927 code | 588 blank | 665 comment | 744 complexity | b945fbd6e1fac0e3a488181d21e0bd89 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*globals saveAs:true, svgEditor:true, globalStorage, widget, svgedit, canvg, jsPDF, svgElementToPdf, jQuery, $, DOMParser, FileReader, URL */
  2. /*jslint vars: true, eqeq: true, todo: true, forin: true, continue: true, regexp: true */
  3. /*
  4. * svg-editor.js
  5. *
  6. * Licensed under the MIT License
  7. *
  8. * Copyright(c) 2010 Alexis Deveria
  9. * Copyright(c) 2010 Pavol Rusnak
  10. * Copyright(c) 2010 Jeff Schiller
  11. * Copyright(c) 2010 Narendra Sisodiya
  12. * Copyright(c) 2014 Brett Zamir
  13. *
  14. */
  15. // Dependencies:
  16. // 1) units.js
  17. // 2) browser.js
  18. // 3) svgcanvas.js
  19. /*
  20. TODOS
  21. 1. JSDoc
  22. */
  23. (function() {
  24. if (window.svgEditor) {
  25. return;
  26. }
  27. window.svgEditor = (function($) {
  28. var editor = {};
  29. // EDITOR PROPERTIES: (defined below)
  30. // curPrefs, curConfig, canvas, storage, uiStrings
  31. //
  32. // STATE MAINTENANCE PROPERTIES
  33. editor.tool_scale = 1; // Dependent on icon size, so any use to making configurable instead? Used by JQuerySpinBtn.js
  34. editor.langChanged = false;
  35. editor.showSaveWarning = false;
  36. editor.storagePromptClosed = false; // For use with ext-storage.js
  37. var svgCanvas, urldata,
  38. Utils = svgedit.utilities,
  39. isReady = false,
  40. callbacks = [],
  41. customHandlers = {},
  42. /**
  43. * PREFS AND CONFIG
  44. */
  45. // The iteration algorithm for defaultPrefs does not currently support array/objects
  46. defaultPrefs = {
  47. // EDITOR OPTIONS (DIALOG)
  48. lang: '', // Default to "en" if locale.js detection does not detect another language
  49. iconsize: '', // Will default to 's' if the window height is smaller than the minimum height and 'm' otherwise
  50. bkgd_color: '#FFF',
  51. bkgd_url: '',
  52. // DOCUMENT PROPERTIES (DIALOG)
  53. img_save: 'embed',
  54. // ALERT NOTICES
  55. // Only shows in UI as far as alert notices, but useful to remember, so keeping as pref
  56. save_notice_done: false,
  57. export_notice_done: false
  58. },
  59. curPrefs = {},
  60. // Note: The difference between Prefs and Config is that Prefs
  61. // can be changed in the UI and are stored in the browser,
  62. // while config cannot
  63. curConfig = {
  64. // We do not put on defaultConfig to simplify object copying
  65. // procedures (we obtain instead from defaultExtensions)
  66. extensions: [],
  67. /**
  68. * Can use window.location.origin to indicate the current
  69. * origin. Can contain a '*' to allow all domains or 'null' (as
  70. * a string) to support all file:// URLs. Cannot be set by
  71. * URL for security reasons (not safe, at least for
  72. * privacy or data integrity of SVG content).
  73. * Might have been fairly safe to allow
  74. * `new URL(window.location.href).origin` by default but
  75. * avoiding it ensures some more security that even third
  76. * party apps on the same domain also cannot communicate
  77. * with this app by default.
  78. * For use with ext-xdomain-messaging.js
  79. * @todo We might instead make as a user-facing preference.
  80. */
  81. allowedOrigins: []
  82. },
  83. defaultExtensions = [
  84. 'ext-overview_window.js',
  85. 'ext-markers.js',
  86. 'ext-connector.js',
  87. 'ext-eyedropper.js',
  88. 'ext-shapes.js',
  89. 'ext-imagelib.js',
  90. 'ext-grid.js',
  91. 'ext-polygon.js',
  92. 'ext-star.js',
  93. 'ext-panning.js',
  94. 'ext-storage.js'
  95. ],
  96. defaultConfig = {
  97. // Todo: svgcanvas.js also sets and checks: show_outside_canvas, selectNew; add here?
  98. // Change the following to preferences and add pref controls to the UI (e.g., initTool, wireframe, showlayers)?
  99. canvasName: 'default',
  100. canvas_expansion: 3,
  101. initFill: {
  102. color: 'FF0000', // solid red
  103. opacity: 1
  104. },
  105. initStroke: {
  106. width: 5,
  107. color: '000000', // solid black
  108. opacity: 1
  109. },
  110. initOpacity: 1,
  111. colorPickerCSS: null, // Defaults to 'left' with a position equal to that of the fill_color or stroke_color element minus 140, and a 'bottom' equal to 40
  112. initTool: 'select',
  113. wireframe: false,
  114. showlayers: false,
  115. no_save_warning: false,
  116. // PATH CONFIGURATION
  117. // The following path configuration items are disallowed in the URL (as should any future path configurations)
  118. imgPath: 'images/',
  119. langPath: 'locale/',
  120. extPath: 'extensions/',
  121. jGraduatePath: 'jgraduate/images/',
  122. // DOCUMENT PROPERTIES
  123. // Change the following to a preference (already in the Document Properties dialog)?
  124. dimensions: [640, 480],
  125. // EDITOR OPTIONS
  126. // Change the following to preferences (already in the Editor Options dialog)?
  127. gridSnapping: false,
  128. gridColor: '#000',
  129. baseUnit: 'px',
  130. snappingStep: 10,
  131. showRulers: true,
  132. // URL BEHAVIOR CONFIGURATION
  133. preventAllURLConfig: false,
  134. preventURLContentLoading: false,
  135. // EXTENSION CONFIGURATION (see also preventAllURLConfig)
  136. lockExtensions: false, // Disallowed in URL setting
  137. noDefaultExtensions: false, // noDefaultExtensions can only be meaningfully used in config.js or in the URL
  138. // EXTENSION-RELATED (GRID)
  139. showGrid: false, // Set by ext-grid.js
  140. // EXTENSION-RELATED (STORAGE)
  141. noStorageOnLoad: false, // Some interaction with ext-storage.js; prevent even the loading of previously saved local storage
  142. forceStorage: false, // Some interaction with ext-storage.js; strongly discouraged from modification as it bypasses user privacy by preventing them from choosing whether to keep local storage or not
  143. emptyStorageOnDecline: false // Used by ext-storage.js; empty any prior storage if the user declines to store
  144. },
  145. /**
  146. * LOCALE
  147. * @todo Can we remove now that we are always loading even English? (unless locale is set to null)
  148. */
  149. uiStrings = editor.uiStrings = {
  150. common: {
  151. ok: 'OK',
  152. cancel: 'Cancel',
  153. key_up: 'Up',
  154. key_down: 'Down',
  155. key_backspace: 'Backspace',
  156. key_del: 'Del'
  157. },
  158. // This is needed if the locale is English, since the locale strings are not read in that instance.
  159. layers: {
  160. layer: 'Layer'
  161. },
  162. notification: {
  163. invalidAttrValGiven: 'Invalid value given',
  164. noContentToFitTo: 'No content to fit to',
  165. dupeLayerName: 'There is already a layer named that!',
  166. enterUniqueLayerName: 'Please enter a unique layer name',
  167. enterNewLayerName: 'Please enter the new layer name',
  168. layerHasThatName: 'Layer already has that name',
  169. QmoveElemsToLayer: 'Move selected elements to layer \'%s\'?',
  170. QwantToClear: 'Do you want to clear the drawing?\nThis will also erase your undo history!',
  171. QwantToOpen: 'Do you want to open a new file?\nThis will also erase your undo history!',
  172. QerrorsRevertToSource: 'There were parsing errors in your SVG source.\nRevert back to original SVG source?',
  173. QignoreSourceChanges: 'Ignore changes made to SVG source?',
  174. featNotSupported: 'Feature not supported',
  175. enterNewImgURL: 'Enter the new image URL',
  176. defsFailOnSave: 'NOTE: Due to a bug in your browser, this image may appear wrong (missing gradients or elements). It will however appear correct once actually saved.',
  177. loadingImage: 'Loading image, please wait...',
  178. saveFromBrowser: 'Select \'Save As...\' in your browser to save this image as a %s file.',
  179. noteTheseIssues: 'Also note the following issues: ',
  180. unsavedChanges: 'There are unsaved changes.',
  181. enterNewLinkURL: 'Enter the new hyperlink URL',
  182. errorLoadingSVG: 'Error: Unable to load SVG data',
  183. URLloadFail: 'Unable to load from URL',
  184. retrieving: 'Retrieving \'%s\' ...'
  185. }
  186. };
  187. function loadSvgString (str, callback) {
  188. var success = svgCanvas.setSvgString(str) !== false;
  189. callback = callback || $.noop;
  190. if (success) {
  191. callback(true);
  192. } else {
  193. $.alert(uiStrings.notification.errorLoadingSVG, function() {
  194. callback(false);
  195. });
  196. }
  197. }
  198. function checkCanvg (callCanvg) {
  199. return function (win, data) {
  200. if (window.canvg) {
  201. callCanvg(win, data);
  202. } else { // Might not be set up yet
  203. $.getScript('canvg/rgbcolor.js', function() {
  204. $.getScript('canvg/canvg.js', function() {
  205. callCanvg(win, data);
  206. });
  207. });
  208. }
  209. };
  210. }
  211. /**
  212. * EXPORTS
  213. */
  214. /**
  215. * Store and retrieve preferences
  216. * @param {string} key The preference name to be retrieved or set
  217. * @param {string} [val] The value. If the value supplied is missing or falsey, no change to the preference will be made.
  218. * @returns {string} If val is missing or falsey, the value of the previously stored preference will be returned.
  219. * @todo Can we change setting on the jQuery namespace (onto editor) to avoid conflicts?
  220. * @todo Review whether any remaining existing direct references to
  221. * getting curPrefs can be changed to use $.pref() getting to ensure
  222. * defaultPrefs fallback (also for sake of allowInitialUserOverride); specifically, bkgd_color could be changed so that
  223. * the pref dialog has a button to auto-calculate background, but otherwise uses $.pref() to be able to get default prefs
  224. * or overridable settings
  225. */
  226. $.pref = function (key, val) {
  227. if (val) {
  228. curPrefs[key] = val;
  229. editor.curPrefs = curPrefs; // Update exported value
  230. return;
  231. }
  232. return (key in curPrefs) ? curPrefs[key] : defaultPrefs[key];
  233. };
  234. /**
  235. * EDITOR PUBLIC METHODS
  236. * locale.js also adds "putLang" and "readLang" as editor methods
  237. * @todo Sort these methods per invocation order, ideally with init at the end
  238. * @todo Prevent execution until init executes if dependent on it?
  239. */
  240. /**
  241. * Where permitted, sets canvas and/or defaultPrefs based on previous
  242. * storage. This will override URL settings (for security reasons) but
  243. * not config.js configuration (unless initial user overriding is explicitly
  244. * permitted there via allowInitialUserOverride).
  245. * @todo Split allowInitialUserOverride into allowOverrideByURL and
  246. * allowOverrideByUserStorage so config.js can disallow some
  247. * individual items for URL setting but allow for user storage AND/OR
  248. * change URL setting so that it always uses a different namespace,
  249. * so it won't affect pre-existing user storage (but then if users saves
  250. * that, it will then be subject to tampering
  251. */
  252. editor.loadContentAndPrefs = function () {
  253. if (!curConfig.forceStorage && (curConfig.noStorageOnLoad || !document.cookie.match(/(?:^|;\s*)store=(?:prefsAndContent|prefsOnly)/))) {
  254. return;
  255. }
  256. // LOAD CONTENT
  257. if (editor.storage && // Cookies do not have enough available memory to hold large documents
  258. (curConfig.forceStorage || (!curConfig.noStorageOnLoad && document.cookie.match(/(?:^|;\s*)store=prefsAndContent/)))
  259. ) {
  260. var name = 'svgedit-' + curConfig.canvasName;
  261. var cached = editor.storage.getItem(name);
  262. if (cached) {
  263. editor.loadFromString(cached);
  264. }
  265. }
  266. // LOAD PREFS
  267. var key;
  268. for (key in defaultPrefs) {
  269. if (defaultPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain
  270. var storeKey = 'svg-edit-' + key;
  271. if (editor.storage) {
  272. var val = editor.storage.getItem(storeKey);
  273. if (val) {
  274. defaultPrefs[key] = String(val); // Convert to string for FF (.value fails in Webkit)
  275. }
  276. }
  277. else if (window.widget) {
  278. defaultPrefs[key] = widget.preferenceForKey(storeKey);
  279. }
  280. else {
  281. var result = document.cookie.match(new RegExp('(?:^|;\\s*)' + Utils.preg_quote(encodeURIComponent(storeKey)) + '=([^;]+)'));
  282. defaultPrefs[key] = result ? decodeURIComponent(result[1]) : '';
  283. }
  284. }
  285. }
  286. };
  287. /**
  288. * Allows setting of preferences or configuration (including extensions).
  289. * @param {object} opts The preferences or configuration (including extensions)
  290. * @param {object} [cfgCfg] Describes configuration which applies to the particular batch of supplied options
  291. * @param {boolean} [cfgCfg.allowInitialUserOverride=false] Set to true if you wish
  292. * to allow initial overriding of settings by the user via the URL
  293. * (if permitted) or previously stored preferences (if permitted);
  294. * note that it will be too late if you make such calls in extension
  295. * code because the URL or preference storage settings will
  296. * have already taken place.
  297. * @param {boolean} [cfgCfg.overwrite=true] Set to false if you wish to
  298. * prevent the overwriting of prior-set preferences or configuration
  299. * (URL settings will always follow this requirement for security
  300. * reasons, so config.js settings cannot be overridden unless it
  301. * explicitly permits via "allowInitialUserOverride" but extension config
  302. * can be overridden as they will run after URL settings). Should
  303. * not be needed in config.js.
  304. */
  305. editor.setConfig = function (opts, cfgCfg) {
  306. cfgCfg = cfgCfg || {};
  307. function extendOrAdd (cfgObj, key, val) {
  308. if (cfgObj[key] && typeof cfgObj[key] === 'object') {
  309. $.extend(true, cfgObj[key], val);
  310. }
  311. else {
  312. cfgObj[key] = val;
  313. }
  314. return;
  315. }
  316. $.each(opts, function(key, val) {
  317. if (opts.hasOwnProperty(key)) {
  318. // Only allow prefs defined in defaultPrefs
  319. if (defaultPrefs.hasOwnProperty(key)) {
  320. if (cfgCfg.overwrite === false && (
  321. curConfig.preventAllURLConfig ||
  322. curPrefs.hasOwnProperty(key)
  323. )) {
  324. return;
  325. }
  326. if (cfgCfg.allowInitialUserOverride === true) {
  327. defaultPrefs[key] = val;
  328. }
  329. else {
  330. $.pref(key, val);
  331. }
  332. }
  333. else if (['extensions', 'allowedOrigins'].indexOf(key) > -1) {
  334. if (cfgCfg.overwrite === false &&
  335. (
  336. curConfig.preventAllURLConfig ||
  337. key === 'allowedOrigins' ||
  338. (key === 'extensions' && curConfig.lockExtensions)
  339. )
  340. ) {
  341. return;
  342. }
  343. curConfig[key] = curConfig[key].concat(val); // We will handle any dupes later
  344. }
  345. // Only allow other curConfig if defined in defaultConfig
  346. else if (defaultConfig.hasOwnProperty(key)) {
  347. if (cfgCfg.overwrite === false && (
  348. curConfig.preventAllURLConfig ||
  349. curConfig.hasOwnProperty(key)
  350. )) {
  351. return;
  352. }
  353. // Potentially overwriting of previously set config
  354. if (curConfig.hasOwnProperty(key)) {
  355. if (cfgCfg.overwrite === false) {
  356. return;
  357. }
  358. extendOrAdd(curConfig, key, val);
  359. }
  360. else {
  361. if (cfgCfg.allowInitialUserOverride === true) {
  362. extendOrAdd(defaultConfig, key, val);
  363. }
  364. else {
  365. if (defaultConfig[key] && typeof defaultConfig[key] === 'object') {
  366. curConfig[key] = {};
  367. $.extend(true, curConfig[key], val); // Merge properties recursively, e.g., on initFill, initStroke objects
  368. }
  369. else {
  370. curConfig[key] = val;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. });
  377. editor.curConfig = curConfig; // Update exported value
  378. };
  379. /**
  380. * @param {object} opts Extension mechanisms may call setCustomHandlers with three functions: opts.open, opts.save, and opts.exportImage
  381. * opts.open's responsibilities are:
  382. * - invoke a file chooser dialog in 'open' mode
  383. * - let user pick a SVG file
  384. * - calls setCanvas.setSvgString() with the string contents of that file
  385. * opts.save's responsibilities are:
  386. * - accept the string contents of the current document
  387. * - invoke a file chooser dialog in 'save' mode
  388. * - save the file to location chosen by the user
  389. * opts.exportImage's responsibilities (with regard to the object it is supplied in its 2nd argument) are:
  390. * - inform user of any issues supplied via the "issues" property
  391. * - convert the "svg" property SVG string into an image for export;
  392. * utilize the properties "type" (currently 'PNG', 'JPEG', 'BMP',
  393. * 'WEBP', 'PDF'), "mimeType", and "quality" (for 'JPEG' and 'WEBP'
  394. * types) to determine the proper output.
  395. */
  396. editor.setCustomHandlers = function (opts) {
  397. editor.ready(function() {
  398. if (opts.open) {
  399. $('#tool_open > input[type="file"]').remove();
  400. $('#tool_open').show();
  401. svgCanvas.open = opts.open;
  402. }
  403. if (opts.save) {
  404. editor.showSaveWarning = false;
  405. svgCanvas.bind('saved', opts.save);
  406. }
  407. if (opts.exportImage) {
  408. svgCanvas.bind('exported', checkCanvg(opts.exportImage));
  409. }
  410. customHandlers = opts;
  411. });
  412. };
  413. editor.randomizeIds = function () {
  414. svgCanvas.randomizeIds(arguments);
  415. };
  416. editor.init = function () {
  417. // var host = location.hostname,
  418. // onWeb = host && host.indexOf('.') >= 0;
  419. // Some FF versions throw security errors here when directly accessing
  420. try {
  421. if ('localStorage' in window) { // && onWeb removed so Webkit works locally
  422. editor.storage = localStorage;
  423. }
  424. } catch(err) {}
  425. // Todo: Avoid var-defined functions and group functions together, etc. where possible
  426. var good_langs = [];
  427. $('#lang_select option').each(function() {
  428. good_langs.push(this.value);
  429. });
  430. function setupCurPrefs () {
  431. curPrefs = $.extend(true, {}, defaultPrefs, curPrefs); // Now safe to merge with priority for curPrefs in the event any are already set
  432. // Export updated prefs
  433. editor.curPrefs = curPrefs;
  434. }
  435. function setupCurConfig () {
  436. curConfig = $.extend(true, {}, defaultConfig, curConfig); // Now safe to merge with priority for curConfig in the event any are already set
  437. // Now deal with extensions and other array config
  438. if (!curConfig.noDefaultExtensions) {
  439. curConfig.extensions = curConfig.extensions.concat(defaultExtensions);
  440. }
  441. // ...and remove any dupes
  442. $.each(['extensions', 'allowedOrigins'], function (i, cfg) {
  443. curConfig[cfg] = $.grep(curConfig[cfg], function (n, i) {
  444. return i === curConfig[cfg].indexOf(n);
  445. });
  446. });
  447. // Export updated config
  448. editor.curConfig = curConfig;
  449. }
  450. (function() {
  451. // Load config/data from URL if given
  452. var src, qstr;
  453. urldata = $.deparam.querystring(true);
  454. if (!$.isEmptyObject(urldata)) {
  455. if (urldata.dimensions) {
  456. urldata.dimensions = urldata.dimensions.split(',');
  457. }
  458. if (urldata.bkgd_color) {
  459. urldata.bkgd_color = '#' + urldata.bkgd_color;
  460. }
  461. if (urldata.extensions) {
  462. // For security reasons, disallow cross-domain or cross-folder extensions via URL
  463. urldata.extensions = urldata.extensions.match(/[:\/\\]/) ? '' : urldata.extensions.split(',');
  464. }
  465. // Disallowing extension paths via URL for
  466. // security reasons, even for same-domain
  467. // ones given potential to interact in undesirable
  468. // ways with other script resources
  469. $.each(
  470. [
  471. 'extPath', 'imgPath',
  472. 'langPath', 'jGraduatePath'
  473. ],
  474. function (pathConfig) {
  475. if (urldata[pathConfig]) {
  476. delete urldata[pathConfig];
  477. }
  478. }
  479. );
  480. editor.setConfig(urldata, {overwrite: false}); // Note: source and url (as with storagePrompt later) are not set on config but are used below
  481. setupCurConfig();
  482. if (!curConfig.preventURLContentLoading) {
  483. src = urldata.source;
  484. qstr = $.param.querystring();
  485. if (!src) { // urldata.source may have been null if it ended with '='
  486. if (qstr.indexOf('source=data:') >= 0) {
  487. src = qstr.match(/source=(data:[^&]*)/)[1];
  488. }
  489. }
  490. if (src) {
  491. if (src.indexOf('data:') === 0) {
  492. editor.loadFromDataURI(src);
  493. } else {
  494. editor.loadFromString(src);
  495. }
  496. return;
  497. }
  498. if (urldata.url) {
  499. editor.loadFromURL(urldata.url);
  500. return;
  501. }
  502. }
  503. if (!urldata.noStorageOnLoad || curConfig.forceStorage) {
  504. editor.loadContentAndPrefs();
  505. }
  506. setupCurPrefs();
  507. }
  508. else {
  509. setupCurConfig();
  510. editor.loadContentAndPrefs();
  511. setupCurPrefs();
  512. }
  513. }());
  514. // For external openers
  515. (function() {
  516. // let the opener know SVG Edit is ready (now that config is set up)
  517. var svgEditorReadyEvent,
  518. w = window.opener;
  519. if (w) {
  520. try {
  521. svgEditorReadyEvent = w.document.createEvent('Event');
  522. svgEditorReadyEvent.initEvent('svgEditorReady', true, true);
  523. w.document.documentElement.dispatchEvent(svgEditorReadyEvent);
  524. }
  525. catch(e) {}
  526. }
  527. }());
  528. var setIcon = editor.setIcon = function(elem, icon_id, forcedSize) {
  529. var icon = (typeof icon_id === 'string') ? $.getSvgIcon(icon_id, true) : icon_id.clone();
  530. if (!icon) {
  531. console.log('NOTE: Icon image missing: ' + icon_id);
  532. return;
  533. }
  534. $(elem).empty().append(icon);
  535. };
  536. var extFunc = function() {
  537. $.each(curConfig.extensions, function() {
  538. var extname = this;
  539. if (!extname.match(/^ext-.*\.js/)) { // Ensure URL cannot specify some other unintended file in the extPath
  540. return;
  541. }
  542. $.getScript(curConfig.extPath + extname, function(d) {
  543. // Fails locally in Chrome 5
  544. if (!d) {
  545. var s = document.createElement('script');
  546. s.src = curConfig.extPath + extname;
  547. document.querySelector('head').appendChild(s);
  548. }
  549. });
  550. });
  551. // var lang = ('lang' in curPrefs) ? curPrefs.lang : null;
  552. editor.putLocale(null, good_langs);
  553. };
  554. // Load extensions
  555. // Bit of a hack to run extensions in local Opera/IE9
  556. if (document.location.protocol === 'file:') {
  557. setTimeout(extFunc, 100);
  558. } else {
  559. extFunc();
  560. }
  561. $.svgIcons(curConfig.imgPath + 'svg_edit_icons.svg', {
  562. w:24, h:24,
  563. id_match: false,
  564. no_img: !svgedit.browser.isWebkit(), // Opera & Firefox 4 gives odd behavior w/images
  565. fallback_path: curConfig.imgPath,
  566. fallback: {
  567. 'new_image': 'clear.png',
  568. 'save': 'save.png',
  569. 'open': 'open.png',
  570. 'source': 'source.png',
  571. 'docprops': 'document-properties.png',
  572. 'wireframe': 'wireframe.png',
  573. 'undo': 'undo.png',
  574. 'redo': 'redo.png',
  575. 'select': 'select.png',
  576. 'select_node': 'select_node.png',
  577. 'pencil': 'fhpath.png',
  578. 'pen': 'line.png',
  579. 'square': 'square.png',
  580. 'rect': 'rect.png',
  581. 'fh_rect': 'freehand-square.png',
  582. 'circle': 'circle.png',
  583. 'ellipse': 'ellipse.png',
  584. 'fh_ellipse': 'freehand-circle.png',
  585. 'path': 'path.png',
  586. 'text': 'text.png',
  587. 'image': 'image.png',
  588. 'zoom': 'zoom.png',
  589. 'clone': 'clone.png',
  590. 'node_clone': 'node_clone.png',
  591. 'delete': 'delete.png',
  592. 'node_delete': 'node_delete.png',
  593. 'group': 'shape_group_elements.png',
  594. 'ungroup': 'shape_ungroup.png',
  595. 'move_top': 'move_top.png',
  596. 'move_bottom': 'move_bottom.png',
  597. 'to_path': 'to_path.png',
  598. 'link_controls': 'link_controls.png',
  599. 'reorient': 'reorient.png',
  600. 'align_left': 'align-left.png',
  601. 'align_center': 'align-center.png',
  602. 'align_right': 'align-right.png',
  603. 'align_top': 'align-top.png',
  604. 'align_middle': 'align-middle.png',
  605. 'align_bottom': 'align-bottom.png',
  606. 'go_up': 'go-up.png',
  607. 'go_down': 'go-down.png',
  608. 'ok': 'save.png',
  609. 'cancel': 'cancel.png',
  610. 'arrow_right': 'flyouth.png',
  611. 'arrow_down': 'dropdown.gif'
  612. },
  613. placement: {
  614. '#logo': 'logo',
  615. '#tool_clear div,#layer_new': 'new_image',
  616. '#tool_save div': 'save',
  617. '#tool_export div': 'export',
  618. '#tool_open div div': 'open',
  619. '#tool_import div div': 'import',
  620. '#tool_source': 'source',
  621. '#tool_docprops > div': 'docprops',
  622. '#tool_wireframe': 'wireframe',
  623. '#tool_undo': 'undo',
  624. '#tool_redo': 'redo',
  625. '#tool_select': 'select',
  626. '#tool_fhpath': 'pencil',
  627. '#tool_line': 'pen',
  628. '#tool_rect,#tools_rect_show': 'rect',
  629. '#tool_square': 'square',
  630. '#tool_fhrect': 'fh_rect',
  631. '#tool_ellipse,#tools_ellipse_show': 'ellipse',
  632. '#tool_circle': 'circle',
  633. '#tool_fhellipse': 'fh_ellipse',
  634. '#tool_path': 'path',
  635. '#tool_text,#layer_rename': 'text',
  636. '#tool_image': 'image',
  637. '#tool_zoom': 'zoom',
  638. '#tool_clone,#tool_clone_multi': 'clone',
  639. '#tool_node_clone': 'node_clone',
  640. '#layer_delete,#tool_delete,#tool_delete_multi': 'delete',
  641. '#tool_node_delete': 'node_delete',
  642. '#tool_add_subpath': 'add_subpath',
  643. '#tool_openclose_path': 'open_path',
  644. '#tool_move_top': 'move_top',
  645. '#tool_move_bottom': 'move_bottom',
  646. '#tool_topath': 'to_path',
  647. '#tool_node_link': 'link_controls',
  648. '#tool_reorient': 'reorient',
  649. '#tool_group_elements': 'group_elements',
  650. '#tool_ungroup': 'ungroup',
  651. '#tool_unlink_use': 'unlink_use',
  652. '#tool_alignleft, #tool_posleft': 'align_left',
  653. '#tool_aligncenter, #tool_poscenter': 'align_center',
  654. '#tool_alignright, #tool_posright': 'align_right',
  655. '#tool_aligntop, #tool_postop': 'align_top',
  656. '#tool_alignmiddle, #tool_posmiddle': 'align_middle',
  657. '#tool_alignbottom, #tool_posbottom': 'align_bottom',
  658. '#cur_position': 'align',
  659. '#linecap_butt,#cur_linecap': 'linecap_butt',
  660. '#linecap_round': 'linecap_round',
  661. '#linecap_square': 'linecap_square',
  662. '#linejoin_miter,#cur_linejoin': 'linejoin_miter',
  663. '#linejoin_round': 'linejoin_round',
  664. '#linejoin_bevel': 'linejoin_bevel',
  665. '#url_notice': 'warning',
  666. '#layer_up': 'go_up',
  667. '#layer_down': 'go_down',
  668. '#layer_moreopts': 'context_menu',
  669. '#layerlist td.layervis': 'eye',
  670. '#tool_source_save,#tool_docprops_save,#tool_prefs_save': 'ok',
  671. '#tool_source_cancel,#tool_docprops_cancel,#tool_prefs_cancel': 'cancel',
  672. '#rwidthLabel, #iwidthLabel': 'width',
  673. '#rheightLabel, #iheightLabel': 'height',
  674. '#cornerRadiusLabel span': 'c_radius',
  675. '#angleLabel': 'angle',
  676. '#linkLabel,#tool_make_link,#tool_make_link_multi': 'globe_link',
  677. '#zoomLabel': 'zoom',
  678. '#tool_fill label': 'fill',
  679. '#tool_stroke .icon_label': 'stroke',
  680. '#group_opacityLabel': 'opacity',
  681. '#blurLabel': 'blur',
  682. '#font_sizeLabel': 'fontsize',
  683. '.flyout_arrow_horiz': 'arrow_right',
  684. '.dropdown button, #main_button .dropdown': 'arrow_down',
  685. '#palette .palette_item:first, #fill_bg, #stroke_bg': 'no_color'
  686. },
  687. resize: {
  688. '#logo .svg_icon': 28,
  689. '.flyout_arrow_horiz .svg_icon': 5,
  690. '.layer_button .svg_icon, #layerlist td.layervis .svg_icon': 14,
  691. '.dropdown button .svg_icon': 7,
  692. '#main_button .dropdown .svg_icon': 9,
  693. '.palette_item:first .svg_icon' : 15,
  694. '#fill_bg .svg_icon, #stroke_bg .svg_icon': 16,
  695. '.toolbar_button button .svg_icon': 16,
  696. '.stroke_tool div div .svg_icon': 20,
  697. '#tools_bottom label .svg_icon': 18
  698. },
  699. callback: function(icons) {
  700. $('.toolbar_button button > svg, .toolbar_button button > img').each(function() {
  701. $(this).parent().prepend(this);
  702. });
  703. var min_height,
  704. tleft = $('#tools_left');
  705. if (tleft.length !== 0) {
  706. min_height = tleft.offset().top + tleft.outerHeight();
  707. }
  708. var size = $.pref('iconsize');
  709. editor.setIconSize(size || ($(window).height() < min_height ? 's': 'm'));
  710. // Look for any missing flyout icons from plugins
  711. $('.tools_flyout').each(function() {
  712. var shower = $('#' + this.id + '_show');
  713. var sel = shower.attr('data-curopt');
  714. // Check if there's an icon here
  715. if (!shower.children('svg, img').length) {
  716. var clone = $(sel).children().clone();
  717. if (clone.length) {
  718. clone[0].removeAttribute('style'); //Needed for Opera
  719. shower.append(clone);
  720. }
  721. }
  722. });
  723. editor.runCallbacks();
  724. setTimeout(function() {
  725. $('.flyout_arrow_horiz:empty').each(function() {
  726. $(this).append($.getSvgIcon('arrow_right').width(5).height(5));
  727. });
  728. }, 1);
  729. }
  730. });
  731. editor.canvas = svgCanvas = new $.SvgCanvas(document.getElementById('svgcanvas'), curConfig);
  732. var supportsNonSS, resize_timer, changeZoom, Actions, curScrollPos,
  733. palette = [ // Todo: Make into configuration item?
  734. '#000000', '#3f3f3f', '#7f7f7f', '#bfbfbf', '#ffffff',
  735. '#ff0000', '#ff7f00', '#ffff00', '#7fff00',
  736. '#00ff00', '#00ff7f', '#00ffff', '#007fff',
  737. '#0000ff', '#7f00ff', '#ff00ff', '#ff007f',
  738. '#7f0000', '#7f3f00', '#7f7f00', '#3f7f00',
  739. '#007f00', '#007f3f', '#007f7f', '#003f7f',
  740. '#00007f', '#3f007f', '#7f007f', '#7f003f',
  741. '#ffaaaa', '#ffd4aa', '#ffffaa', '#d4ffaa',
  742. '#aaffaa', '#aaffd4', '#aaffff', '#aad4ff',
  743. '#aaaaff', '#d4aaff', '#ffaaff', '#ffaad4'
  744. ],
  745. modKey = (svgedit.browser.isMac() ? 'meta+' : 'ctrl+'), // â&#x152;&#x2DC;
  746. path = svgCanvas.pathActions,
  747. undoMgr = svgCanvas.undoMgr,
  748. defaultImageURL = curConfig.imgPath + 'logo.png',
  749. workarea = $('#workarea'),
  750. canv_menu = $('#cmenu_canvas'),
  751. // layer_menu = $('#cmenu_layers'), // Unused
  752. exportWindow = null,
  753. zoomInIcon = 'crosshair',
  754. zoomOutIcon = 'crosshair',
  755. ui_context = 'toolbars',
  756. origSource = '',
  757. paintBox = {fill: null, stroke:null};
  758. // This sets up alternative dialog boxes. They mostly work the same way as
  759. // their UI counterparts, expect instead of returning the result, a callback
  760. // needs to be included that returns the result as its first parameter.
  761. // In the future we may want to add additional types of dialog boxes, since
  762. // they should be easy to handle this way.
  763. (function() {
  764. $('#dialog_container').draggable({cancel: '#dialog_content, #dialog_buttons *', containment: 'window'});
  765. var box = $('#dialog_box'),
  766. btn_holder = $('#dialog_buttons'),
  767. dialog_content = $('#dialog_content'),
  768. dbox = function(type, msg, callback, defaultVal, opts, changeCb, checkbox) {
  769. var ok, ctrl, chkbx;
  770. dialog_content.html('<p>'+msg.replace(/\n/g, '</p><p>')+'</p>')
  771. .toggleClass('prompt', (type == 'prompt'));
  772. btn_holder.empty();
  773. ok = $('<input type="button" value="' + uiStrings.common.ok + '">').appendTo(btn_holder);
  774. if (type !== 'alert') {
  775. $('<input type="button" value="' + uiStrings.common.cancel + '">')
  776. .appendTo(btn_holder)
  777. .click(function() { box.hide(); callback(false);});
  778. }
  779. if (type === 'prompt') {
  780. ctrl = $('<input type="text">').prependTo(btn_holder);
  781. ctrl.val(defaultVal || '');
  782. ctrl.bind('keydown', 'return', function() {ok.click();});
  783. }
  784. else if (type === 'select') {
  785. var div = $('<div style="text-align:center;">');
  786. ctrl = $('<select>').appendTo(div);
  787. if (checkbox) {
  788. var label = $('<label>').text(checkbox.label);
  789. chkbx = $('<input type="checkbox">').appendTo(label);
  790. chkbx.val(checkbox.value);
  791. if (checkbox.tooltip) {
  792. label.attr('title', checkbox.tooltip);
  793. }
  794. chkbx.prop('checked', !!checkbox.checked);
  795. div.append($('<div>').append(label));
  796. }
  797. $.each(opts || [], function (opt, val) {
  798. if (typeof val === 'object') {
  799. ctrl.append($('<option>').val(val.value).html(val.text));
  800. }
  801. else {
  802. ctrl.append($('<option>').html(val));
  803. }
  804. });
  805. dialog_content.append(div);
  806. if (defaultVal) {
  807. ctrl.val(defaultVal);
  808. }
  809. if (changeCb) {
  810. ctrl.bind('change', 'return', changeCb);
  811. }
  812. ctrl.bind('keydown', 'return', function() {ok.click();});
  813. }
  814. if (type === 'process') {
  815. ok.hide();
  816. }
  817. box.show();
  818. ok.click(function() {
  819. box.hide();
  820. var resp = (type === 'prompt' || type === 'select') ? ctrl.val() : true;
  821. if (callback) {
  822. if (chkbx) {
  823. callback(resp, chkbx.prop('checked'));
  824. }
  825. else {
  826. callback(resp);
  827. }
  828. }
  829. }).focus();
  830. if (type === 'prompt' || type === 'select') {
  831. ctrl.focus();
  832. }
  833. };
  834. $.alert = function(msg, cb) { dbox('alert', msg, cb);};
  835. $.confirm = function(msg, cb) { dbox('confirm', msg, cb);};
  836. $.process_cancel = function(msg, cb) { dbox('process', msg, cb);};
  837. $.prompt = function(msg, txt, cb) { dbox('prompt', msg, cb, txt);};
  838. $.select = function(msg, opts, cb, changeCb, txt, checkbox) { dbox('select', msg, cb, txt, opts, changeCb, checkbox);};
  839. }());
  840. var setSelectMode = function() {
  841. var curr = $('.tool_button_current');
  842. if (curr.length && curr[0].id !== 'tool_select') {
  843. curr.removeClass('tool_button_current').addClass('tool_button');
  844. $('#tool_select').addClass('tool_button_current').removeClass('tool_button');
  845. $('#styleoverrides').text('#svgcanvas svg *{cursor:move;pointer-events:all} #svgcanvas svg{cursor:default}');
  846. }
  847. svgCanvas.setMode('select');
  848. workarea.css('cursor', 'auto');
  849. };
  850. // used to make the flyouts stay on the screen longer the very first time
  851. // var flyoutspeed = 1250; // Currently unused
  852. var textBeingEntered = false;
  853. var selectedElement = null;
  854. var multiselected = false;
  855. var editingsource = false;
  856. var docprops = false;
  857. var preferences = false;
  858. var cur_context = '';
  859. var origTitle = $('title:first').text();
  860. // Make [1,2,5] array
  861. var r_intervals = [];
  862. var i;
  863. for (i = 0.1; i < 1E5; i *= 10) {
  864. r_intervals.push(i);
  865. r_intervals.push(2 * i);
  866. r_intervals.push(5 * i);
  867. }
  868. // This function highlights the layer passed in (by fading out the other layers)
  869. // if no layer is passed in, this function restores the other layers
  870. var toggleHighlightLayer = function(layerNameToHighlight) {
  871. var i, curNames = [], numLayers = svgCanvas.getCurrentDrawing().getNumLayers();
  872. for (i = 0; i < numLayers; i++) {
  873. curNames[i] = svgCanvas.getCurrentDrawing().getLayerName(i);
  874. }
  875. if (layerNameToHighlight) {
  876. for (i = 0; i < numLayers; ++i) {
  877. if (curNames[i] != layerNameToHighlight) {
  878. svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 0.5);
  879. }
  880. }
  881. } else {
  882. for (i = 0; i < numLayers; ++i) {
  883. svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 1.0);
  884. }
  885. }
  886. };
  887. var populateLayers = function() {
  888. svgCanvas.clearSelection();
  889. var layerlist = $('#layerlist tbody').empty();
  890. var selLayerNames = $('#selLayerNames').empty();
  891. var drawing = svgCanvas.getCurrentDrawing();
  892. var currentLayerName = drawing.getCurrentLayerName();
  893. var layer = svgCanvas.getCurrentDrawing().getNumLayers();
  894. var icon = $.getSvgIcon('eye');
  895. // we get the layers in the reverse z-order (the layer rendered on top is listed first)
  896. while (layer--) {
  897. var name = drawing.getLayerName(layer);
  898. var layerTr = $('<tr class="layer">').toggleClass('layersel', name === currentLayerName);
  899. var layerVis = $('<td class="layervis">').toggleClass('layerinvis', !drawing.getLayerVisibility(name));
  900. var layerName = $('<td class="layername">' + name + '</td>');
  901. layerlist.append(layerTr.append(layerVis, layerName));
  902. selLayerNames.append('<option value="' + name + '">' + name + '</option>');
  903. }
  904. if (icon !== undefined) {
  905. var copy = icon.clone();
  906. $('td.layervis', layerlist).append(copy);
  907. $.resizeSvgIcons({'td.layervis .svg_icon': 14});
  908. }
  909. // handle selection of layer
  910. $('#layerlist td.layername')
  911. .mouseup(function(evt) {
  912. $('#layerlist tr.layer').removeClass('layersel');
  913. $(this.parentNode).addClass('layersel');
  914. svgCanvas.setCurrentLayer(this.textContent);
  915. evt.preventDefault();
  916. })
  917. .mouseover(function() {
  918. toggleHighlightLayer(this.textContent);
  919. })
  920. .mouseout(function() {
  921. toggleHighlightLayer();
  922. });
  923. $('#layerlist td.layervis').click(function() {
  924. var row = $(this.parentNode).prevAll().length;
  925. var name = $('#layerlist tr.layer:eq(' + row + ') td.layername').text();
  926. var vis = $(this).hasClass('layerinvis');
  927. svgCanvas.setLayerVisibility(name, vis);
  928. $(this).toggleClass('layerinvis');
  929. });
  930. // if there were too few rows, let's add a few to make it not so lonely
  931. var num = 5 - $('#layerlist tr.layer').size();
  932. while (num-- > 0) {
  933. // FIXME: there must a better way to do this
  934. layerlist.append('<tr><td style="color:white">_</td><td/></tr>');
  935. }
  936. };
  937. var showSourceEditor = function(e, forSaving) {
  938. if (editingsource) {return;}
  939. editingsource = true;
  940. origSource = svgCanvas.getSvgString();
  941. $('#save_output_btns').toggle(!!forSaving);
  942. $('#tool_source_back').toggle(!forSaving);
  943. $('#svg_source_textarea').val(origSource);
  944. $('#svg_source_editor').fadeIn();
  945. $('#svg_source_textarea').focus();
  946. };
  947. var togglePathEditMode = function(editmode, elems) {
  948. $('#path_node_panel').toggle(editmode);
  949. $('#tools_bottom_2,#tools_bottom_3').toggle(!editmode);
  950. if (editmode) {
  951. // Change select icon
  952. $('.tool_button_current').removeClass('tool_button_current').addClass('tool_button');
  953. $('#tool_select').addClass('tool_button_current').removeClass('tool_button');
  954. setIcon('#tool_select', 'select_node');
  955. multiselected = false;
  956. if (elems.length) {
  957. selectedElement = elems[0];
  958. }
  959. } else {
  960. setTimeout(function () {
  961. setIcon('#tool_select', 'select');
  962. }, 1000);
  963. }
  964. };
  965. var saveHandler = function(wind, svg) {
  966. editor.showSaveWarning = false;
  967. // by default, we add the XML prolog back, systems integrating SVG-edit (wikis, CMSs)
  968. // can just provide their own custom save handler and might not want the XML prolog
  969. svg = '<?xml version="1.0"?>\n' + svg;
  970. // IE9 doesn't allow standalone Data URLs
  971. // https://connect.microsoft.com/IE/feedback/details/542600/data-uri-images-fail-when-loaded-by-themselves
  972. if (svgedit.browser.isIE()) {
  973. showSourceEditor(0, true);
  974. return;
  975. }
  976. // Opens the SVG in new window
  977. var win = wind.open('data:image/svg+xml;base64,' + Utils.encode64(svg));
  978. // Alert will only appear the first time saved OR the first time the bug is encountered
  979. var done = $.pref('save_notice_done');
  980. if (done !== 'all') {
  981. var note = uiStrings.notification.saveFromBrowser.replace('%s', 'SVG');
  982. // Check if FF and has <defs/>
  983. if (svgedit.browser.isGecko()) {
  984. if (svg.indexOf('<defs') !== -1) {
  985. // warning about Mozilla bug #308590 when applicable (seems to be fixed now in Feb 2013)
  986. note += '\n\n' + uiStrings.notification.defsFailOnSave;
  987. $.pref('save_notice_done', 'all');
  988. done = 'all';
  989. } else {
  990. $.pref('save_notice_done', 'part');
  991. }
  992. } else {
  993. $.pref('save_notice_done', 'all');
  994. }
  995. if (done !== 'part') {
  996. win.alert(note);
  997. }
  998. }
  999. };
  1000. // Export global for use by jsPDF
  1001. saveAs = function (blob, options) {
  1002. var blobUrl = URL.createObjectURL(blob);
  1003. try {
  1004. // This creates a bookmarkable data URL,
  1005. // but it doesn't currently work in
  1006. // Firefox, and although it works in Chrome,
  1007. // Chrome doesn't make the full "data:" URL
  1008. // visible unless you right-click to "Inspect
  1009. // element" and then right-click on the element
  1010. // to "Copy link address".
  1011. var xhr = new XMLHttpRequest();
  1012. xhr.responseType = 'blob';
  1013. xhr.onload = function() {
  1014. var recoveredBlob = xhr.response;
  1015. var reader = new FileReader();
  1016. reader.onload = function() {
  1017. var blobAsDataUrl = reader.result;
  1018. exportWindow.location.href = blobAsDataUrl;
  1019. };
  1020. reader.readAsDataURL(recoveredBlob);
  1021. };
  1022. xhr.open('GET', blobUrl);
  1023. xhr.send();
  1024. }
  1025. catch (e) {
  1026. exportWindow.location.href = blobUrl;
  1027. }
  1028. };
  1029. var exportHandler = function(win, data) {
  1030. var issues = data.issues,
  1031. type = data.type || 'PNG',
  1032. dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
  1033. exportWindow = window.open('', data.exportWindowName); // A hack to get the window via JSON-able name without opening a new one
  1034. if (!$('#export_canvas').length) {
  1035. $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
  1036. }
  1037. var c = $('#export_canvas')[0];
  1038. if (type === 'PDF') {
  1039. var res = svgCanvas.getResolution();
  1040. var orientation = res.w > res.h ? 'landscape' : 'portrait';
  1041. var units = 'pt'; // curConfig.baseUnit; // We could use baseUnit, but that is presumably not intended for export purposes
  1042. var doc = new jsPDF(orientation, units, [res.w, res.h]); // Todo: Give options to use predefined jsPDF formats like "a4", etc. from pull-down (with option to keep customizable)
  1043. var docTitle = svgCanvas.getDocumentTitle();
  1044. doc.setProperties({
  1045. title: docTitle/*,
  1046. subject: '',
  1047. author: '',
  1048. keywords: '',
  1049. creator: ''*/
  1050. });
  1051. svgElementToPdf(data.svg, doc, {});
  1052. doc.save(docTitle + '.pdf');
  1053. return;
  1054. }
  1055. c.width = svgCanvas.contentW;
  1056. c.height = svgCanvas.contentH;
  1057. canvg(c, data.svg, {renderCallback: function() {
  1058. var datauri = data.quality ? c.toDataURL('image/' + dataURLType, data.quality) : c.toDataURL('image/' + dataURLType);
  1059. exportWindow.location.href = datauri;
  1060. var done = $.pref('export_notice_done');
  1061. if (done !== 'all') {
  1062. var note = uiStrings.notification.saveFromBrowser.replace('%s', type);
  1063. // Check if there's issues
  1064. if (issues.length) {
  1065. var pre = '\n \u2022 ';
  1066. note += ('\n\n' + uiStrings.notification.noteTheseIssues + pre + issues.join(pre));
  1067. }
  1068. // Note that this will also prevent the notice even though new issues may appear later.
  1069. // May want to find a way to deal with that without annoying the user
  1070. $.pref('export_notice_done', 'all');
  1071. exportWindow.alert(note);
  1072. }
  1073. }});
  1074. };
  1075. var operaRepaint = function() {
  1076. // Repaints canvas in Opera. Needed for stroke-dasharray change as well as fill change
  1077. if (!window.opera) {
  1078. return;
  1079. }
  1080. $('<p/>').hide().appendTo('body').remove();
  1081. };
  1082. function setStrokeOpt(opt, changeElem) {
  1083. var id = opt.id;
  1084. var bits = id.split('_');
  1085. var pre = bits[0];
  1086. var val = bits[1];
  1087. if (changeElem) {
  1088. svgCanvas.setStrokeAttr('stroke-' + pre, val);
  1089. }
  1090. operaRepaint();
  1091. setIcon('#cur_' + pre, id, 20);
  1092. $(opt).addClass('current').siblings().removeClass('current');
  1093. }
  1094. // This is a common function used when a tool has been clicked (chosen)
  1095. // It does several common things:
  1096. // - removes the tool_button_current class from whatever tool currently has it
  1097. // - hides any flyouts
  1098. // - adds the tool_button_current class to the button passed in
  1099. var toolButtonClick = editor.toolButtonClick = function(button, noHiding) {
  1100. if ($(button).hasClass('disabled')) {return false;}
  1101. if ($(button).parent().hasClass('tools_flyout')) {return true;}
  1102. var fadeFlyouts = 'normal';
  1103. if (!noHiding) {
  1104. $('.tools_flyout').fadeOut(fadeFlyouts);
  1105. }
  1106. $('#styleoverrides').text('');
  1107. workarea.css('cursor', 'auto');
  1108. $('.tool_button_current').removeClass('tool_button_current').addClass('tool_button');
  1109. $(button).addClass('tool_button_current').removeClass('tool_button');
  1110. return true;
  1111. };
  1112. var clickSelect = editor.clickSelect = function() {
  1113. if (toolButtonClick('#tool_select')) {
  1114. svgCanvas.setMode('select');
  1115. $('#styleoverrides').text('#svgcanvas svg *{cursor:move;pointer-events:all}, #svgcanvas svg{cursor:default}');
  1116. }
  1117. };
  1118. var setImageURL = editor.setImageURL = function(url) {
  1119. if (!url) {
  1120. url = defaultImageURL;
  1121. }
  1122. svgCanvas.setImageURL(url);
  1123. $('#image_url').val(url);
  1124. if (url.indexOf('data:') === 0) {
  1125. // data URI found
  1126. $('#image_url').hide();
  1127. $('#change_image_url').show();
  1128. } else {
  1129. // regular URL
  1130. svgCanvas.embedImage(url, function(dataURI) {
  1131. // Couldn't embed, so show warning
  1132. $('#url_notice').toggle(!dataURI);
  1133. defaultImageURL = url;
  1134. });
  1135. $('#image_url').show();
  1136. $('#change_image_url').hide();
  1137. }
  1138. };
  1139. function setBackground (color, url) {
  1140. // if (color == $.pref('bkgd_color') && url == $.pref('bkgd_url')) {return;}
  1141. $.pref('bkgd_color', color);
  1142. $.pref('bkgd_url', url);
  1143. // This should be done in svgcanvas.js for the borderRect fill
  1144. svgCanvas.setBackground(color, url);
  1145. }
  1146. function promptImgURL() {
  1147. var curhref = svgCanvas.getHref(selectedElement);
  1148. curhref = curhref.indexOf('data:') === 0 ? '' : curhref;
  1149. $.prompt(uiStrings.notification.enterNewImgURL, curhref, function(url) {
  1150. if (url) {setImageURL(url);}
  1151. });
  1152. }
  1153. var setInputWidth = function(elem) {
  1154. var w = Math.min(Math.max(12 + elem.value.length * 6, 50), 300);
  1155. $(elem).width(w);
  1156. };
  1157. function updateRulers(scanvas, zoom) {
  1158. if (!zoom) {zoom = svgCanvas.getZoom();}
  1159. if (!scanvas) {scanvas = $('#svgcanvas');}
  1160. var d, i;
  1161. var limit = 30000;
  1162. var contentElem = svgCanvas.getContentElem();
  1163. var units = svgedit.units.getTypeMap();
  1164. var unit = units[curConfig.baseUnit]; // 1 = 1px
  1165. // draw x ruler then y ruler
  1166. for (d = 0; d < 2; d++) {
  1167. var isX = (d === 0);
  1168. var dim = isX ? 'x' : 'y';
  1169. var lentype = isX ? 'width' : 'height';
  1170. var contentDim = Number(contentElem.getAttribute(dim));
  1171. var $hcanv_orig = $('#ruler_' + dim + ' canvas:first');
  1172. // Bit of a hack to fully clear the canvas in Safari & IE9
  1173. var $hcanv = $hcanv_orig.clone();
  1174. $hcanv_orig.replaceWith($hcanv);
  1175. var hcanv = $hcanv[0];
  1176. // Set the canvas size to the width of the container
  1177. var ruler_len = scanvas[lentype]();
  1178. var total_len = ruler_len;
  1179. hcanv.parentNode.style[lentype] = total_len + 'px';
  1180. var ctx_num = 0;
  1181. var ctx = hcanv.getContext('2d');
  1182. var ctx_arr, num, ctx_arr_num;
  1183. ctx.fillStyle = 'rgb(200,0,0)';
  1184. ctx.fillRect(0, 0, hcanv.width, hcanv.height);
  1185. // Remove any existing canvasses
  1186. $hcanv.siblings().remove();
  1187. // Create multiple canvases when necessary (due to browser limits)
  1188. if (ruler_len >= limit) {
  1189. ctx_arr_num = parseInt(ruler_len / limit, 10) + 1;
  1190. ctx_arr = [];
  1191. ctx_arr[0] = ctx;
  1192. var copy;
  1193. for (i = 1; i < ctx_arr_num; i++) {
  1194. hcanv[lentype] = limit;
  1195. copy = hcanv.cloneNode(true);
  1196. hcanv.parentNode.appendChild(copy);
  1197. ctx_arr[i] = copy.getContext('2d');
  1198. }
  1199. copy[lentype] = ruler_len % limit;
  1200. // set copy width to last
  1201. ruler_len = limit;
  1202. }
  1203. hcanv[lentype] = ruler_len;
  1204. var u_multi = unit * zoom;
  1205. // Calculate the main number interval
  1206. var raw_m = 50 / u_multi;
  1207. var multi = 1;
  1208. for (i = 0; i < r_intervals.length; i++) {
  1209. num = r_intervals[i];
  1210. multi = num;
  1211. if (raw_m <= num) {
  1212. break;
  1213. }
  1214. }
  1215. var big_int = multi * u_multi;
  1216. ctx.font = '9px sans-serif';
  1217. var ruler_d = ((contentDim / u_multi) % multi) * u_multi;
  1218. var label_pos = ruler_d - big_int;
  1219. // draw big intervals
  1220. while (ruler_d < total_len) {
  1221. label_pos += big_int;
  1222. // var real_d = ruler_d - contentDim; // Currently unused
  1223. var cur_d = Math.round(ruler_d) + 0.5;
  1224. if (isX) {
  1225. ctx.moveTo(cur_d, 15);
  1226. ctx.lineTo(cur_d, 0);
  1227. }
  1228. else {
  1229. ctx.moveTo(15, cur_d);
  1230. ctx.lineTo(0, cur_d);
  1231. }
  1232. num = (label_pos - contentDim) / u_multi;
  1233. var label;
  1234. if (multi >= 1) {
  1235. label = Math.round(num);
  1236. }
  1237. else {
  1238. var decs = String(multi).split('.')[1].length;
  1239. label = num.toFixed(decs);
  1240. }
  1241. // Change 1000s to Ks
  1242. if (label !== 0 && label !== 1000 && label % 1000 === 0) {
  1243. label = (label / 1000) + 'K';
  1244. }
  1245. if (isX) {
  1246. ctx.fillText(label, ruler_d+2, 8);
  1247. } else {
  1248. // draw label vertically
  1249. var str = String(label).split('');
  1250. for (i = 0; i < str.length; i++) {
  1251. ctx.fillText(str[i], 1, (ruler_d+9) + i*9);
  1252. }
  1253. }
  1254. var part = big_int / 10;
  1255. // draw the small intervals
  1256. for (i = 1; i < 10; i++) {
  1257. var sub_d = Math.round(ruler_d + part * i) + 0.5;
  1258. if (ctx_arr && sub_d > ruler_len) {
  1259. ctx_num++;
  1260. ctx.stroke();
  1261. if (ctx_num >= ctx_arr_num) {
  1262. i = 10;
  1263. ruler_d = total_len;
  1264. continue;
  1265. }
  1266. ctx = ctx_arr[ctx_num];
  1267. ruler_d -= limit;
  1268. sub_d = Math.round(ruler_d + part * i) + 0.5;
  1269. }
  1270. // odd lines are slighly longer
  1271. var line_num = (i % 2) ? 12 : 10;
  1272. if (isX) {
  1273. ctx.moveTo(sub_d, 15);
  1274. ctx.lineTo(sub_d, line_num);
  1275. } else {
  1276. ctx.moveTo(15, sub_d);
  1277. ctx.lineTo(line_num, sub_d);
  1278. }
  1279. }
  1280. ruler_d += big_int;
  1281. }
  1282. ctx.strokeStyle = '#000';
  1283. ctx.stroke();
  1284. }
  1285. }
  1286. var updateCanvas = editor.updateCanvas = function(center, new_ctr) {
  1287. var w = workarea.width(), h = workarea.height();
  1288. var w_orig = w, h_orig = h;
  1289. var zoom = svgCanvas.getZoom();
  1290. var w_area = workarea;
  1291. var cnvs = $('#svgcanvas');
  1292. var old_ctr = {
  1293. x: w_area[0].scrollLeft + w_orig/2,
  1294. y: w_area[0].scrollTop + h_orig/2
  1295. };
  1296. var multi = curConfig.canvas_expansion;
  1297. w = Math.max(w_orig, svgCanvas.contentW * zoom * multi);
  1298. h = Math.max(h_orig, svgCanvas.contentH * zoom * multi);
  1299. if (w == w_orig && h == h_orig) {
  1300. workarea.css('overflow', 'hidden');
  1301. } else {
  1302. workarea.css('overflow', 'scroll');
  1303. }
  1304. var old_can_y = cnvs.height()/2;
  1305. var old_can_x = cnvs.width()/2;
  1306. cnvs.width(w).height(h);
  1307. var new_can_y = h/2;
  1308. var new_can_x = w/2;
  1309. var offset = svgCanvas.updateCanvas(w, h);
  1310. var ratio = new_can_x / old_can_x;
  1311. var scroll_x = w/2 - w_orig/2;
  1312. var scroll_y = h/2 - h_orig/2;
  1313. if (!new_ctr) {
  1314. var old_dist_x = old_ctr.x - old_can_x;
  1315. var new_x = new_can_x + old_dist_x * ratio;
  1316. var old_dist_y = old_ctr.y - old_can_y;
  1317. var new_y = new_can_y + old_dist_y * ratio;
  1318. new_ctr = {
  1319. x: new_x,
  1320. y: new_y
  1321. };
  1322. } else {
  1323. new_ctr.x += offset.x;
  1324. new_ctr.y += offset.y;
  1325. }
  1326. if (center) {
  1327. // Go to top-left for larger documents
  1328. if (svgCanvas.contentW > w_area.width()) {
  1329. // Top-left
  1330. workarea[0].scrollLeft = offset.x - 10;
  1331. workarea[0].scrollTop = offset.y - 10;
  1332. } else {
  1333. // Center
  1334. w_area[0].scrollLeft = scroll_x;
  1335. w_area[0].scrollTop = scroll_y;
  1336. }
  1337. } else {
  1338. w_area[0].scrollLeft = new_ctr.x - w_orig/2;
  1339. w_area[0].scrollTop = new_ctr.y - h_orig/2;
  1340. }
  1341. if (curConfig.showRulers) {
  1342. updateRulers(cnvs, zoom);
  1343. workarea.scroll();
  1344. }
  1345. if (urldata.storagePrompt !== true && !editor.storagePromptClosed) {
  1346. $('#dialog_box').hide();
  1347. }
  1348. };
  1349. var updateToolButtonState = function() {
  1350. var index, button;
  1351. var bNoFill = (svgCanvas.getColor('fill') == 'none');
  1352. var bNoStroke = (svgCanvas.getColor('stroke') == 'none');
  1353. var buttonsNeedingStroke = [ '#tool_fhpath', '#tool_line' ];
  1354. var buttonsNeedingFillAndStroke = [ '#tools_rect .tool_button', '#tools_ellipse .tool_button', '#tool_text', '#tool_path'];
  1355. if (bNoStroke) {
  1356. for (index in buttonsNeedingStroke) {
  1357. button = buttonsNeedingStroke[index];
  1358. if ($(button).hasClass('tool_button_current')) {
  1359. clickSelect();
  1360. }
  1361. $(button).addClass('disabled');
  1362. }
  1363. } else {
  1364. for (index in buttonsNeedingStroke) {
  1365. button = buttonsNeedingStroke[index];
  1366. $(button).removeClass('disabled');
  1367. }
  1368. }
  1369. if (bNoStroke && bNoFill) {
  1370. for (index in buttonsNeedingFillAndStroke) {
  1371. button = buttonsNeedingFillAndStroke[index];
  1372. if ($(button).hasClass('tool_button_current')) {
  1373. clickSelect();
  1374. }
  1375. $(button).addClass('disabled');
  1376. }
  1377. } else {
  1378. for (index in buttonsNeedingFillAndStroke) {
  1379. button = buttonsNeedingFillAndStroke[index];
  1380. $(button).removeClass('disabled');
  1381. }
  1382. }
  1383. svgCanvas.runExtensions('toolButtonStateUpdate', {
  1384. nofill: bNoFill,
  1385. nostroke: bNoStroke
  1386. });
  1387. // Disable flyouts if all inside are disabled
  1388. $('.tools_flyout').each(function() {
  1389. var shower = $('#' + this.id + '_show');
  1390. var has_enabled = false;
  1391. $(this).children().each(function() {
  1392. if (!$(this).hasClass('disabled')) {
  1393. has_enabled = true;
  1394. }
  1395. });
  1396. shower.toggleClass('disabled', !has_enabled);
  1397. });
  1398. operaRepaint();
  1399. };
  1400. // Updates the toolbar (colors, opacity, etc) based on the selected element
  1401. // This function also updates the opacity and id elements that are in the context panel
  1402. var updateToolbar = function() {
  1403. var i, len;
  1404. if (selectedElement != null) {
  1405. switch (selectedElement.tagName) {
  1406. case 'use':
  1407. case 'image':
  1408. case 'foreignObject':
  1409. break;
  1410. case 'g':
  1411. case 'a':
  1412. // Look for common styles
  1413. var gWidth = null;
  1414. var childs = selectedElement.getElementsByTagName('*');
  1415. for (i = 0, len = childs.length; i < len; i++) {
  1416. var swidth = childs[i].getAttribute('stroke-width');
  1417. if (i === 0) {
  1418. gWidth = swidth;
  1419. } else if (gWidth !== swidth) {
  1420. gWidth = null;
  1421. }
  1422. }
  1423. $('#stroke_width').val(gWidth === null ? '' : gWidth);
  1424. paintBox.fill.update(true);
  1425. paintBox.stroke.update(true);
  1426. break;
  1427. default:
  1428. paintBox.fill.update(true);
  1429. paintBox.stroke.update(true);
  1430. $('#stroke_width').val(selectedElement.getAttribute('stroke-width') || 1);
  1431. $('#stroke_style').val(selectedElement.getAttribute('stroke-dasharray') || 'none');
  1432. var attr = selectedElement.getAttribute('stroke-linejoin') || 'miter';
  1433. if ($('#linejoin_' + attr).length != 0) {
  1434. setStrokeOpt($('#linejoin_' + attr)[0]);
  1435. }
  1436. attr = selectedElement.getAttribute('stroke-linecap') || 'butt';
  1437. if ($('#linecap_' + attr).length != 0) {
  1438. setStrokeOpt($('#linecap_' + attr)[0]);
  1439. }
  1440. }
  1441. }
  1442. // All elements including image and group have opacity
  1443. if (selectedElement != null) {
  1444. var opac_perc = ((selectedElement.getAttribute('opacity')||1.0)*100);
  1445. $('#group_opacity').val(opac_perc);
  1446. $('#opac_slider').slider('option', 'value', opac_perc);
  1447. $('#elem_id').val(selectedElement.id);
  1448. }
  1449. updateToolButtonState();
  1450. };
  1451. // updates the context panel tools based on the selected element
  1452. var updateContextPanel = function() {
  1453. var elem = selectedElement;
  1454. // If element has just been deleted, consider it null
  1455. if (elem != null && !elem.parentNode) {elem = null;}
  1456. var currentLayerName = svgCanvas.getCurrentDrawing().getCurrentLayerName();
  1457. var currentMode = svgCanvas.getMode();
  1458. var unit = curConfig.baseUnit !== 'px' ? curConfig.baseUnit : null;
  1459. var is_node = currentMode == 'pathedit'; //elem ? (elem.id && elem.id.indexOf('pathpointgrip') == 0) : false;
  1460. var menu_items = $('#cmenu_canvas li');
  1461. $('#selected_panel, #multiselected_panel, #g_panel, #rect_panel, #circle_panel,'+
  1462. '#ellipse_panel, #line_panel, #text_panel, #image_panel, #container_panel,'+
  1463. ' #use_panel, #a_panel').hide();
  1464. if (elem != null) {
  1465. var elname = elem.nodeName;
  1466. // If this is a link with no transform and one child, pretend
  1467. // its child is selected
  1468. // if (elname === 'a') { // && !$(elem).attr('transform')) {
  1469. // elem = elem.firstChild;
  1470. // }
  1471. var angle = svgCanvas.getRotationAngle(elem);
  1472. $('#angle').val(angle);
  1473. var blurval = svgCanvas.getBlur(elem);
  1474. $('#blur').val(blurval);
  1475. $('#blur_slider').slider('option', 'value', blurval);
  1476. if (svgCanvas.addedNew) {
  1477. if (elname === 'image') {
  1478. // Prompt for URL if not a data URL
  1479. if (svgCanvas.getHref(elem).indexOf('data:') !== 0) {
  1480. promptImgURL();
  1481. }
  1482. } /*else if (elname == 'text') {
  1483. // TODO: Do something here for new text
  1484. }*/
  1485. }
  1486. if (!is_node && currentMode != 'pathedit') {
  1487. $('#selected_panel').show();
  1488. // Elements in this array already have coord fields
  1489. if (['line', 'circle', 'ellipse'].indexOf(elname) >= 0) {
  1490. $('#xy_panel').hide();
  1491. } else {
  1492. var x, y;
  1493. // Get BBox vals for g, polyline and path
  1494. if (['g', 'polyline', 'path'].indexOf(elname) >= 0) {
  1495. var bb = svgCanvas.getStrokedBBox([elem]);
  1496. if (bb) {
  1497. x = bb.x;
  1498. y = bb.y;
  1499. }
  1500. } else {
  1501. x = elem.getAttribute('x');
  1502. y = elem.getAttribute('y');
  1503. }
  1504. if (unit) {
  1505. x = svgedit.units.convertUnit(x);
  1506. y = svgedit.units.convertUnit(y);
  1507. }
  1508. $('#selected_x').val(x || 0);
  1509. $('#selected_y').val(y || 0);
  1510. $('#xy_panel').show();
  1511. }
  1512. // Elements in this array cannot be converted to a path
  1513. var no_path = ['image', 'text', 'path', 'g', 'use'].indexOf(elname) == -1;
  1514. $('#tool_topath').toggle(no_path);
  1515. $('#tool_reorient').toggle(elname === 'path');
  1516. $('#tool_reorient').toggleClass('disabled', angle === 0);
  1517. } else {
  1518. var point = path.getNodePoint();
  1519. $('#tool_add_subpath').removeClass('push_button_pressed').addClass('tool_button');
  1520. $('#tool_node_delete').toggleClass('disabled', !path.canDeleteNodes);
  1521. // Show open/close button based on selected point
  1522. setIcon('#tool_openclose_path', path.closed_subpath ? 'open_path' : 'close_path');
  1523. if (point) {
  1524. var seg_type = $('#seg_type');
  1525. if (unit) {
  1526. point.x = svgedit.units.convertUnit(point.x);
  1527. point.y = svgedit.units.convertUnit(point.y);
  1528. }
  1529. $('#path_node_x').val(point.x);
  1530. $('#path_node_y').val(point.y);
  1531. if (point.type) {
  1532. seg_type.val(point.type).removeAttr('disabled');
  1533. } else {
  1534. seg_type.val(4).attr('disabled', 'disabled');
  1535. }
  1536. }
  1537. return;
  1538. }
  1539. // update contextual tools here
  1540. var panels = {
  1541. g: [],
  1542. a: [],
  1543. rect: ['rx', 'width', 'height'],
  1544. image: ['width', 'height'],
  1545. circle: ['cx', 'cy', 'r'],
  1546. ellipse: ['cx', 'cy', 'rx', 'ry'],
  1547. line: ['x1', 'y1', 'x2', 'y2'],
  1548. text: [],
  1549. use: []
  1550. };
  1551. var el_name = elem.tagName;
  1552. // if ($(elem).data('gsvg')) {
  1553. // $('#g_panel').show();
  1554. // }
  1555. var link_href = null;
  1556. if (el_name === 'a') {
  1557. link_href = svgCanvas.getHref(elem);
  1558. $('#g_panel').show();
  1559. }
  1560. if (elem.parentNode.tagName === 'a') {
  1561. if (!$(elem).siblings().length) {
  1562. $('#a_panel').show();
  1563. link_href = svgCanvas.getHref(elem.parentNode);
  1564. }
  1565. }
  1566. // Hide/show the make_link buttons
  1567. $('#tool_make_link, #tool_make_link').toggle(!link_href);
  1568. if (link_href) {
  1569. $('#link_url').val(link_href);
  1570. }
  1571. if (panels[el_name]) {
  1572. var cur_panel = panels[el_name];
  1573. $('#' + el_name + '_panel').show();
  1574. $.each(cur_panel, function(i, item) {
  1575. var attrVal = elem.getAttribute(item);
  1576. if (curConfig.baseUnit !== 'px' && elem[item]) {
  1577. var bv = elem[item].baseVal.value;
  1578. attrVal = svgedit.units.convertUnit(bv);
  1579. }
  1580. $('#' + el_name + '_' + item).val(attrVal || 0);
  1581. });
  1582. if (el_name == 'text') {
  1583. $('#text_panel').css('display', 'inline');
  1584. if (svgCanvas.getItalic()) {
  1585. $('#tool_italic').addClass('push_button_pressed').removeClass('tool_button');
  1586. } else {
  1587. $('#tool_italic').removeClass('push_button_pressed').addClass('tool_button');
  1588. }
  1589. if (svgCanvas.getBold()) {
  1590. $('#tool_bold').addClass('push_button_pressed').removeClass('tool_button');
  1591. } else {
  1592. $('#tool_bold').removeClass('push_button_pressed').addClass('tool_button');
  1593. }
  1594. $('#font_family').val(elem.getAttribute('font-family'));
  1595. $('#font_size').val(elem.getAttribute('font-size'));
  1596. $('#text').val(elem.textContent);
  1597. if (svgCanvas.addedNew) {
  1598. // Timeout needed for IE9
  1599. setTimeout(function() {
  1600. $('#text').focus().select();
  1601. }, 100);
  1602. }
  1603. } // text
  1604. else if (el_name == 'image') {
  1605. setImageURL(svgCanvas.getHref(elem));
  1606. } // image
  1607. else if (el_name === 'g' || el_name === 'use') {
  1608. $('#container_panel').show();
  1609. var title = svgCanvas.getTitle();
  1610. var label = $('#g_title')[0];
  1611. label.value = title;
  1612. setInputWidth(label);
  1613. $('#g_title').prop('disabled', el_name == 'use');
  1614. }
  1615. }
  1616. menu_items[(el_name === 'g' ? 'en' : 'dis') + 'ableContextMenuItems']('#ungroup');
  1617. menu_items[((el_name === 'g' || !multiselected) ? 'dis' : 'en') + 'ableContextMenuItems']('#group');
  1618. } // if (elem != null)
  1619. else if (multiselected) {
  1620. $('#multiselected_panel').show();
  1621. menu_items
  1622. .enableContextMenuItems('#group')
  1623. .disableContextMenuItems('#ungroup');
  1624. } else {
  1625. menu_items.disableContextMenuItems('#delete,#cut,#copy,#group,#ungroup,#move_front,#move_up,#move_down,#move_back');
  1626. }
  1627. // update history buttons
  1628. $('#tool_undo').toggleClass('disabled', undoMgr.getUndoStackSize() === 0);
  1629. $('#tool_redo').toggleClass('disabled', undoMgr.getRedoStackSize() === 0);
  1630. svgCanvas.addedNew = false;
  1631. if ( (elem && !is_node) || multiselected) {
  1632. // update the selected elements' layer
  1633. $('#selLayerNames').removeAttr('disabled').val(currentLayerName);
  1634. // Enable regular menu options
  1635. canv_menu.enableContextMenuItems('#delete,#cut,#copy,#move_front,#move_up,#move_down,#move_back');
  1636. } else {
  1637. $('#selLayerNames').attr('disabled', 'disabled');
  1638. }
  1639. };
  1640. var updateWireFrame = function() {
  1641. // Test support
  1642. if (supportsNonSS) {return;}
  1643. var rule = '#workarea.wireframe #svgcontent * { stroke-width: ' + 1/svgCanvas.getZoom() + 'px; }';
  1644. $('#wireframe_rules').text(workarea.hasClass('wireframe') ? rule : '');
  1645. };
  1646. var updateTitle = function(title) {
  1647. title = title || svgCanvas.getDocumentTitle();
  1648. var newTitle = origTitle + (title ? ': ' + title : '');
  1649. // Remove title update with current context info, isn't really necessary
  1650. // if (cur_context) {
  1651. // new_title = new_title + cur_context;
  1652. // }
  1653. $('title:first').text(newTitle);
  1654. };
  1655. // called when we've selected a different element
  1656. var selectedChanged = function(win, elems) {
  1657. var mode = svgCanvas.getMode();
  1658. if (mode === 'select') {
  1659. setSelectMode();
  1660. }
  1661. var is_node = (mode == "pathedit");
  1662. // if elems[1] is present, then we have more than one element
  1663. selectedElement = (elems.length === 1 || elems[1] == null ? elems[0] : null);
  1664. multiselected = (elems.length >= 2 && elems[1] != null);
  1665. if (selectedElement != null) {
  1666. // unless we're already in always set the mode of the editor to select because
  1667. // upon creation of a text element the editor is switched into
  1668. // select mode and this event fires - we need our UI to be in sync
  1669. if (!is_node) {
  1670. updateToolbar();
  1671. }
  1672. } // if (elem != null)
  1673. // Deal with pathedit mode
  1674. togglePathEditMode(is_node, elems);
  1675. updateContextPanel();
  1676. svgCanvas.runExtensions('selectedChanged', {
  1677. elems: elems,
  1678. selectedElement: selectedElement,
  1679. multiselected: multiselected
  1680. });
  1681. };
  1682. // Call when part of element is in process of changing, generally
  1683. // on mousemove actions like rotate, move, etc.
  1684. var elementTransition = function(win, elems) {
  1685. var mode = svgCanvas.getMode();
  1686. var elem = elems[0];
  1687. if (!elem) {
  1688. return;
  1689. }
  1690. multiselected = (elems.length >= 2 && elems[1] != null);
  1691. // Only updating fields for single elements for now
  1692. if (!multiselected) {
  1693. switch (mode) {
  1694. case 'rotate':
  1695. var ang = svgCanvas.getRotationAngle(elem);
  1696. $('#angle').val(ang);
  1697. $('#tool_reorient').toggleClass('disabled', ang === 0);
  1698. break;
  1699. // TODO: Update values that change on move/resize, etc
  1700. // case "select":
  1701. // case "resize":
  1702. // break;
  1703. }
  1704. }
  1705. svgCanvas.runExtensions('elementTransition', {
  1706. elems: elems
  1707. });
  1708. };
  1709. // called when any element has changed
  1710. var elementChanged = function(win, elems) {
  1711. var i,
  1712. mode = svgCanvas.getMode();
  1713. if (mode === 'select') {
  1714. setSelectMode();
  1715. }
  1716. for (i = 0; i < elems.length; ++i) {
  1717. var elem = elems[i];
  1718. // if the element changed was the svg, then it could be a resolution change
  1719. if (elem && elem.tagName === 'svg') {
  1720. populateLayers();
  1721. updateCanvas();
  1722. }
  1723. // Update selectedElement if element is no longer part of the image.
  1724. // This occurs for the text elements in Firefox
  1725. else if (elem && selectedElement && selectedElement.parentNode == null) {
  1726. // || elem && elem.tagName == "path" && !multiselected) { // This was added in r1430, but not sure why
  1727. selectedElement = elem;
  1728. }
  1729. }
  1730. editor.showSaveWarning = true;
  1731. // we update the contextual panel with potentially new
  1732. // positional/sizing information (we DON'T want to update the
  1733. // toolbar here as that creates an infinite loop)
  1734. // also this updates the history buttons
  1735. // we tell it to skip focusing the text control if the
  1736. // text element was previously in focus
  1737. updateContextPanel();
  1738. // In the event a gradient was flipped:
  1739. if (selectedElement && mode === 'select') {
  1740. paintBox.fill.update();
  1741. paintBox.stroke.update();
  1742. }
  1743. svgCanvas.runExtensions('elementChanged', {
  1744. elems: elems
  1745. });
  1746. };
  1747. var zoomDone = function() {
  1748. updateWireFrame();
  1749. // updateCanvas(); // necessary?
  1750. };
  1751. var zoomChanged = svgCanvas.zoomChanged = function(win, bbox, autoCenter) {
  1752. var scrbar = 15,
  1753. // res = svgCanvas.getResolution(), // Currently unused
  1754. w_area = workarea;
  1755. // var canvas_pos = $('#svgcanvas').position(); // Currently unused
  1756. var z_info = svgCanvas.setBBoxZoom(bbox, w_area.width()-scrbar, w_area.height()-scrbar);
  1757. if (!z_info) {return;}
  1758. var zoomlevel = z_info.zoom,
  1759. bb = z_info.bbox;
  1760. if (zoomlevel < 0.001) {
  1761. changeZoom({value: 0.1});
  1762. return;
  1763. }
  1764. $('#zoom').val((zoomlevel*100).toFixed(1));
  1765. if (autoCenter) {
  1766. updateCanvas();
  1767. } else {
  1768. updateCanvas(false, {x: bb.x * zoomlevel + (bb.width * zoomlevel)/2, y: bb.y * zoomlevel + (bb.height * zoomlevel)/2});
  1769. }
  1770. if (svgCanvas.getMode() == 'zoom' && bb.width) {
  1771. // Go to select if a zoom box was drawn
  1772. setSelectMode();
  1773. }
  1774. zoomDone();
  1775. };
  1776. changeZoom = function(ctl) {
  1777. var zoomlevel = ctl.value / 100;
  1778. if (zoomlevel < 0.001) {
  1779. ctl.value = 0.1;
  1780. return;
  1781. }
  1782. var zoom = svgCanvas.getZoom();
  1783. var w_area = workarea;
  1784. zoomChanged(window, {
  1785. width: 0,
  1786. height: 0,
  1787. // center pt of scroll position
  1788. x: (w_area[0].scrollLeft + w_area.width()/2)/zoom,
  1789. y: (w_area[0].scrollTop + w_area.height()/2)/zoom,
  1790. zoom: zoomlevel
  1791. }, true);
  1792. };
  1793. $('#cur_context_panel').delegate('a', 'click', function() {
  1794. var link = $(this);
  1795. if (link.attr('data-root')) {
  1796. svgCanvas.leaveContext();
  1797. } else {
  1798. svgCanvas.setContext(link.text());
  1799. }
  1800. svgCanvas.clearSelection();
  1801. return false;
  1802. });
  1803. var contextChanged = function(win, context) {
  1804. var link_str = '';
  1805. if (context) {
  1806. var str = '';
  1807. link_str = '<a href="#" data-root="y">' + svgCanvas.getCurrentDrawing().getCurrentLayerName() + '</a>';
  1808. $(context).parentsUntil('#svgcontent > g').andSelf().each(function() {
  1809. if (this.id) {
  1810. str += ' > ' + this.id;
  1811. if (this !== context) {
  1812. link_str += ' > <a href="#">' + this.id + '</a>';
  1813. } else {
  1814. link_str += ' > ' + this.id;
  1815. }
  1816. }
  1817. });
  1818. cur_context = str;
  1819. } else {
  1820. cur_context = null;
  1821. }
  1822. $('#cur_context_panel').toggle(!!context).html(link_str);
  1823. updateTitle();
  1824. };
  1825. // Makes sure the current selected paint is available to work with
  1826. var prepPaints = function() {
  1827. paintBox.fill.prep();
  1828. paintBox.stroke.prep();
  1829. };
  1830. var flyout_funcs = {};
  1831. var setFlyoutTitles = function() {
  1832. $('.tools_flyout').each(function() {
  1833. var shower = $('#' + this.id + '_show');
  1834. if (shower.data('isLibrary')) {
  1835. return;
  1836. }
  1837. var tooltips = [];
  1838. $(this).children().each(function() {
  1839. tooltips.push(this.title);
  1840. });
  1841. shower[0].title = tooltips.join(' / ');
  1842. });
  1843. };
  1844. var setFlyoutPositions = function() {
  1845. $('.tools_flyout').each(function() {
  1846. var shower = $('#' + this.id + '_show');
  1847. var pos = shower.offset();
  1848. var w = shower.outerWidth();
  1849. $(this).css({left: (pos.left + w) * editor.tool_scale, top: pos.top});
  1850. });
  1851. };
  1852. var setupFlyouts = function(holders) {
  1853. $.each(holders, function(hold_sel, btn_opts) {
  1854. var buttons = $(hold_sel).children();
  1855. var show_sel = hold_sel + '_show';
  1856. var shower = $(show_sel);
  1857. var def = false;
  1858. buttons.addClass('tool_button')
  1859. .unbind('click mousedown mouseup') // may not be necessary
  1860. .each(function(i) {
  1861. // Get this buttons options
  1862. var opts = btn_opts[i];
  1863. // Remember the function that goes with this ID
  1864. flyout_funcs[opts.sel] = opts.fn;
  1865. if (opts.isDefault) {def = i;}
  1866. // Clicking the icon in flyout should set this set's icon
  1867. var func = function(event) {
  1868. var options = opts;
  1869. //find the currently selected tool if comes from keystroke
  1870. if (event.type === 'keydown') {
  1871. var flyoutIsSelected = $(options.parent + '_show').hasClass('tool_button_current');
  1872. var currentOperation = $(options.parent + '_show').attr('data-curopt');
  1873. $.each(holders[opts.parent], function(i, tool) {
  1874. if (tool.sel == currentOperation) {
  1875. if (!event.shiftKey || !flyoutIsSelected) {
  1876. options = tool;
  1877. } else {
  1878. options = holders[opts.parent][i+1] || holders[opts.parent][0];
  1879. }
  1880. }
  1881. });
  1882. }
  1883. if ($(this).hasClass('disabled')) {return false;}
  1884. if (toolButtonClick(show_sel)) {
  1885. options.fn();
  1886. }
  1887. var icon;
  1888. if (options.icon) {
  1889. icon = $.getSvgIcon(options.icon, true);
  1890. } else {
  1891. icon = $(options.sel).children().eq(0).clone();
  1892. }
  1893. icon[0].setAttribute('width', shower.width());
  1894. icon[0].setAttribute('height', shower.height());
  1895. shower.children(':not(.flyout_arrow_horiz)').remove();
  1896. shower.append(icon).attr('data-curopt', options.sel); // This sets the current mode
  1897. };
  1898. $(this).mouseup(func);
  1899. if (opts.key) {
  1900. $(document).bind('keydown', opts.key[0] + ' shift+' + opts.key[0], func);
  1901. }
  1902. });
  1903. if (def) {
  1904. shower.attr('data-curopt', btn_opts[def].sel);
  1905. } else if (!shower.attr('data-curopt')) {
  1906. // Set first as default
  1907. shower.attr('data-curopt', btn_opts[0].sel);
  1908. }
  1909. var timer;
  1910. var pos = $(show_sel).position();
  1911. // Clicking the "show" icon should set the current mode
  1912. shower.mousedown(function(evt) {
  1913. if (shower.hasClass('disabled')) {
  1914. return false;
  1915. }
  1916. var holder = $(hold_sel);
  1917. var l = pos.left + 34;
  1918. var w = holder.width() * -1;
  1919. var time = holder.data('shown_popop') ? 200 : 0;
  1920. timer = setTimeout(function() {
  1921. // Show corresponding menu
  1922. if (!shower.data('isLibrary')) {
  1923. holder.css('left', w).show().animate({
  1924. left: l
  1925. }, 150);
  1926. } else {
  1927. holder.css('left', l).show();
  1928. }
  1929. holder.data('shown_popop', true);
  1930. },time);
  1931. evt.preventDefault();
  1932. }).mouseup(function(evt) {
  1933. clearTimeout(timer);
  1934. var opt = $(this).attr('data-curopt');
  1935. // Is library and popped up, so do nothing
  1936. if (shower.data('isLibrary') && $(show_sel.replace('_show', '')).is(':visible')) {
  1937. toolButtonClick(show_sel, true);
  1938. return;
  1939. }
  1940. if (toolButtonClick(show_sel) && flyout_funcs[opt]) {
  1941. flyout_funcs[opt]();
  1942. }
  1943. });
  1944. // $('#tools_rect').mouseleave(function(){$('#tools_rect').fadeOut();});
  1945. });
  1946. setFlyoutTitles();
  1947. setFlyoutPositions();
  1948. };
  1949. var makeFlyoutHolder = function(id, child) {
  1950. var div = $('<div>', {
  1951. 'class': 'tools_flyout',
  1952. id: id
  1953. }).appendTo('#svg_editor').append(child);
  1954. return div;
  1955. };
  1956. var uaPrefix = (function() {
  1957. var prop;
  1958. var regex = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/;
  1959. var someScript = document.getElementsByTagName('script')[0];
  1960. for (prop in someScript.style) {
  1961. if (regex.test(prop)) {
  1962. // test is faster than match, so it's better to perform
  1963. // that on the lot and match only when necessary
  1964. return prop.match(regex)[0];
  1965. }
  1966. }
  1967. // Nothing found so far?
  1968. if ('WebkitOpacity' in someScript.style) {return 'Webkit';}
  1969. if ('KhtmlOpacity' in someScript.style) {return 'Khtml';}
  1970. return '';
  1971. }());
  1972. var scaleElements = function(elems, scale) {
  1973. // var prefix = '-' + uaPrefix.toLowerCase() + '-'; // Currently unused
  1974. var sides = ['top', 'left', 'bottom', 'right'];
  1975. elems.each(function() {
  1976. // Handled in CSS
  1977. // this.style[uaPrefix + 'Transform'] = 'scale(' + scale + ')';
  1978. var i;
  1979. var el = $(this);
  1980. var w = el.outerWidth() * (scale - 1);
  1981. var h = el.outerHeight() * (scale - 1);
  1982. // var margins = {}; // Currently unused
  1983. for (i = 0; i < 4; i++) {
  1984. var s = sides[i];
  1985. var cur = el.data('orig_margin-' + s);
  1986. if (cur == null) {
  1987. cur = parseInt(el.css('margin-' + s), 10);
  1988. // Cache the original margin
  1989. el.data('orig_margin-' + s, cur);
  1990. }
  1991. var val = cur * scale;
  1992. if (s === 'right') {
  1993. val += w;
  1994. } else if (s === 'bottom') {
  1995. val += h;
  1996. }
  1997. el.css('margin-' + s, val);
  1998. // el.css('outline', '1px solid red');
  1999. }
  2000. });
  2001. };
  2002. var setIconSize = editor.setIconSize = function (size) {
  2003. // var elems = $('.tool_button, .push_button, .tool_button_current, .disabled, .icon_label, #url_notice, #tool_open');
  2004. var sel_toscale = '#tools_top .toolset, #editor_panel > *, #history_panel > *,'+
  2005. ' #main_button, #tools_left > *, #path_node_panel > *, #multiselected_panel > *,'+
  2006. ' #g_panel > *, #tool_font_size > *, .tools_flyout';
  2007. var elems = $(sel_toscale);
  2008. var scale = 1;
  2009. if (typeof size === 'number') {
  2010. scale = size;
  2011. } else {
  2012. var icon_sizes = {s: 0.75, m:1, l: 1.25, xl: 1.5};
  2013. scale = icon_sizes[size];
  2014. }
  2015. editor.tool_scale = scale;
  2016. setFlyoutPositions();
  2017. // $('.tools_flyout').each(function() {
  2018. // var pos = $(this).position();
  2019. // console.log($(this), pos.left+(34 * scale));
  2020. // $(this).css({'left': pos.left+(34 * scale), 'top': pos.top+(77 * scale)});
  2021. // console.log('l', $(this).css('left'));
  2022. // });
  2023. // var scale = .75;
  2024. var hidden_ps = elems.parents(':hidden');
  2025. hidden_ps.css('visibility', 'hidden').show();
  2026. scaleElements(elems, scale);
  2027. hidden_ps.css('visibility', 'visible').hide();
  2028. // return;
  2029. $.pref('iconsize', size);
  2030. $('#iconsize').val(size);
  2031. // Change icon size
  2032. // $('.tool_button, .push_button, .tool_button_current, .disabled, .icon_label, #url_notice, #tool_open')
  2033. // .find('> svg, > img').each(function() {
  2034. // this.setAttribute('width',size_num);
  2035. // this.setAttribute('height',size_num);
  2036. // });
  2037. //
  2038. // $.resizeSvgIcons({
  2039. // '.flyout_arrow_horiz > svg, .flyout_arrow_horiz > img': size_num / 5,
  2040. // '#logo > svg, #logo > img': size_num * 1.3,
  2041. // '#tools_bottom .icon_label > *': (size_num === 16 ? 18 : size_num * .75)
  2042. // });
  2043. // if (size != 's') {
  2044. // $.resizeSvgIcons({'#layerbuttons svg, #layerbuttons img': size_num * .6});
  2045. // }
  2046. // Note that all rules will be prefixed with '#svg_editor' when parsed
  2047. var cssResizeRules = {
  2048. // '.tool_button,\
  2049. // .push_button,\
  2050. // .tool_button_current,\
  2051. // .push_button_pressed,\
  2052. // .disabled,\
  2053. // .icon_label,\
  2054. // .tools_flyout .tool_button': {
  2055. // 'width': {s: '16px', l: '32px', xl: '48px'},
  2056. // 'height': {s: '16px', l: '32px', xl: '48px'},
  2057. // 'padding': {s: '1px', l: '2px', xl: '3px'}
  2058. // },
  2059. // '.tool_sep': {
  2060. // 'height': {s: '16px', l: '32px', xl: '48px'},
  2061. // 'margin': {s: '2px 2px', l: '2px 5px', xl: '2px 8px'}
  2062. // },
  2063. // '#main_icon': {
  2064. // 'width': {s: '31px', l: '53px', xl: '75px'},
  2065. // 'height': {s: '22px', l: '42px', xl: '64px'}
  2066. // },
  2067. '#tools_top': {
  2068. 'left': 50,
  2069. 'height': 72
  2070. },
  2071. '#tools_left': {
  2072. 'width': 31,
  2073. 'top': 74
  2074. },
  2075. 'div#workarea': {
  2076. 'left': 38,
  2077. 'top': 74
  2078. }
  2079. // '#tools_bottom': {
  2080. // 'left': {s: '27px', l: '46px', xl: '65px'},
  2081. // 'height': {s: '58px', l: '98px', xl: '145px'}
  2082. // },
  2083. // '#color_tools': {
  2084. // 'border-spacing': {s: '0 1px'},
  2085. // 'margin-top': {s: '-1px'}
  2086. // },
  2087. // '#color_tools .icon_label': {
  2088. // 'width': {l:'43px', xl: '60px'}
  2089. // },
  2090. // '.color_tool': {
  2091. // 'height': {s: '20px'}
  2092. // },
  2093. // '#tool_opacity': {
  2094. // 'top': {s: '1px'},
  2095. // 'height': {s: 'auto', l:'auto', xl:'auto'}
  2096. // },
  2097. // '#tools_top input, #tools_bottom input': {
  2098. // 'margin-top': {s: '2px', l: '4px', xl: '5px'},
  2099. // 'height': {s: 'auto', l: 'auto', xl: 'auto'},
  2100. // 'border': {s: '1px solid #555', l: 'auto', xl: 'auto'},
  2101. // 'font-size': {s: '.9em', l: '1.2em', xl: '1.4em'}
  2102. // },
  2103. // '#zoom_panel': {
  2104. // 'margin-top': {s: '3px', l: '4px', xl: '5px'}
  2105. // },
  2106. // '#copyright, #tools_bottom .label': {
  2107. // 'font-size': {l: '1.5em', xl: '2em'},
  2108. // 'line-height': {s: '15px'}
  2109. // },
  2110. // '#tools_bottom_2': {
  2111. // 'width': {l: '295px', xl: '355px'},
  2112. // 'top': {s: '4px'}
  2113. // },
  2114. // '#tools_top > div, #tools_top': {
  2115. // 'line-height': {s: '17px', l: '34px', xl: '50px'}
  2116. // },
  2117. // '.dropdown button': {
  2118. // 'height': {s: '18px', l: '34px', xl: '40px'},
  2119. // 'line-height': {s: '18px', l: '34px', xl: '40px'},
  2120. // 'margin-top': {s: '3px'}
  2121. // },
  2122. // '#tools_top label, #tools_bottom label': {
  2123. // 'font-size': {s: '1em', l: '1.5em', xl: '2em'},
  2124. // 'height': {s: '25px', l: '42px', xl: '64px'}
  2125. // },
  2126. // 'div.toolset': {
  2127. // 'height': {s: '25px', l: '42px', xl: '64px'}
  2128. // },
  2129. // '#tool_bold, #tool_italic': {
  2130. // 'font-size': {s: '1.5em', l: '3em', xl: '4.5em'}
  2131. // },
  2132. // '#sidepanels': {
  2133. // 'top': {s: '50px', l: '88px', xl: '125px'},
  2134. // 'bottom': {s: '51px', l: '68px', xl: '65px'}
  2135. // },
  2136. // '#layerbuttons': {
  2137. // 'width': {l: '130px', xl: '175px'},
  2138. // 'height': {l: '24px', xl: '30px'}
  2139. // },
  2140. // '#layerlist': {
  2141. // 'width': {l: '128px', xl: '150px'}
  2142. // },
  2143. // '.layer_button': {
  2144. // 'width': {l: '19px', xl: '28px'},
  2145. // 'height': {l: '19px', xl: '28px'}
  2146. // },
  2147. // 'input.spin-button': {
  2148. // 'background-image': {l: 'url('images/spinbtn_updn_big.png')', xl: 'url('images/spinbtn_updn_big.png')'},
  2149. // 'background-position': {l: '100% -5px', xl: '100% -2px'},
  2150. // 'padding-right': {l: '24px', xl: '24px' }
  2151. // },
  2152. // 'input.spin-button.up': {
  2153. // 'background-position': {l: '100% -45px', xl: '100% -42px'}
  2154. // },
  2155. // 'input.spin-button.down': {
  2156. // 'background-position': {l: '100% -85px', xl: '100% -82px'}
  2157. // },
  2158. // '#position_opts': {
  2159. // 'width': {all: (size_num*4) +'px'}
  2160. // }
  2161. };
  2162. var rule_elem = $('#tool_size_rules');
  2163. if (!rule_elem.length) {
  2164. rule_elem = $('<style id="tool_size_rules"></style>').appendTo('head');
  2165. } else {
  2166. rule_elem.empty();
  2167. }
  2168. if (size !== 'm') {
  2169. var styleStr = '';
  2170. $.each(cssResizeRules, function(selector, rules) {
  2171. selector = '#svg_editor ' + selector.replace(/,/g,', #svg_editor');
  2172. styleStr += selector + '{';
  2173. $.each(rules, function(prop, values) {
  2174. var val;
  2175. if (typeof values === 'number') {
  2176. val = (values * scale) + 'px';
  2177. } else if (values[size] || values.all) {
  2178. val = (values[size] || values.all);
  2179. }
  2180. styleStr += (prop + ':' + val + ';');
  2181. });
  2182. styleStr += '}';
  2183. });
  2184. //this.style[uaPrefix + 'Transform'] = 'scale(' + scale + ')';
  2185. var prefix = '-' + uaPrefix.toLowerCase() + '-';
  2186. styleStr += (sel_toscale + '{' + prefix + 'transform: scale(' + scale + ');}'
  2187. + ' #svg_editor div.toolset .toolset {' + prefix + 'transform: scale(1); margin: 1px !important;}' // Hack for markers
  2188. + ' #svg_editor .ui-slider {' + prefix + 'transform: scale(' + (1/scale) + ');}' // Hack for sliders
  2189. );
  2190. rule_elem.text(styleStr);
  2191. }
  2192. setFlyoutPositions();
  2193. };
  2194. // TODO: Combine this with addDropDown or find other way to optimize
  2195. var addAltDropDown = function(elem, list, callback, opts) {
  2196. var button = $(elem);
  2197. list = $(list);
  2198. var on_button = false;
  2199. var dropUp = opts.dropUp;
  2200. if (dropUp) {
  2201. $(elem).addClass('dropup');
  2202. }
  2203. list.find('li').bind('mouseup', function() {
  2204. if (opts.seticon) {
  2205. setIcon('#cur_' + button[0].id , $(this).children());
  2206. $(this).addClass('current').siblings().removeClass('current');
  2207. }
  2208. callback.apply(this, arguments);
  2209. });
  2210. $(window).mouseup(function(evt) {
  2211. if (!on_button) {
  2212. button.removeClass('down');
  2213. list.hide();
  2214. list.css({top:0, left:0});
  2215. }
  2216. on_button = false;
  2217. });
  2218. // var height = list.height(); // Currently unused
  2219. button.bind('mousedown',function() {
  2220. var off = button.offset();
  2221. if (dropUp) {
  2222. off.top -= list.height();
  2223. off.left += 8;
  2224. } else {
  2225. off.top += button.height();
  2226. }
  2227. list.offset(off);
  2228. if (!button.hasClass('down')) {
  2229. list.show();
  2230. on_button = true;
  2231. } else {
  2232. // CSS position must be reset for Webkit
  2233. list.hide();
  2234. list.css({top:0, left:0});
  2235. }
  2236. button.toggleClass('down');
  2237. }).hover(function() {
  2238. on_button = true;
  2239. }).mouseout(function() {
  2240. on_button = false;
  2241. });
  2242. if (opts.multiclick) {
  2243. list.mousedown(function() {
  2244. on_button = true;
  2245. });
  2246. }
  2247. };
  2248. var extsPreLang = [];
  2249. var extAdded = function(win, ext) {
  2250. if (!ext) {
  2251. return;
  2252. }
  2253. var cb_called = false;
  2254. var resize_done = false;
  2255. var cb_ready = true; // Set to false to delay callback (e.g. wait for $.svgIcons)
  2256. if (ext.langReady) {
  2257. if (editor.langChanged) { // We check for this since the "lang" pref could have been set by storage
  2258. var lang = $.pref('lang');
  2259. ext.langReady({lang:lang, uiStrings:uiStrings});
  2260. }
  2261. else {
  2262. extsPreLang.push(ext);
  2263. }
  2264. }
  2265. function prepResize() {
  2266. if (resize_timer) {
  2267. clearTimeout(resize_timer);
  2268. resize_timer = null;
  2269. }
  2270. if (!resize_done) {
  2271. resize_timer = setTimeout(function() {
  2272. resize_done = true;
  2273. setIconSize($.pref('iconsize'));
  2274. }, 50);
  2275. }
  2276. }
  2277. var runCallback = function() {
  2278. if (ext.callback && !cb_called && cb_ready) {
  2279. cb_called = true;
  2280. ext.callback();
  2281. }
  2282. };
  2283. var btn_selects = [];
  2284. if (ext.context_tools) {
  2285. $.each(ext.context_tools, function(i, tool) {
  2286. // Add select tool
  2287. var html;
  2288. var cont_id = tool.container_id ? (' id="' + tool.container_id + '"') : '';
  2289. var panel = $('#' + tool.panel);
  2290. // create the panel if it doesn't exist
  2291. if (!panel.length) {
  2292. panel = $('<div>', {id: tool.panel}).appendTo('#tools_top');
  2293. }
  2294. // TODO: Allow support for other types, or adding to existing tool
  2295. switch (tool.type) {
  2296. case 'tool_button':
  2297. html = '<div class="tool_button">' + tool.id + '</div>';
  2298. var div = $(html).appendTo(panel);
  2299. if (tool.events) {
  2300. $.each(tool.events, function(evt, func) {
  2301. $(div).bind(evt, func);
  2302. });
  2303. }
  2304. break;
  2305. case 'select':
  2306. html = '<label' + cont_id + '>'
  2307. + '<select id="' + tool.id + '">';
  2308. $.each(tool.options, function(val, text) {
  2309. var sel = (val == tool.defval) ? ' selected' : '';
  2310. html += '<option value="'+val+'"' + sel + '>' + text + '</option>';
  2311. });
  2312. html += '</select></label>';
  2313. // Creates the tool, hides & adds it, returns the select element
  2314. var sel = $(html).appendTo(panel).find('select');
  2315. $.each(tool.events, function(evt, func) {
  2316. $(sel).bind(evt, func);
  2317. });
  2318. break;
  2319. case 'button-select':
  2320. html = '<div id="' + tool.id + '" class="dropdown toolset" title="' + tool.title + '">'
  2321. + '<div id="cur_' + tool.id + '" class="icon_label"></div><button></button></div>';
  2322. var list = $('<ul id="' + tool.id + '_opts"></ul>').appendTo('#option_lists');
  2323. if (tool.colnum) {
  2324. list.addClass('optcols' + tool.colnum);
  2325. }
  2326. // Creates the tool, hides & adds it, returns the select element
  2327. var dropdown = $(html).appendTo(panel).children();
  2328. btn_selects.push({
  2329. elem: ('#' + tool.id),
  2330. list: ('#' + tool.id + '_opts'),
  2331. title: tool.title,
  2332. callback: tool.events.change,
  2333. cur: ('#cur_' + tool.id)
  2334. });
  2335. break;
  2336. case 'input':
  2337. html = '<label' + cont_id + '>'
  2338. + '<span id="' + tool.id + '_label">'
  2339. + tool.label + ':</span>'
  2340. + '<input id="' + tool.id + '" title="' + tool.title
  2341. + '" size="' + (tool.size || '4') + '" value="' + (tool.defval || '') + '" type="text"/></label>';
  2342. // Creates the tool, hides & adds it, returns the select element
  2343. // Add to given tool.panel
  2344. var inp = $(html).appendTo(panel).find('input');
  2345. if (tool.spindata) {
  2346. inp.SpinButton(tool.spindata);
  2347. }
  2348. if (tool.events) {
  2349. $.each(tool.events, function(evt, func) {
  2350. inp.bind(evt, func);
  2351. });
  2352. }
  2353. break;
  2354. default:
  2355. break;
  2356. }
  2357. });
  2358. }
  2359. if (ext.buttons) {
  2360. var fallback_obj = {},
  2361. placement_obj = {},
  2362. svgicons = ext.svgicons,
  2363. holders = {};
  2364. // Add buttons given by extension
  2365. $.each(ext.buttons, function(i, btn) {
  2366. var icon, svgicon, tls_id;
  2367. var id = btn.id;
  2368. var num = i;
  2369. // Give button a unique ID
  2370. while($('#'+id).length) {
  2371. id = btn.id + '_' + (++num);
  2372. }
  2373. if (!svgicons) {
  2374. icon = $('<img src="' + btn.icon + '">');
  2375. } else {
  2376. fallback_obj[id] = btn.icon;
  2377. svgicon = btn.svgicon || btn.id;
  2378. if (btn.type == 'app_menu') {
  2379. placement_obj['#' + id + ' > div'] = svgicon;
  2380. } else {
  2381. placement_obj['#' + id] = svgicon;
  2382. }
  2383. }
  2384. var cls, parent;
  2385. // Set button up according to its type
  2386. switch ( btn.type ) {
  2387. case 'mode_flyout':
  2388. case 'mode':
  2389. cls = 'tool_button';
  2390. parent = '#tools_left';
  2391. break;
  2392. case 'context':
  2393. cls = 'tool_button';
  2394. parent = '#' + btn.panel;
  2395. // create the panel if it doesn't exist
  2396. if (!$(parent).length) {
  2397. $('<div>', {id: btn.panel}).appendTo('#tools_top');
  2398. }
  2399. break;
  2400. case 'app_menu':
  2401. cls = '';
  2402. parent = '#main_menu ul';
  2403. break;
  2404. }
  2405. var flyout_holder, cur_h, show_btn, ref_data, ref_btn;
  2406. var button = $((btn.list || btn.type == 'app_menu') ? '<li/>' : '<div/>')
  2407. .attr('id', id)
  2408. .attr('title', btn.title)
  2409. .addClass(cls);
  2410. if (!btn.includeWith && !btn.list) {
  2411. if ('position' in btn) {
  2412. if ($(parent).children().eq(btn.position).length) {
  2413. $(parent).children().eq(btn.position).before(button);
  2414. }
  2415. else {
  2416. $(parent).children().last().before(button);
  2417. }
  2418. } else {
  2419. button.appendTo(parent);
  2420. }
  2421. if (btn.type =='mode_flyout') {
  2422. // Add to flyout menu / make flyout menu
  2423. // var opts = btn.includeWith;
  2424. // // opts.button, default, position
  2425. ref_btn = $(button);
  2426. flyout_holder = ref_btn.parent();
  2427. // Create a flyout menu if there isn't one already
  2428. if (!ref_btn.parent().hasClass('tools_flyout')) {
  2429. // Create flyout placeholder
  2430. tls_id = ref_btn[0].id.replace('tool_', 'tools_');
  2431. show_btn = ref_btn.clone()
  2432. .attr('id', tls_id + '_show')
  2433. .append($('<div>', {'class': 'flyout_arrow_horiz'}));
  2434. ref_btn.before(show_btn);
  2435. // Create a flyout div
  2436. flyout_holder = makeFlyoutHolder(tls_id, ref_btn);
  2437. flyout_holder.data('isLibrary', true);
  2438. show_btn.data('isLibrary', true);
  2439. }
  2440. // ref_data = Actions.getButtonData(opts.button);
  2441. placement_obj['#' + tls_id + '_show'] = btn.id;
  2442. // TODO: Find way to set the current icon using the iconloader if this is not default
  2443. // Include data for extension button as well as ref button
  2444. cur_h = holders['#'+flyout_holder[0].id] = [{
  2445. sel: '#'+id,
  2446. fn: btn.events.click,
  2447. icon: btn.id,
  2448. // key: btn.key,
  2449. isDefault: true
  2450. }, ref_data];
  2451. //
  2452. // // {sel:'#tool_rect', fn: clickRect, evt: 'mouseup', key: 4, parent: '#tools_rect', icon: 'rect'}
  2453. //
  2454. // var pos = ('position' in opts)?opts.position:'last';
  2455. // var len = flyout_holder.children().length;
  2456. //
  2457. // // Add at given position or end
  2458. // if (!isNaN(pos) && pos >= 0 && pos < len) {
  2459. // flyout_holder.children().eq(pos).before(button);
  2460. // } else {
  2461. // flyout_holder.append(button);
  2462. // cur_h.reverse();
  2463. // }
  2464. } else if (btn.type == 'app_menu') {
  2465. button.append('<div>').append(btn.title);
  2466. }
  2467. }
  2468. else if (btn.list) {
  2469. // Add button to list
  2470. button.addClass('push_button');
  2471. $('#' + btn.list + '_opts').append(button);
  2472. if (btn.isDefault) {
  2473. $('#cur_' + btn.list).append(button.children().clone());
  2474. svgicon = btn.svgicon || btn.id;
  2475. placement_obj['#cur_' + btn.list] = svgicon;
  2476. }
  2477. }
  2478. else if (btn.includeWith) {
  2479. // Add to flyout menu / make flyout menu
  2480. var opts = btn.includeWith;
  2481. // opts.button, default, position
  2482. ref_btn = $(opts.button);
  2483. flyout_holder = ref_btn.parent();
  2484. // Create a flyout menu if there isn't one already
  2485. if (!ref_btn.parent().hasClass('tools_flyout')) {
  2486. // Create flyout placeholder
  2487. tls_id = ref_btn[0].id.replace('tool_', 'tools_');
  2488. show_btn = ref_btn.clone()
  2489. .attr('id',tls_id + '_show')
  2490. .append($('<div>', {'class': 'flyout_arrow_horiz'}));
  2491. ref_btn.before(show_btn);
  2492. // Create a flyout div
  2493. flyout_holder = makeFlyoutHolder(tls_id, ref_btn);
  2494. }
  2495. ref_data = Actions.getButtonData(opts.button);
  2496. if (opts.isDefault) {
  2497. placement_obj['#' + tls_id + '_show'] = btn.id;
  2498. }
  2499. // TODO: Find way to set the current icon using the iconloader if this is not default
  2500. // Include data for extension button as well as ref button
  2501. cur_h = holders['#' + flyout_holder[0].id] = [{
  2502. sel: '#' + id,
  2503. fn: btn.events.click,
  2504. icon: btn.id,
  2505. key: btn.key,
  2506. isDefault: btn.includeWith ? btn.includeWith.isDefault : 0
  2507. }, ref_data];
  2508. // {sel:'#tool_rect', fn: clickRect, evt: 'mouseup', key: 4, parent: '#tools_rect', icon: 'rect'}
  2509. var pos = ('position' in opts) ? opts.position : 'last';
  2510. var len = flyout_holder.children().length;
  2511. // Add at given position or end
  2512. if (!isNaN(pos) && pos >= 0 && pos < len) {
  2513. flyout_holder.children().eq(pos).before(button);
  2514. } else {
  2515. flyout_holder.append(button);
  2516. cur_h.reverse();
  2517. }
  2518. }
  2519. if (!svgicons) {
  2520. button.append(icon);
  2521. }
  2522. if (!btn.list) {
  2523. // Add given events to button
  2524. $.each(btn.events, function(name, func) {
  2525. if (name == 'click' && btn.type == 'mode') {
  2526. if (btn.includeWith) {
  2527. button.bind(name, func);
  2528. } else {
  2529. button.bind(name, function() {
  2530. if (toolButtonClick(button)) {
  2531. func();
  2532. }
  2533. });
  2534. }
  2535. if (btn.key) {
  2536. $(document).bind('keydown', btn.key, func);
  2537. if (btn.title) {
  2538. button.attr('title', btn.title + ' ['+btn.key+']');
  2539. }
  2540. }
  2541. } else {
  2542. button.bind(name, func);
  2543. }
  2544. });
  2545. }
  2546. setupFlyouts(holders);
  2547. });
  2548. $.each(btn_selects, function() {
  2549. addAltDropDown(this.elem, this.list, this.callback, {seticon: true});
  2550. });
  2551. if (svgicons) {
  2552. cb_ready = false; // Delay callback
  2553. }
  2554. $.svgIcons(svgicons, {
  2555. w: 24, h: 24,
  2556. id_match: false,
  2557. no_img: (!svgedit.browser.isWebkit()),
  2558. fallback: fallback_obj,
  2559. placement: placement_obj,
  2560. callback: function (icons) {
  2561. // Non-ideal hack to make the icon match the current size
  2562. //if (curPrefs.iconsize && curPrefs.iconsize !== 'm') {
  2563. if ($.pref('iconsize') !== 'm') {
  2564. prepResize();
  2565. }
  2566. cb_ready = true; // Ready for callback
  2567. runCallback();
  2568. }
  2569. });
  2570. }
  2571. runCallback();
  2572. };
  2573. var getPaint = function(color, opac, type) {
  2574. // update the editor's fill paint
  2575. var opts = { alpha: opac };
  2576. if (color.indexOf('url(#') === 0) {
  2577. var refElem = svgCanvas.getRefElem(color);
  2578. if (refElem) {
  2579. refElem = refElem.cloneNode(true);
  2580. } else {
  2581. refElem = $('#' + type + '_color defs *')[0];
  2582. }
  2583. opts[refElem.tagName] = refElem;
  2584. } else if (color.indexOf('#') === 0) {
  2585. opts.solidColor = color.substr(1);
  2586. } else {
  2587. opts.solidColor = 'none';
  2588. }
  2589. return new $.jGraduate.Paint(opts);
  2590. };
  2591. $('#text').focus( function(){ textBeingEntered = true; } );
  2592. $('#text').blur( function(){ textBeingEntered = false; } );
  2593. // bind the selected event to our function that handles updates to the UI
  2594. svgCanvas.bind('selected', selectedChanged);
  2595. svgCanvas.bind('transition', elementTransition);
  2596. svgCanvas.bind('changed', elementChanged);
  2597. svgCanvas.bind('saved', saveHandler);
  2598. svgCanvas.bind('exported', checkCanvg(exportHandler));
  2599. svgCanvas.bind('zoomed', zoomChanged);
  2600. svgCanvas.bind('contextset', contextChanged);
  2601. svgCanvas.bind('extension_added', extAdded);
  2602. svgCanvas.textActions.setInputElem($('#text')[0]);
  2603. var str = '<div class="palette_item" data-rgb="none"></div>';
  2604. $.each(palette, function(i, item) {
  2605. str += '<div class="palette_item" style="background-color: ' + item + ';" data-rgb="' + item + '"></div>';
  2606. });
  2607. $('#palette').append(str);
  2608. // Set up editor background functionality
  2609. // TODO add checkerboard as "pattern"
  2610. var color_blocks = ['#FFF', '#888', '#000']; // ,'url(data:image/gif;base64,R0lGODlhEAAQAIAAAP%2F%2F%2F9bW1iH5BAAAAAAALAAAAAAQABAAAAIfjG%2Bgq4jM3IFLJgpswNly%2FXkcBpIiVaInlLJr9FZWAQA7)'];
  2611. str = '';
  2612. $.each(color_blocks, function() {
  2613. str += '<div class="color_block" style="background-color:' + this + ';"></div>';
  2614. });
  2615. $('#bg_blocks').append(str);
  2616. var blocks = $('#bg_blocks div');
  2617. var cur_bg = 'cur_background';
  2618. blocks.each(function() {
  2619. var blk = $(this);
  2620. blk.click(function() {
  2621. blocks.removeClass(cur_bg);
  2622. $(this).addClass(cur_bg);
  2623. });
  2624. });
  2625. setBackground($.pref('bkgd_color'), $.pref('bkgd_url'));
  2626. $('#image_save_opts input').val([$.pref('img_save')]);
  2627. var changeRectRadius = function(ctl) {
  2628. svgCanvas.setRectRadius(ctl.value);
  2629. };
  2630. var changeFontSize = function(ctl) {
  2631. svgCanvas.setFontSize(ctl.value);
  2632. };
  2633. var changeStrokeWidth = function(ctl) {
  2634. var val = ctl.value;
  2635. if (val == 0 && selectedElement && ['line', 'polyline'].indexOf(selectedElement.nodeName) >= 0) {
  2636. val = ctl.value = 1;
  2637. }
  2638. svgCanvas.setStrokeWidth(val);
  2639. };
  2640. var changeRotationAngle = function(ctl) {
  2641. svgCanvas.setRotationAngle(ctl.value);
  2642. $('#tool_reorient').toggleClass('disabled', parseInt(ctl.value, 10) === 0);
  2643. };
  2644. var changeOpacity = function(ctl, val) {
  2645. if (val == null) {val = ctl.value;}
  2646. $('#group_opacity').val(val);
  2647. if (!ctl || !ctl.handle) {
  2648. $('#opac_slider').slider('option', 'value', val);
  2649. }
  2650. svgCanvas.setOpacity(val/100);
  2651. };
  2652. var changeBlur = function(ctl, val, noUndo) {
  2653. if (val == null) {val = ctl.value;}
  2654. $('#blur').val(val);
  2655. var complete = false;
  2656. if (!ctl || !ctl.handle) {
  2657. $('#blur_slider').slider('option', 'value', val);
  2658. complete = true;
  2659. }
  2660. if (noUndo) {
  2661. svgCanvas.setBlurNoUndo(val);
  2662. } else {
  2663. svgCanvas.setBlur(val, complete);
  2664. }
  2665. };
  2666. $('#stroke_style').change(function() {
  2667. svgCanvas.setStrokeAttr('stroke-dasharray', $(this).val());
  2668. operaRepaint();
  2669. });
  2670. $('#stroke_linejoin').change(function() {
  2671. svgCanvas.setStrokeAttr('stroke-linejoin', $(this).val());
  2672. operaRepaint();
  2673. });
  2674. // Lose focus for select elements when changed (Allows keyboard shortcuts to work better)
  2675. $('select').change(function(){$(this).blur();});
  2676. // fired when user wants to move elements to another layer
  2677. var promptMoveLayerOnce = false;
  2678. $('#selLayerNames').change(function() {
  2679. var destLayer = this.options[this.selectedIndex].value;
  2680. var confirmStr = uiStrings.notification.QmoveElemsToLayer.replace('%s', destLayer);
  2681. var moveToLayer = function(ok) {
  2682. if (!ok) {return;}
  2683. promptMoveLayerOnce = true;
  2684. svgCanvas.moveSelectedToLayer(destLayer);
  2685. svgCanvas.clearSelection();
  2686. populateLayers();
  2687. };
  2688. if (destLayer) {
  2689. if (promptMoveLayerOnce) {
  2690. moveToLayer(true);
  2691. } else {
  2692. $.confirm(confirmStr, moveToLayer);
  2693. }
  2694. }
  2695. });
  2696. $('#font_family').change(function() {
  2697. svgCanvas.setFontFamily(this.value);
  2698. });
  2699. $('#seg_type').change(function() {
  2700. svgCanvas.setSegType($(this).val());
  2701. });
  2702. $('#text').keyup(function() {
  2703. svgCanvas.setTextContent(this.value);
  2704. });
  2705. $('#image_url').change(function() {
  2706. setImageURL(this.value);
  2707. });
  2708. $('#link_url').change(function() {
  2709. if (this.value.length) {
  2710. svgCanvas.setLinkURL(this.value);
  2711. } else {
  2712. svgCanvas.removeHyperlink();
  2713. }
  2714. });
  2715. $('#g_title').change(function() {
  2716. svgCanvas.setGroupTitle(this.value);
  2717. });
  2718. $('.attr_changer').change(function() {
  2719. var attr = this.getAttribute('data-attr');
  2720. var val = this.value;
  2721. var valid = svgedit.units.isValidUnit(attr, val, selectedElement);
  2722. if (!valid) {
  2723. $.alert(uiStrings.notification.invalidAttrValGiven);
  2724. this.value = selectedElement.getAttribute(attr);
  2725. return false;
  2726. }
  2727. if (attr !== 'id') {
  2728. if (isNaN(val)) {
  2729. val = svgCanvas.convertToNum(attr, val);
  2730. } else if (curConfig.baseUnit !== 'px') {
  2731. // Convert unitless value to one with given unit
  2732. var unitData = svgedit.units.getTypeMap();
  2733. if (selectedElement[attr] || svgCanvas.getMode() === 'pathedit' || attr === 'x' || attr === 'y') {
  2734. val *= unitData[curConfig.baseUnit];
  2735. }
  2736. }
  2737. }
  2738. // if the user is changing the id, then de-select the element first
  2739. // change the ID, then re-select it with the new ID
  2740. if (attr === 'id') {
  2741. var elem = selectedElement;
  2742. svgCanvas.clearSelection();
  2743. elem.id = val;
  2744. svgCanvas.addToSelection([elem],true);
  2745. } else {
  2746. svgCanvas.changeSelectedAttribute(attr, val);
  2747. }
  2748. this.blur();
  2749. });
  2750. // Prevent selection of elements when shift-clicking
  2751. $('#palette').mouseover(function() {
  2752. var inp = $('<input type="hidden">');
  2753. $(this).append(inp);
  2754. inp.focus().remove();
  2755. });
  2756. $('.palette_item').mousedown(function(evt) {
  2757. // shift key or right click for stroke
  2758. var picker = evt.shiftKey || evt.button === 2 ? 'stroke' : 'fill';
  2759. var color = $(this).data('rgb');
  2760. var paint;
  2761. // Webkit-based browsers returned 'initial' here for no stroke
  2762. if (color === 'none' || color === 'transparent' || color === 'initial') {
  2763. color = 'none';
  2764. paint = new $.jGraduate.Paint();
  2765. } else {
  2766. paint = new $.jGraduate.Paint({alpha: 100, solidColor: color.substr(1)});
  2767. }
  2768. paintBox[picker].setPaint(paint);
  2769. svgCanvas.setColor(picker, color);
  2770. if (color !== 'none' && svgCanvas.getPaintOpacity(picker) !== 1) {
  2771. svgCanvas.setPaintOpacity(picker, 1.0);
  2772. }
  2773. updateToolButtonState();
  2774. }).bind('contextmenu', function(e) {e.preventDefault();});
  2775. $('#toggle_stroke_tools').on('click', function() {
  2776. $('#tools_bottom').toggleClass('expanded');
  2777. });
  2778. (function() {
  2779. var last_x = null, last_y = null, w_area = workarea[0],
  2780. panning = false, keypan = false;
  2781. $('#svgcanvas').bind('mousemove mouseup', function(evt) {
  2782. if (panning === false) {return;}
  2783. w_area.scrollLeft -= (evt.clientX - last_x);
  2784. w_area.scrollTop -= (evt.clientY - last_y);
  2785. last_x = evt.clientX;
  2786. last_y = evt.clientY;
  2787. if (evt.type === 'mouseup') {panning = false;}
  2788. return false;
  2789. }).mousedown(function(evt) {
  2790. if (evt.button === 1 || keypan === true) {
  2791. panning = true;
  2792. last_x = evt.clientX;
  2793. last_y = evt.clientY;
  2794. return false;
  2795. }
  2796. });
  2797. $(window).mouseup(function() {
  2798. panning = false;
  2799. });
  2800. $(document).bind('keydown', 'space', function(evt) {
  2801. svgCanvas.spaceKey = keypan = true;
  2802. evt.preventDefault();
  2803. }).bind('keyup', 'space', function(evt) {
  2804. evt.preventDefault();
  2805. svgCanvas.spaceKey = keypan = false;
  2806. }).bind('keydown', 'shift', function(evt) {
  2807. if (svgCanvas.getMode() === 'zoom') {
  2808. workarea.css('cursor', zoomOutIcon);
  2809. }
  2810. }).bind('keyup', 'shift', function(evt) {
  2811. if (svgCanvas.getMode() === 'zoom') {
  2812. workarea.css('cursor', zoomInIcon);
  2813. }
  2814. });
  2815. editor.setPanning = function(active) {
  2816. svgCanvas.spaceKey = keypan = active;
  2817. };
  2818. }());
  2819. (function () {
  2820. var button = $('#main_icon');
  2821. var overlay = $('#main_icon span');
  2822. var list = $('#main_menu');
  2823. var on_button = false;
  2824. var height = 0;
  2825. var js_hover = true;
  2826. var set_click = false;
  2827. /*
  2828. // Currently unused
  2829. var hideMenu = function() {
  2830. list.fadeOut(200);
  2831. };
  2832. */
  2833. $(window).mouseup(function(evt) {
  2834. if (!on_button) {
  2835. button.removeClass('buttondown');
  2836. // do not hide if it was the file input as that input needs to be visible
  2837. // for its change event to fire
  2838. if (evt.target.tagName != 'INPUT') {
  2839. list.fadeOut(200);
  2840. } else if (!set_click) {
  2841. set_click = true;
  2842. $(evt.target).click(function() {
  2843. list.css('margin-left', '-9999px').show();
  2844. });
  2845. }
  2846. }
  2847. on_button = false;
  2848. }).mousedown(function(evt) {
  2849. // $('.contextMenu').hide();
  2850. var islib = $(evt.target).closest('div.tools_flyout, .contextMenu').length;
  2851. if (!islib) {$('.tools_flyout:visible,.contextMenu').fadeOut(250);}
  2852. });
  2853. overlay.bind('mousedown',function() {
  2854. if (!button.hasClass('buttondown')) {
  2855. // Margin must be reset in case it was changed before;
  2856. list.css('margin-left', 0).show();
  2857. if (!height) {
  2858. height = list.height();
  2859. }
  2860. // Using custom animation as slideDown has annoying 'bounce effect'
  2861. list.css('height',0).animate({
  2862. 'height': height
  2863. }, 200);
  2864. on_button = true;
  2865. } else {
  2866. list.fadeOut(200);
  2867. }
  2868. button.toggleClass('buttondown buttonup');
  2869. }).hover(function() {
  2870. on_button = true;
  2871. }).mouseout(function() {
  2872. on_button = false;
  2873. });
  2874. var list_items = $('#main_menu li');
  2875. // Check if JS method of hovering needs to be used (Webkit bug)
  2876. list_items.mouseover(function() {
  2877. js_hover = ($(this).css('background-color') == 'rgba(0, 0, 0, 0)');
  2878. list_items.unbind('mouseover');
  2879. if (js_hover) {
  2880. list_items.mouseover(function() {
  2881. this.style.backgroundColor = '#FFC';
  2882. }).mouseout(function() {
  2883. this.style.backgroundColor = 'transparent';
  2884. return true;
  2885. });
  2886. }
  2887. });
  2888. }());
  2889. // Made public for UI customization.
  2890. // TODO: Group UI functions into a public editor.ui interface.
  2891. editor.addDropDown = function(elem, callback, dropUp) {
  2892. if ($(elem).length == 0) {return;} // Quit if called on non-existant element
  2893. var button = $(elem).find('button');
  2894. var list = $(elem).find('ul').attr('id', $(elem)[0].id + '-list');
  2895. var on_button = false;
  2896. if (dropUp) {
  2897. $(elem).addClass('dropup');
  2898. } else {
  2899. // Move list to place where it can overflow container
  2900. $('#option_lists').append(list);
  2901. }
  2902. list.find('li').bind('mouseup', callback);
  2903. $(window).mouseup(function(evt) {
  2904. if (!on_button) {
  2905. button.removeClass('down');
  2906. list.hide();
  2907. }
  2908. on_button = false;
  2909. });
  2910. button.bind('mousedown',function() {
  2911. if (!button.hasClass('down')) {
  2912. if (!dropUp) {
  2913. var pos = $(elem).position();
  2914. list.css({
  2915. top: pos.top + 24,
  2916. left: pos.left - 10
  2917. });
  2918. }
  2919. list.show();
  2920. on_button = true;
  2921. } else {
  2922. list.hide();
  2923. }
  2924. button.toggleClass('down');
  2925. }).hover(function() {
  2926. on_button = true;
  2927. }).mouseout(function() {
  2928. on_button = false;
  2929. });
  2930. };
  2931. editor.addDropDown('#font_family_dropdown', function() {
  2932. $('#font_family').val($(this).text()).change();
  2933. });
  2934. editor.addDropDown('#opacity_dropdown', function() {
  2935. if ($(this).find('div').length) {return;}
  2936. var perc = parseInt($(this).text().split('%')[0], 10);
  2937. changeOpacity(false, perc);
  2938. }, true);
  2939. // For slider usage, see: http://jqueryui.com/demos/slider/
  2940. $('#opac_slider').slider({
  2941. start: function() {
  2942. $('#opacity_dropdown li:not(.special)').hide();
  2943. },
  2944. stop: function() {
  2945. $('#opacity_dropdown li').show();
  2946. $(window).mouseup();
  2947. },
  2948. slide: function(evt, ui) {
  2949. changeOpacity(ui);
  2950. }
  2951. });
  2952. editor.addDropDown('#blur_dropdown', $.noop);
  2953. var slideStart = false;
  2954. $('#blur_slider').slider({
  2955. max: 10,
  2956. step: 0.1,
  2957. stop: function(evt, ui) {
  2958. slideStart = false;
  2959. changeBlur(ui);
  2960. $('#blur_dropdown li').show();
  2961. $(window).mouseup();
  2962. },
  2963. start: function() {
  2964. slideStart = true;
  2965. },
  2966. slide: function(evt, ui) {
  2967. changeBlur(ui, null, slideStart);
  2968. }
  2969. });
  2970. editor.addDropDown('#zoom_dropdown', function() {
  2971. var item = $(this);
  2972. var val = item.data('val');
  2973. if (val) {
  2974. zoomChanged(window, val);
  2975. } else {
  2976. changeZoom({value: parseFloat(item.text())});
  2977. }
  2978. }, true);
  2979. addAltDropDown('#stroke_linecap', '#linecap_opts', function() {
  2980. setStrokeOpt(this, true);
  2981. }, {dropUp: true});
  2982. addAltDropDown('#stroke_linejoin', '#linejoin_opts', function() {
  2983. setStrokeOpt(this, true);
  2984. }, {dropUp: true});
  2985. addAltDropDown('#tool_position', '#position_opts', function() {
  2986. var letter = this.id.replace('tool_pos', '').charAt(0);
  2987. svgCanvas.alignSelectedElements(letter, 'page');
  2988. }, {multiclick: true});
  2989. /*
  2990. When a flyout icon is selected
  2991. (if flyout) {
  2992. - Change the icon
  2993. - Make pressing the button run its stuff
  2994. }
  2995. - Run its stuff
  2996. When its shortcut key is pressed
  2997. - If not current in list, do as above
  2998. , else:
  2999. - Just run its stuff
  3000. */
  3001. // Unfocus text input when workarea is mousedowned.
  3002. (function() {
  3003. var inp;
  3004. var unfocus = function() {
  3005. $(inp).blur();
  3006. };
  3007. $('#svg_editor').find('button, select, input:not(#text)').focus(function() {
  3008. inp = this;
  3009. ui_context = 'toolbars';
  3010. workarea.mousedown(unfocus);
  3011. }).blur(function() {
  3012. ui_context = 'canvas';
  3013. workarea.unbind('mousedown', unfocus);
  3014. // Go back to selecting text if in textedit mode
  3015. if (svgCanvas.getMode() == 'textedit') {
  3016. $('#text').focus();
  3017. }
  3018. });
  3019. }());
  3020. var clickFHPath = function() {
  3021. if (toolButtonClick('#tool_fhpath')) {
  3022. svgCanvas.setMode('fhpath');
  3023. }
  3024. };
  3025. var clickLine = function() {
  3026. if (toolButtonClick('#tool_line')) {
  3027. svgCanvas.setMode('line');
  3028. }
  3029. };
  3030. var clickSquare = function() {
  3031. if (toolButtonClick('#tool_square')) {
  3032. svgCanvas.setMode('square');
  3033. }
  3034. };
  3035. var clickRect = function() {
  3036. if (toolButtonClick('#tool_rect')) {
  3037. svgCanvas.setMode('rect');
  3038. }
  3039. };
  3040. var clickFHRect = function() {
  3041. if (toolButtonClick('#tool_fhrect')) {
  3042. svgCanvas.setMode('fhrect');
  3043. }
  3044. };
  3045. var clickCircle = function() {
  3046. if (toolButtonClick('#tool_circle')) {
  3047. svgCanvas.setMode('circle');
  3048. }
  3049. };
  3050. var clickEllipse = function() {
  3051. if (toolButtonClick('#tool_ellipse')) {
  3052. svgCanvas.setMode('ellipse');
  3053. }
  3054. };
  3055. var clickFHEllipse = function() {
  3056. if (toolButtonClick('#tool_fhellipse')) {
  3057. svgCanvas.setMode('fhellipse');
  3058. }
  3059. };
  3060. var clickImage = function() {
  3061. if (toolButtonClick('#tool_image')) {
  3062. svgCanvas.setMode('image');
  3063. }
  3064. };
  3065. var clickZoom = function() {
  3066. if (toolButtonClick('#tool_zoom')) {
  3067. svgCanvas.setMode('zoom');
  3068. workarea.css('cursor', zoomInIcon);
  3069. }
  3070. };
  3071. var zoomImage = function(multiplier) {
  3072. var res = svgCanvas.getResolution();
  3073. multiplier = multiplier ? res.zoom * multiplier : 1;
  3074. // setResolution(res.w * multiplier, res.h * multiplier, true);
  3075. $('#zoom').val(multiplier * 100);
  3076. svgCanvas.setZoom(multiplier);
  3077. zoomDone();
  3078. updateCanvas(true);
  3079. };
  3080. var dblclickZoom = function() {
  3081. if (toolButtonClick('#tool_zoom')) {
  3082. zoomImage();
  3083. setSelectMode();
  3084. }
  3085. };
  3086. var clickText = function() {
  3087. if (toolButtonClick('#tool_text')) {
  3088. svgCanvas.setMode('text');
  3089. }
  3090. };
  3091. var clickPath = function() {
  3092. if (toolButtonClick('#tool_path')) {
  3093. svgCanvas.setMode('path');
  3094. }
  3095. };
  3096. // Delete is a contextual tool that only appears in the ribbon if
  3097. // an element has been selected
  3098. var deleteSelected = function() {
  3099. if (selectedElement != null || multiselected) {
  3100. svgCanvas.deleteSelectedElements();
  3101. }
  3102. };
  3103. var cutSelected = function() {
  3104. if (selectedElement != null || multiselected) {
  3105. svgCanvas.cutSelectedElements();
  3106. }
  3107. };
  3108. var copySelected = function() {
  3109. if (selectedElement != null || multiselected) {
  3110. svgCanvas.copySelectedElements();
  3111. }
  3112. };
  3113. var pasteInCenter = function() {
  3114. var zoom = svgCanvas.getZoom();
  3115. var x = (workarea[0].scrollLeft + workarea.width()/2)/zoom - svgCanvas.contentW;
  3116. var y = (workarea[0].scrollTop + workarea.height()/2)/zoom - svgCanvas.contentH;
  3117. svgCanvas.pasteElements('point', x, y);
  3118. };
  3119. var moveToTopSelected = function() {
  3120. if (selectedElement != null) {
  3121. svgCanvas.moveToTopSelectedElement();
  3122. }
  3123. };
  3124. var moveToBottomSelected = function() {
  3125. if (selectedElement != null) {
  3126. svgCanvas.moveToBottomSelectedElement();
  3127. }
  3128. };
  3129. var moveUpDownSelected = function(dir) {
  3130. if (selectedElement != null) {
  3131. svgCanvas.moveUpDownSelected(dir);
  3132. }
  3133. };
  3134. var convertToPath = function() {
  3135. if (selectedElement != null) {
  3136. svgCanvas.convertToPath();
  3137. }
  3138. };
  3139. var reorientPath = function() {
  3140. if (selectedElement != null) {
  3141. path.reorient();
  3142. }
  3143. };
  3144. var makeHyperlink = function() {
  3145. if (selectedElement != null || multiselected) {
  3146. $.prompt(uiStrings.notification.enterNewLinkURL, 'http://', function(url) {
  3147. if (url) {svgCanvas.makeHyperlink(url);}
  3148. });
  3149. }
  3150. };
  3151. var moveSelected = function(dx,dy) {
  3152. if (selectedElement != null || multiselected) {
  3153. if (curConfig.gridSnapping) {
  3154. // Use grid snap value regardless of zoom level
  3155. var multi = svgCanvas.getZoom() * curConfig.snappingStep;
  3156. dx *= multi;
  3157. dy *= multi;
  3158. }
  3159. svgCanvas.moveSelectedElements(dx,dy);
  3160. }
  3161. };
  3162. var linkControlPoints = function() {
  3163. $('#tool_node_link').toggleClass('push_button_pressed tool_button');
  3164. var linked = $('#tool_node_link').hasClass('push_button_pressed');
  3165. path.linkControlPoints(linked);
  3166. };
  3167. var clonePathNode = function() {
  3168. if (path.getNodePoint()) {
  3169. path.clonePathNode();
  3170. }
  3171. };
  3172. var deletePathNode = function() {
  3173. if (path.getNodePoint()) {
  3174. path.deletePathNode();
  3175. }
  3176. };
  3177. var addSubPath = function() {
  3178. var button = $('#tool_add_subpath');
  3179. var sp = !button.hasClass('push_button_pressed');
  3180. button.toggleClass('push_button_pressed tool_button');
  3181. path.addSubPath(sp);
  3182. };
  3183. var opencloseSubPath = function() {
  3184. path.opencloseSubPath();
  3185. };
  3186. var selectNext = function() {
  3187. svgCanvas.cycleElement(1);
  3188. };
  3189. var selectPrev = function() {
  3190. svgCanvas.cycleElement(0);
  3191. };
  3192. var rotateSelected = function(cw, step) {
  3193. if (selectedElement == null || multiselected) {return;}
  3194. if (!cw) {step *= -1;}
  3195. var angle = parseFloat($('#angle').val()) + step;
  3196. svgCanvas.setRotationAngle(angle);
  3197. updateContextPanel();
  3198. };
  3199. var clickClear = function() {
  3200. var dims = curConfig.dimensions;
  3201. $.confirm(uiStrings.notification.QwantToClear, function(ok) {
  3202. if (!ok) {return;}
  3203. setSelectMode();
  3204. svgCanvas.clear();
  3205. svgCanvas.setResolution(dims[0], dims[1]);
  3206. updateCanvas(true);
  3207. zoomImage();
  3208. populateLayers();
  3209. updateContextPanel();
  3210. prepPaints();
  3211. svgCanvas.runExtensions('onNewDocument');
  3212. });
  3213. };
  3214. var clickBold = function() {
  3215. svgCanvas.setBold( !svgCanvas.getBold() );
  3216. updateContextPanel();
  3217. return false;
  3218. };
  3219. var clickItalic = function() {
  3220. svgCanvas.setItalic( !svgCanvas.getItalic() );
  3221. updateContextPanel();
  3222. return false;
  3223. };
  3224. var clickSave = function() {
  3225. // In the future, more options can be provided here
  3226. var saveOpts = {
  3227. 'images': $.pref('img_save'),
  3228. 'round_digits': 6
  3229. };
  3230. svgCanvas.save(saveOpts);
  3231. };
  3232. var clickExport = function() {
  3233. $.select('Select an image type for export: ', [
  3234. // See http://kangax.github.io/jstests/toDataUrl_mime_type_test/ for a useful list of MIME types and browser support
  3235. // 'ICO', // Todo: Find a way to preserve transparency in SVG-Edit if not working presently and do full packaging for x-icon; then switch back to position after 'PNG'
  3236. 'PNG',
  3237. 'JPEG', 'BMP', 'WEBP', 'PDF'
  3238. ], function (imgType) { // todo: replace hard-coded msg with uiStrings.notification.
  3239. if (!imgType) {
  3240. return;
  3241. }
  3242. // Open placeholder window (prevents popup)
  3243. if (!customHandlers.exportImage) {
  3244. var str = uiStrings.notification.loadingImage;
  3245. exportWindow = window.open(
  3246. 'data:text/html;charset=utf-8,' + encodeURIComponent('<title>' + str + '</title><h1>' + str + '</h1>'),
  3247. 'svg-edit-exportWindow'
  3248. );
  3249. }
  3250. var quality = parseInt($('#image-slider').val(), 10)/100;
  3251. svgCanvas.rasterExport(imgType, quality, (exportWindow && exportWindow.name));
  3252. }, function () {
  3253. var sel = $(this);
  3254. if (sel.val() === 'JPEG' || sel.val() === 'WEBP') {
  3255. if (!$('#image-slider').length) {
  3256. $('<div><label>Quality: <input id="image-slider" type="range" min="1" max="100" value="92" /></label></div>').appendTo(sel.parent()); // Todo: i18n-ize label
  3257. }
  3258. }
  3259. else {
  3260. $('#image-slider').parent().remove();
  3261. }
  3262. });
  3263. };
  3264. // by default, svgCanvas.open() is a no-op.
  3265. // it is up to an extension mechanism (opera widget, etc)
  3266. // to call setCustomHandlers() which will make it do something
  3267. var clickOpen = function() {
  3268. svgCanvas.open();
  3269. };
  3270. var clickImport = function() {
  3271. };
  3272. var clickUndo = function() {
  3273. if (undoMgr.getUndoStackSize() > 0) {
  3274. undoMgr.undo();
  3275. populateLayers();
  3276. }
  3277. };
  3278. var clickRedo = function() {
  3279. if (undoMgr.getRedoStackSize() > 0) {
  3280. undoMgr.redo();
  3281. populateLayers();
  3282. }
  3283. };
  3284. var clickGroup = function() {
  3285. // group
  3286. if (multiselected) {
  3287. svgCanvas.groupSelectedElements();
  3288. }
  3289. // ungroup
  3290. else if (selectedElement) {
  3291. svgCanvas.ungroupSelectedElement();
  3292. }
  3293. };
  3294. var clickClone = function() {
  3295. svgCanvas.cloneSelectedElements(20, 20);
  3296. };
  3297. var clickAlign = function() {
  3298. var letter = this.id.replace('tool_align', '').charAt(0);
  3299. svgCanvas.alignSelectedElements(letter, $('#align_relative_to').val());
  3300. };
  3301. var clickWireframe = function() {
  3302. $('#tool_wireframe').toggleClass('push_button_pressed tool_button');
  3303. workarea.toggleClass('wireframe');
  3304. if (supportsNonSS) {return;}
  3305. var wf_rules = $('#wireframe_rules');
  3306. if (!wf_rules.length) {
  3307. wf_rules = $('<style id="wireframe_rules"></style>').appendTo('head');
  3308. } else {
  3309. wf_rules.empty();
  3310. }
  3311. updateWireFrame();
  3312. };
  3313. $('#svg_docprops_container, #svg_prefs_container').draggable({cancel: 'button,fieldset', containment: 'window'});
  3314. var showDocProperties = function() {
  3315. if (docprops) {return;}
  3316. docprops = true;
  3317. // This selects the correct radio button by using the array notation
  3318. $('#image_save_opts input').val([$.pref('img_save')]);
  3319. // update resolution option with actual resolution
  3320. var res = svgCanvas.getResolution();
  3321. if (curConfig.baseUnit !== 'px') {
  3322. res.w = svgedit.units.convertUnit(res.w) + curConfig.baseUnit;
  3323. res.h = svgedit.units.convertUnit(res.h) + curConfig.baseUnit;
  3324. }
  3325. $('#canvas_width').val(res.w);
  3326. $('#canvas_height').val(res.h);
  3327. $('#canvas_title').val(svgCanvas.getDocumentTitle());
  3328. $('#svg_docprops').show();
  3329. };
  3330. var showPreferences = function() {
  3331. if (preferences) {return;}
  3332. preferences = true;
  3333. $('#main_menu').hide();
  3334. // Update background color with current one
  3335. var blocks = $('#bg_blocks div');
  3336. var cur_bg = 'cur_background';
  3337. var canvas_bg = curPrefs.bkgd_color;
  3338. var url = $.pref('bkgd_url');
  3339. blocks.each(function() {
  3340. var blk = $(this);
  3341. var is_bg = blk.css('background-color') == canvas_bg;
  3342. blk.toggleClass(cur_bg, is_bg);
  3343. if (is_bg) {$('#canvas_bg_url').removeClass(cur_bg);}
  3344. });
  3345. if (!canvas_bg) {blocks.eq(0).addClass(cur_bg);}
  3346. if (url) {
  3347. $('#canvas_bg_url').val(url);
  3348. }
  3349. $('#grid_snapping_on').prop('checked', curConfig.gridSnapping);
  3350. $('#grid_snapping_step').attr('value', curConfig.snappingStep);
  3351. $('#grid_color').attr('value', curConfig.gridColor);
  3352. $('#svg_prefs').show();
  3353. };
  3354. var hideSourceEditor = function() {
  3355. $('#svg_source_editor').hide();
  3356. editingsource = false;
  3357. $('#svg_source_textarea').blur();
  3358. };
  3359. var saveSourceEditor = function() {
  3360. if (!editingsource) {return;}
  3361. var saveChanges = function() {
  3362. svgCanvas.clearSelection();
  3363. hideSourceEditor();
  3364. zoomImage();
  3365. populateLayers();
  3366. updateTitle();
  3367. prepPaints();
  3368. };
  3369. if (!svgCanvas.setSvgString($('#svg_source_textarea').val())) {
  3370. $.confirm(uiStrings.notification.QerrorsRevertToSource, function(ok) {
  3371. if (!ok) {return false;}
  3372. saveChanges();
  3373. });
  3374. } else {
  3375. saveChanges();
  3376. }
  3377. setSelectMode();
  3378. };
  3379. var hideDocProperties = function() {
  3380. $('#svg_docprops').hide();
  3381. $('#canvas_width,#canvas_height').removeAttr('disabled');
  3382. $('#resolution')[0].selectedIndex = 0;
  3383. $('#image_save_opts input').val([$.pref('img_save')]);
  3384. docprops = false;
  3385. };
  3386. var hidePreferences = function() {
  3387. $('#svg_prefs').hide();
  3388. preferences = false;
  3389. };
  3390. var saveDocProperties = function() {
  3391. // set title
  3392. var newTitle = $('#canvas_title').val();
  3393. updateTitle(newTitle);
  3394. svgCanvas.setDocumentTitle(newTitle);
  3395. // update resolution
  3396. var width = $('#canvas_width'), w = width.val();
  3397. var height = $('#canvas_height'), h = height.val();
  3398. if (w != 'fit' && !svgedit.units.isValidUnit('width', w)) {
  3399. $.alert(uiStrings.notification.invalidAttrValGiven);
  3400. width.parent().addClass('error');
  3401. return false;
  3402. }
  3403. width.parent().removeClass('error');
  3404. if (h != 'fit' && !svgedit.units.isValidUnit('height', h)) {
  3405. $.alert(uiStrings.notification.invalidAttrValGiven);
  3406. height.parent().addClass('error');
  3407. return false;
  3408. }
  3409. height.parent().removeClass('error');
  3410. if (!svgCanvas.setResolution(w, h)) {
  3411. $.alert(uiStrings.notification.noContentToFitTo);
  3412. return false;
  3413. }
  3414. // Set image save option
  3415. $.pref('img_save', $('#image_save_opts :checked').val());
  3416. updateCanvas();
  3417. hideDocProperties();
  3418. };
  3419. var savePreferences = editor.savePreferences = function() {
  3420. // Set background
  3421. var color = $('#bg_blocks div.cur_background').css('background-color') || '#FFF';
  3422. setBackground(color, $('#canvas_bg_url').val());
  3423. // set language
  3424. var lang = $('#lang_select').val();
  3425. if (lang !== $.pref('lang')) {
  3426. editor.putLocale(lang, good_langs);
  3427. }
  3428. // set icon size
  3429. setIconSize($('#iconsize').val());
  3430. // set grid setting
  3431. curConfig.gridSnapping = $('#grid_snapping_on')[0].checked;
  3432. curConfig.snappingStep = $('#grid_snapping_step').val();
  3433. curConfig.gridColor = $('#grid_color').val();
  3434. curConfig.showRulers = $('#show_rulers')[0].checked;
  3435. $('#rulers').toggle(curConfig.showRulers);
  3436. if (curConfig.showRulers) {updateRulers();}
  3437. curConfig.baseUnit = $('#base_unit').val();
  3438. svgCanvas.setConfig(curConfig);
  3439. updateCanvas();
  3440. hidePreferences();
  3441. };
  3442. var resetScrollPos = $.noop;
  3443. var cancelOverlays = function() {
  3444. $('#dialog_box').hide();
  3445. if (!editingsource && !docprops && !preferences) {
  3446. if (cur_context) {
  3447. svgCanvas.leaveContext();
  3448. }
  3449. return;
  3450. }
  3451. if (editingsource) {
  3452. if (origSource !== $('#svg_source_textarea').val()) {
  3453. $.confirm(uiStrings.notification.QignoreSourceChanges, function(ok) {
  3454. if (ok) {hideSourceEditor();}
  3455. });
  3456. } else {
  3457. hideSourceEditor();
  3458. }
  3459. } else if (docprops) {
  3460. hideDocProperties();
  3461. } else if (preferences) {
  3462. hidePreferences();
  3463. }
  3464. resetScrollPos();
  3465. };
  3466. var win_wh = {width:$(window).width(), height:$(window).height()};
  3467. // Fix for Issue 781: Drawing area jumps to top-left corner on window resize (IE9)
  3468. if (svgedit.browser.isIE()) {
  3469. (function() {
  3470. resetScrollPos = function() {
  3471. if (workarea[0].scrollLeft === 0 && workarea[0].scrollTop === 0) {
  3472. workarea[0].scrollLeft = curScrollPos.left;
  3473. workarea[0].scrollTop = curScrollPos.top;
  3474. }
  3475. };
  3476. curScrollPos = {
  3477. left: workarea[0].scrollLeft,
  3478. top: workarea[0].scrollTop
  3479. };
  3480. $(window).resize(resetScrollPos);
  3481. editor.ready(function() {
  3482. // TODO: Find better way to detect when to do this to minimize
  3483. // flickering effect
  3484. setTimeout(function() {
  3485. resetScrollPos();
  3486. }, 500);
  3487. });
  3488. workarea.scroll(function() {
  3489. curScrollPos = {
  3490. left: workarea[0].scrollLeft,
  3491. top: workarea[0].scrollTop
  3492. };
  3493. });
  3494. }());
  3495. }
  3496. $(window).resize(function(evt) {
  3497. $.each(win_wh, function(type, val) {
  3498. var curval = $(window)[type]();
  3499. workarea[0]['scroll' + (type === 'width' ? 'Left' : 'Top')] -= (curval - val)/2;
  3500. win_wh[type] = curval;
  3501. });
  3502. setFlyoutPositions();
  3503. });
  3504. (function() {
  3505. workarea.scroll(function() {
  3506. // TODO: jQuery's scrollLeft/Top() wouldn't require a null check
  3507. if ($('#ruler_x').length != 0) {
  3508. $('#ruler_x')[0].scrollLeft = workarea[0].scrollLeft;
  3509. }
  3510. if ($('#ruler_y').length != 0) {
  3511. $('#ruler_y')[0].scrollTop = workarea[0].scrollTop;
  3512. }
  3513. });
  3514. }());
  3515. $('#url_notice').click(function() {
  3516. $.alert(this.title);
  3517. });
  3518. $('#change_image_url').click(promptImgURL);
  3519. // added these event handlers for all the push buttons so they
  3520. // behave more like buttons being pressed-in and not images
  3521. (function() {
  3522. var toolnames = ['clear', 'open', 'save', 'source', 'delete', 'delete_multi', 'paste', 'clone', 'clone_multi', 'move_top', 'move_bottom'];
  3523. var all_tools = '';
  3524. var cur_class = 'tool_button_current';
  3525. $.each(toolnames, function(i,item) {
  3526. all_tools += '#tool_' + item + (i == toolnames.length-1 ? ',' : '');
  3527. });
  3528. $(all_tools).mousedown(function() {
  3529. $(this).addClass(cur_class);
  3530. }).bind('mousedown mouseout', function() {
  3531. $(this).removeClass(cur_class);
  3532. });
  3533. $('#tool_undo, #tool_redo').mousedown(function() {
  3534. if (!$(this).hasClass('disabled')) {$(this).addClass(cur_class);}
  3535. }).bind('mousedown mouseout',function() {
  3536. $(this).removeClass(cur_class);}
  3537. );
  3538. }());
  3539. // switch modifier key in tooltips if mac
  3540. // NOTE: This code is not used yet until I can figure out how to successfully bind ctrl/meta
  3541. // in Opera and Chrome
  3542. if (svgedit.browser.isMac() && !window.opera) {
  3543. var shortcutButtons = ['tool_clear', 'tool_save', 'tool_source', 'tool_undo', 'tool_redo', 'tool_clone'];
  3544. i = shortcutButtons.length;
  3545. while (i--) {
  3546. var button = document.getElementById(shortcutButtons[i]);
  3547. if (button) {
  3548. var title = button.title;
  3549. var index = title.indexOf('Ctrl+');
  3550. button.title = [title.substr(0, index), 'Cmd+', title.substr(index + 5)].join('');
  3551. }
  3552. }
  3553. }
  3554. // TODO: go back to the color boxes having white background-color and then setting
  3555. // background-image to none.png (otherwise partially transparent gradients look weird)
  3556. var colorPicker = function(elem) {
  3557. var picker = elem.attr('id') == 'stroke_color' ? 'stroke' : 'fill';
  3558. // var opacity = (picker == 'stroke' ? $('#stroke_opacity') : $('#fill_opacity'));
  3559. var paint = paintBox[picker].paint;
  3560. var title = (picker == 'stroke' ? 'Pick a Stroke Paint and Opacity' : 'Pick a Fill Paint and Opacity');
  3561. // var was_none = false; // Currently unused
  3562. var pos = elem.offset();
  3563. $('#color_picker')
  3564. .draggable({cancel: '.jGraduate_tabs, .jGraduate_colPick, .jGraduate_gradPick, .jPicker', containment: 'window'})
  3565. .css(curConfig.colorPickerCSS || {'left': pos.left - 140, 'bottom': 40})
  3566. .jGraduate(
  3567. {
  3568. paint: paint,
  3569. window: { pickerTitle: title },
  3570. images: { clientPath: curConfig.jGraduatePath },
  3571. newstop: 'inverse'
  3572. },
  3573. function(p) {
  3574. paint = new $.jGraduate.Paint(p);
  3575. paintBox[picker].setPaint(paint);
  3576. svgCanvas.setPaint(picker, paint);
  3577. $('#color_picker').hide();
  3578. },
  3579. function() {
  3580. $('#color_picker').hide();
  3581. });
  3582. };
  3583. var PaintBox = function(container, type) {
  3584. var paintColor, paintOpacity,
  3585. cur = curConfig[type === 'fill' ? 'initFill' : 'initStroke'];
  3586. // set up gradients to be used for the buttons
  3587. var svgdocbox = new DOMParser().parseFromString(
  3588. '<svg xmlns="http://www.w3.org/2000/svg"><rect width="16.5" height="16.5"'+
  3589. ' fill="#' + cur.color + '" opacity="' + cur.opacity + '"/>'+
  3590. ' <defs><linearGradient id="gradbox_"/></defs></svg>', 'text/xml');
  3591. var docElem = svgdocbox.documentElement;
  3592. docElem = $(container)[0].appendChild(document.importNode(docElem, true));
  3593. docElem.setAttribute('width',16.5);
  3594. this.rect = docElem.firstChild;
  3595. this.defs = docElem.getElementsByTagName('defs')[0];
  3596. this.grad = this.defs.firstChild;
  3597. this.paint = new $.jGraduate.Paint({solidColor: cur.color});
  3598. this.type = type;
  3599. this.setPaint = function(paint, apply) {
  3600. this.paint = paint;
  3601. var fillAttr = 'none';
  3602. var ptype = paint.type;
  3603. var opac = paint.alpha / 100;
  3604. switch ( ptype ) {
  3605. case 'solidColor':
  3606. fillAttr = (paint[ptype] != 'none') ? '#' + paint[ptype] : paint[ptype];
  3607. break;
  3608. case 'linearGradient':
  3609. case 'radialGradient':
  3610. this.defs.removeChild(this.grad);
  3611. this.grad = this.defs.appendChild(paint[ptype]);
  3612. var id = this.grad.id = 'gradbox_' + this.type;
  3613. fillAttr = 'url(#' + id + ')';
  3614. break;
  3615. }
  3616. this.rect.setAttribute('fill', fillAttr);
  3617. this.rect.setAttribute('opacity', opac);
  3618. if (apply) {
  3619. svgCanvas.setColor(this.type, paintColor, true);
  3620. svgCanvas.setPaintOpacity(this.type, paintOpacity, true);
  3621. }
  3622. };
  3623. this.update = function(apply) {
  3624. if (!selectedElement) {return;}
  3625. var i, len;
  3626. var type = this.type;
  3627. switch (selectedElement.tagName) {
  3628. case 'use':
  3629. case 'image':
  3630. case 'foreignObject':
  3631. // These elements don't have fill or stroke, so don't change
  3632. // the current value
  3633. return;
  3634. case 'g':
  3635. case 'a':
  3636. var gPaint = null;
  3637. var childs = selectedElement.getElementsByTagName('*');
  3638. for (i = 0, len = childs.length; i < len; i++) {
  3639. var elem = childs[i];
  3640. var p = elem.getAttribute(type);
  3641. if (i === 0) {
  3642. gPaint = p;
  3643. } else if (gPaint !== p) {
  3644. gPaint = null;
  3645. break;
  3646. }
  3647. }
  3648. if (gPaint === null) {
  3649. // No common color, don't update anything
  3650. paintColor = null;
  3651. return;
  3652. }
  3653. paintColor = gPaint;
  3654. paintOpacity = 1;
  3655. break;
  3656. default:
  3657. paintOpacity = parseFloat(selectedElement.getAttribute(type + '-opacity'));
  3658. if (isNaN(paintOpacity)) {
  3659. paintOpacity = 1.0;
  3660. }
  3661. var defColor = type === 'fill' ? 'black' : 'none';
  3662. paintColor = selectedElement.getAttribute(type) || defColor;
  3663. }
  3664. if (apply) {
  3665. svgCanvas.setColor(type, paintColor, true);
  3666. svgCanvas.setPaintOpacity(type, paintOpacity, true);
  3667. }
  3668. paintOpacity *= 100;
  3669. var paint = getPaint(paintColor, paintOpacity, type);
  3670. // update the rect inside #fill_color/#stroke_color
  3671. this.setPaint(paint);
  3672. };
  3673. this.prep = function() {
  3674. var ptype = this.paint.type;
  3675. switch ( ptype ) {
  3676. case 'linearGradient':
  3677. case 'radialGradient':
  3678. var paint = new $.jGraduate.Paint({copy: this.paint});
  3679. svgCanvas.setPaint(type, paint);
  3680. break;
  3681. }
  3682. };
  3683. };
  3684. paintBox.fill = new PaintBox('#fill_color', 'fill');
  3685. paintBox.stroke = new PaintBox('#stroke_color', 'stroke');
  3686. $('#stroke_width').val(curConfig.initStroke.width);
  3687. $('#group_opacity').val(curConfig.initOpacity * 100);
  3688. // Use this SVG elem to test vectorEffect support
  3689. var testEl = paintBox.fill.rect.cloneNode(false);
  3690. testEl.setAttribute('style', 'vector-effect:non-scaling-stroke');
  3691. supportsNonSS = (testEl.style.vectorEffect === 'non-scaling-stroke');
  3692. testEl.removeAttribute('style');
  3693. var svgdocbox = paintBox.fill.rect.ownerDocument;
  3694. // Use this to test support for blur element. Seems to work to test support in Webkit
  3695. var blurTest = svgdocbox.createElementNS(svgedit.NS.SVG, 'feGaussianBlur');
  3696. if (blurTest.stdDeviationX === undefined) {
  3697. $('#tool_blur').hide();
  3698. }
  3699. $(blurTest).remove();
  3700. // Test for zoom icon support
  3701. (function() {
  3702. var pre = '-' + uaPrefix.toLowerCase() + '-zoom-';
  3703. var zoom = pre + 'in';
  3704. workarea.css('cursor', zoom);
  3705. if (workarea.css('cursor') === zoom) {
  3706. zoomInIcon = zoom;
  3707. zoomOutIcon = pre + 'out';
  3708. }
  3709. workarea.css('cursor', 'auto');
  3710. }());
  3711. // Test for embedImage support (use timeout to not interfere with page load)
  3712. setTimeout(function() {
  3713. svgCanvas.embedImage('images/logo.png', function(datauri) {
  3714. if (!datauri) {
  3715. // Disable option
  3716. $('#image_save_opts [value=embed]').attr('disabled', 'disabled');
  3717. $('#image_save_opts input').val(['ref']);
  3718. $.pref('img_save', 'ref');
  3719. $('#image_opt_embed').css('color', '#666').attr('title', uiStrings.notification.featNotSupported);
  3720. }
  3721. });
  3722. }, 1000);
  3723. $('#fill_color, #tool_fill .icon_label').click(function() {
  3724. colorPicker($('#fill_color'));
  3725. updateToolButtonState();
  3726. });
  3727. $('#stroke_color, #tool_stroke .icon_label').click(function() {
  3728. colorPicker($('#stroke_color'));
  3729. updateToolButtonState();
  3730. });
  3731. $('#group_opacityLabel').click(function() {
  3732. $('#opacity_dropdown button').mousedown();
  3733. $(window).mouseup();
  3734. });
  3735. $('#zoomLabel').click(function() {
  3736. $('#zoom_dropdown button').mousedown();
  3737. $(window).mouseup();
  3738. });
  3739. $('#tool_move_top').mousedown(function(evt) {
  3740. $('#tools_stacking').show();
  3741. evt.preventDefault();
  3742. });
  3743. $('.layer_button').mousedown(function() {
  3744. $(this).addClass('layer_buttonpressed');
  3745. }).mouseout(function() {
  3746. $(this).removeClass('layer_buttonpressed');
  3747. }).mouseup(function() {
  3748. $(this).removeClass('layer_buttonpressed');
  3749. });
  3750. $('.push_button').mousedown(function() {
  3751. if (!$(this).hasClass('disabled')) {
  3752. $(this).addClass('push_button_pressed').removeClass('push_button');
  3753. }
  3754. }).mouseout(function() {
  3755. $(this).removeClass('push_button_pressed').addClass('push_button');
  3756. }).mouseup(function() {
  3757. $(this).removeClass('push_button_pressed').addClass('push_button');
  3758. });
  3759. // ask for a layer name
  3760. $('#layer_new').click(function() {
  3761. var uniqName,
  3762. i = svgCanvas.getCurrentDrawing().getNumLayers();
  3763. do {
  3764. uniqName = uiStrings.layers.layer + ' ' + (++i);
  3765. } while(svgCanvas.getCurrentDrawing().hasLayer(uniqName));
  3766. $.prompt(uiStrings.notification.enterUniqueLayerName, uniqName, function(newName) {
  3767. if (!newName) {return;}
  3768. if (svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3769. $.alert(uiStrings.notification.dupeLayerName);
  3770. return;
  3771. }
  3772. svgCanvas.createLayer(newName);
  3773. updateContextPanel();
  3774. populateLayers();
  3775. });
  3776. });
  3777. function deleteLayer() {
  3778. if (svgCanvas.deleteCurrentLayer()) {
  3779. updateContextPanel();
  3780. populateLayers();
  3781. // This matches what SvgCanvas does
  3782. // TODO: make this behavior less brittle (svg-editor should get which
  3783. // layer is selected from the canvas and then select that one in the UI)
  3784. $('#layerlist tr.layer').removeClass('layersel');
  3785. $('#layerlist tr.layer:first').addClass('layersel');
  3786. }
  3787. }
  3788. function cloneLayer() {
  3789. var name = svgCanvas.getCurrentDrawing().getCurrentLayerName() + ' copy';
  3790. $.prompt(uiStrings.notification.enterUniqueLayerName, name, function(newName) {
  3791. if (!newName) {return;}
  3792. if (svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3793. $.alert(uiStrings.notification.dupeLayerName);
  3794. return;
  3795. }
  3796. svgCanvas.cloneLayer(newName);
  3797. updateContextPanel();
  3798. populateLayers();
  3799. });
  3800. }
  3801. function mergeLayer() {
  3802. if ($('#layerlist tr.layersel').index() == svgCanvas.getCurrentDrawing().getNumLayers()-1) {
  3803. return;
  3804. }
  3805. svgCanvas.mergeLayer();
  3806. updateContextPanel();
  3807. populateLayers();
  3808. }
  3809. function moveLayer(pos) {
  3810. var curIndex = $('#layerlist tr.layersel').index();
  3811. var total = svgCanvas.getCurrentDrawing().getNumLayers();
  3812. if (curIndex > 0 || curIndex < total-1) {
  3813. curIndex += pos;
  3814. svgCanvas.setCurrentLayerPosition(total-curIndex-1);
  3815. populateLayers();
  3816. }
  3817. }
  3818. $('#layer_delete').click(deleteLayer);
  3819. $('#layer_up').click(function() {
  3820. moveLayer(-1);
  3821. });
  3822. $('#layer_down').click(function() {
  3823. moveLayer(1);
  3824. });
  3825. $('#layer_rename').click(function() {
  3826. // var curIndex = $('#layerlist tr.layersel').prevAll().length; // Currently unused
  3827. var oldName = $('#layerlist tr.layersel td.layername').text();
  3828. $.prompt(uiStrings.notification.enterNewLayerName, '', function(newName) {
  3829. if (!newName) {return;}
  3830. if (oldName == newName || svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3831. $.alert(uiStrings.notification.layerHasThatName);
  3832. return;
  3833. }
  3834. svgCanvas.renameCurrentLayer(newName);
  3835. populateLayers();
  3836. });
  3837. });
  3838. var SIDEPANEL_MAXWIDTH = 300;
  3839. var SIDEPANEL_OPENWIDTH = 150;
  3840. var sidedrag = -1, sidedragging = false, allowmove = false;
  3841. var changeSidePanelWidth = function(delta) {
  3842. var rulerX = $('#ruler_x');
  3843. $('#sidepanels').width('+=' + delta);
  3844. $('#layerpanel').width('+=' + delta);
  3845. rulerX.css('right', parseInt(rulerX.css('right'), 10) + delta);
  3846. workarea.css('right', parseInt(workarea.css('right'), 10) + delta);
  3847. svgCanvas.runExtensions('workareaResized');
  3848. };
  3849. var resizeSidePanel = function(evt) {
  3850. if (!allowmove) {return;}
  3851. if (sidedrag == -1) {return;}
  3852. sidedragging = true;
  3853. var deltaX = sidedrag - evt.pageX;
  3854. var sideWidth = $('#sidepanels').width();
  3855. if (sideWidth + deltaX > SIDEPANEL_MAXWIDTH) {
  3856. deltaX = SIDEPANEL_MAXWIDTH - sideWidth;
  3857. sideWidth = SIDEPANEL_MAXWIDTH;
  3858. } else if (sideWidth + deltaX < 2) {
  3859. deltaX = 2 - sideWidth;
  3860. sideWidth = 2;
  3861. }
  3862. if (deltaX == 0) {return;}
  3863. sidedrag -= deltaX;
  3864. changeSidePanelWidth(deltaX);
  3865. };
  3866. // if width is non-zero, then fully close it, otherwise fully open it
  3867. // the optional close argument forces the side panel closed
  3868. var toggleSidePanel = function(close) {
  3869. var w = $('#sidepanels').width();
  3870. var deltaX = (w > 2 || close ? 2 : SIDEPANEL_OPENWIDTH) - w;
  3871. changeSidePanelWidth(deltaX);
  3872. };
  3873. $('#sidepanel_handle')
  3874. .mousedown(function(evt) {
  3875. sidedrag = evt.pageX;
  3876. $(window).mousemove(resizeSidePanel);
  3877. allowmove = false;
  3878. // Silly hack for Chrome, which always runs mousemove right after mousedown
  3879. setTimeout(function() {
  3880. allowmove = true;
  3881. }, 20);
  3882. })
  3883. .mouseup(function(evt) {
  3884. if (!sidedragging) {toggleSidePanel();}
  3885. sidedrag = -1;
  3886. sidedragging = false;
  3887. });
  3888. $(window).mouseup(function() {
  3889. sidedrag = -1;
  3890. sidedragging = false;
  3891. $('#svg_editor').unbind('mousemove', resizeSidePanel);
  3892. });
  3893. populateLayers();
  3894. // function changeResolution(x,y) {
  3895. // var zoom = svgCanvas.getResolution().zoom;
  3896. // setResolution(x * zoom, y * zoom);
  3897. // }
  3898. var centerCanvas = function() {
  3899. // this centers the canvas vertically in the workarea (horizontal handled in CSS)
  3900. workarea.css('line-height', workarea.height() + 'px');
  3901. };
  3902. $(window).bind('load resize', centerCanvas);
  3903. function stepFontSize(elem, step) {
  3904. var orig_val = Number(elem.value);
  3905. var sug_val = orig_val + step;
  3906. var increasing = sug_val >= orig_val;
  3907. if (step === 0) {return orig_val;}
  3908. if (orig_val >= 24) {
  3909. if (increasing) {
  3910. return Math.round(orig_val * 1.1);
  3911. }
  3912. return Math.round(orig_val / 1.1);
  3913. }
  3914. if (orig_val <= 1) {
  3915. if (increasing) {
  3916. return orig_val * 2;
  3917. }
  3918. return orig_val / 2;
  3919. }
  3920. return sug_val;
  3921. }
  3922. function stepZoom(elem, step) {
  3923. var orig_val = Number(elem.value);
  3924. if (orig_val === 0) {return 100;}
  3925. var sug_val = orig_val + step;
  3926. if (step === 0) {return orig_val;}
  3927. if (orig_val >= 100) {
  3928. return sug_val;
  3929. }
  3930. if (sug_val >= orig_val) {
  3931. return orig_val * 2;
  3932. }
  3933. return orig_val / 2;
  3934. }
  3935. // function setResolution(w, h, center) {
  3936. // updateCanvas();
  3937. // // w-=0; h-=0;
  3938. // // $('#svgcanvas').css( { 'width': w, 'height': h } );
  3939. // // $('#canvas_width').val(w);
  3940. // // $('#canvas_height').val(h);
  3941. // //
  3942. // // if (center) {
  3943. // // var w_area = workarea;
  3944. // // var scroll_y = h/2 - w_area.height()/2;
  3945. // // var scroll_x = w/2 - w_area.width()/2;
  3946. // // w_area[0].scrollTop = scroll_y;
  3947. // // w_area[0].scrollLeft = scroll_x;
  3948. // // }
  3949. // }
  3950. $('#resolution').change(function() {
  3951. var wh = $('#canvas_width,#canvas_height');
  3952. if (!this.selectedIndex) {
  3953. if ($('#canvas_width').val() == 'fit') {
  3954. wh.removeAttr('disabled').val(100);
  3955. }
  3956. } else if (this.value == 'content') {
  3957. wh.val('fit').attr('disabled', 'disabled');
  3958. } else {
  3959. var dims = this.value.split('x');
  3960. $('#canvas_width').val(dims[0]);
  3961. $('#canvas_height').val(dims[1]);
  3962. wh.removeAttr('disabled');
  3963. }
  3964. });
  3965. //Prevent browser from erroneously repopulating fields
  3966. $('input,select').attr('autocomplete', 'off');
  3967. // Associate all button actions as well as non-button keyboard shortcuts
  3968. Actions = (function() {
  3969. // sel:'selector', fn:function, evt:'event', key:[key, preventDefault, NoDisableInInput]
  3970. var tool_buttons = [
  3971. {sel: '#tool_select', fn: clickSelect, evt: 'click', key: ['V', true]},
  3972. {sel: '#tool_fhpath', fn: clickFHPath, evt: 'click', key: ['Q', true]},
  3973. {sel: '#tool_line', fn: clickLine, evt: 'click', key: ['L', true]},
  3974. {sel: '#tool_rect', fn: clickRect, evt: 'mouseup', key: ['R', true], parent: '#tools_rect', icon: 'rect'},
  3975. {sel: '#tool_square', fn: clickSquare, evt: 'mouseup', parent: '#tools_rect', icon: 'square'},
  3976. {sel: '#tool_fhrect', fn: clickFHRect, evt: 'mouseup', parent: '#tools_rect', icon: 'fh_rect'},
  3977. {sel: '#tool_ellipse', fn: clickEllipse, evt: 'mouseup', key: ['E', true], parent: '#tools_ellipse', icon: 'ellipse'},
  3978. {sel: '#tool_circle', fn: clickCircle, evt: 'mouseup', parent: '#tools_ellipse', icon: 'circle'},
  3979. {sel: '#tool_fhellipse', fn: clickFHEllipse, evt: 'mouseup', parent: '#tools_ellipse', icon: 'fh_ellipse'},
  3980. {sel: '#tool_path', fn: clickPath, evt: 'click', key: ['P', true]},
  3981. {sel: '#tool_text', fn: clickText, evt: 'click', key: ['T', true]},
  3982. {sel: '#tool_image', fn: clickImage, evt: 'mouseup'},
  3983. {sel: '#tool_zoom', fn: clickZoom, evt: 'mouseup', key: ['Z', true]},
  3984. {sel: '#tool_clear', fn: clickClear, evt: 'mouseup', key: ['N', true]},
  3985. {sel: '#tool_save', fn: function() {
  3986. if (editingsource) {
  3987. saveSourceEditor();
  3988. }
  3989. else {
  3990. clickSave();
  3991. }
  3992. }, evt: 'mouseup', key: ['S', true]},
  3993. {sel: '#tool_export', fn: clickExport, evt: 'mouseup'},
  3994. {sel: '#tool_open', fn: clickOpen, evt: 'mouseup', key: ['O', true]},
  3995. {sel: '#tool_import', fn: clickImport, evt: 'mouseup'},
  3996. {sel: '#tool_source', fn: showSourceEditor, evt: 'click', key: ['U', true]},
  3997. {sel: '#tool_wireframe', fn: clickWireframe, evt: 'click', key: ['F', true]},
  3998. {sel: '#tool_source_cancel,.overlay,#tool_docprops_cancel,#tool_prefs_cancel', fn: cancelOverlays, evt: 'click', key: ['esc', false, false], hidekey: true},
  3999. {sel: '#tool_source_save', fn: saveSourceEditor, evt: 'click'},
  4000. {sel: '#tool_docprops_save', fn: saveDocProperties, evt: 'click'},
  4001. {sel: '#tool_docprops', fn: showDocProperties, evt: 'mouseup'},
  4002. {sel: '#tool_prefs_save', fn: savePreferences, evt: 'click'},
  4003. {sel: '#tool_prefs_option', fn: function() {showPreferences(); return false;}, evt: 'mouseup'},
  4004. {sel: '#tool_delete,#tool_delete_multi', fn: deleteSelected, evt: 'click', key: ['del/backspace', true]},
  4005. {sel: '#tool_reorient', fn: reorientPath, evt: 'click'},
  4006. {sel: '#tool_node_link', fn: linkControlPoints, evt: 'click'},
  4007. {sel: '#tool_node_clone', fn: clonePathNode, evt: 'click'},
  4008. {sel: '#tool_node_delete', fn: deletePathNode, evt: 'click'},
  4009. {sel: '#tool_openclose_path', fn: opencloseSubPath, evt: 'click'},
  4010. {sel: '#tool_add_subpath', fn: addSubPath, evt: 'click'},
  4011. {sel: '#tool_move_top', fn: moveToTopSelected, evt: 'click', key: 'ctrl+shift+]'},
  4012. {sel: '#tool_move_bottom', fn: moveToBottomSelected, evt: 'click', key: 'ctrl+shift+['},
  4013. {sel: '#tool_topath', fn: convertToPath, evt: 'click'},
  4014. {sel: '#tool_make_link,#tool_make_link_multi', fn: makeHyperlink, evt: 'click'},
  4015. {sel: '#tool_undo', fn: clickUndo, evt: 'click', key: ['Z', true]},
  4016. {sel: '#tool_redo', fn: clickRedo, evt: 'click', key: ['Y', true]},
  4017. {sel: '#tool_clone,#tool_clone_multi', fn: clickClone, evt: 'click', key: ['D', true]},
  4018. {sel: '#tool_group_elements', fn: clickGroup, evt: 'click', key: ['G', true]},
  4019. {sel: '#tool_ungroup', fn: clickGroup, evt: 'click'},
  4020. {sel: '#tool_unlink_use', fn: clickGroup, evt: 'click'},
  4021. {sel: '[id^=tool_align]', fn: clickAlign, evt: 'click'},
  4022. // these two lines are required to make Opera work properly with the flyout mechanism
  4023. // {sel: '#tools_rect_show', fn: clickRect, evt: 'click'},
  4024. // {sel: '#tools_ellipse_show', fn: clickEllipse, evt: 'click'},
  4025. {sel: '#tool_bold', fn: clickBold, evt: 'mousedown'},
  4026. {sel: '#tool_italic', fn: clickItalic, evt: 'mousedown'},
  4027. {sel: '#sidepanel_handle', fn: toggleSidePanel, key: ['X']},
  4028. {sel: '#copy_save_done', fn: cancelOverlays, evt: 'click'},
  4029. // Shortcuts not associated with buttons
  4030. {key: 'ctrl+left', fn: function(){rotateSelected(0,1);}},
  4031. {key: 'ctrl+right', fn: function(){rotateSelected(1,1);}},
  4032. {key: 'ctrl+shift+left', fn: function(){rotateSelected(0,5);}},
  4033. {key: 'ctrl+shift+right', fn: function(){rotateSelected(1,5);}},
  4034. {key: 'shift+O', fn: selectPrev},
  4035. {key: 'shift+P', fn: selectNext},
  4036. {key: [modKey+'up', true], fn: function(){zoomImage(2);}},
  4037. {key: [modKey+'down', true], fn: function(){zoomImage(0.5);}},
  4038. {key: [modKey+']', true], fn: function(){moveUpDownSelected('Up');}},
  4039. {key: [modKey+'[', true], fn: function(){moveUpDownSelected('Down');}},
  4040. {key: ['up', true], fn: function(){moveSelected(0,-1);}},
  4041. {key: ['down', true], fn: function(){moveSelected(0,1);}},
  4042. {key: ['left', true], fn: function(){moveSelected(-1,0);}},
  4043. {key: ['right', true], fn: function(){moveSelected(1,0);}},
  4044. {key: 'shift+up', fn: function(){moveSelected(0,-10);}},
  4045. {key: 'shift+down', fn: function(){moveSelected(0,10);}},
  4046. {key: 'shift+left', fn: function(){moveSelected(-10,0);}},
  4047. {key: 'shift+right', fn: function(){moveSelected(10,0);}},
  4048. {key: ['alt+up', true], fn: function(){svgCanvas.cloneSelectedElements(0,-1);}},
  4049. {key: ['alt+down', true], fn: function(){svgCanvas.cloneSelectedElements(0,1);}},
  4050. {key: ['alt+left', true], fn: function(){svgCanvas.cloneSelectedElements(-1,0);}},
  4051. {key: ['alt+right', true], fn: function(){svgCanvas.cloneSelectedElements(1,0);}},
  4052. {key: ['alt+shift+up', true], fn: function(){svgCanvas.cloneSelectedElements(0,-10);}},
  4053. {key: ['alt+shift+down', true], fn: function(){svgCanvas.cloneSelectedElements(0,10);}},
  4054. {key: ['alt+shift+left', true], fn: function(){svgCanvas.cloneSelectedElements(-10,0);}},
  4055. {key: ['alt+shift+right', true], fn: function(){svgCanvas.cloneSelectedElements(10,0);}},
  4056. {key: 'A', fn: function(){svgCanvas.selectAllInCurrentLayer();}},
  4057. // Standard shortcuts
  4058. {key: modKey+'z', fn: clickUndo},
  4059. {key: modKey + 'shift+z', fn: clickRedo},
  4060. {key: modKey + 'y', fn: clickRedo},
  4061. {key: modKey+'x', fn: cutSelected},
  4062. {key: modKey+'c', fn: copySelected},
  4063. {key: modKey+'v', fn: pasteInCenter}
  4064. ];
  4065. // Tooltips not directly associated with a single function
  4066. var key_assocs = {
  4067. '4/Shift+4': '#tools_rect_show',
  4068. '5/Shift+5': '#tools_ellipse_show'
  4069. };
  4070. return {
  4071. setAll: function() {
  4072. var flyouts = {};
  4073. $.each(tool_buttons, function(i, opts) {
  4074. // Bind function to button
  4075. var btn;
  4076. if (opts.sel) {
  4077. btn = $(opts.sel);
  4078. if (btn.length == 0) {return true;} // Skip if markup does not exist
  4079. if (opts.evt) {
  4080. if (svgedit.browser.isTouch() && opts.evt === 'click') {
  4081. opts.evt = 'mousedown';
  4082. }
  4083. btn[opts.evt](opts.fn);
  4084. }
  4085. // Add to parent flyout menu, if able to be displayed
  4086. if (opts.parent && $(opts.parent + '_show').length != 0) {
  4087. var f_h = $(opts.parent);
  4088. if (!f_h.length) {
  4089. f_h = makeFlyoutHolder(opts.parent.substr(1));
  4090. }
  4091. f_h.append(btn);
  4092. if (!$.isArray(flyouts[opts.parent])) {
  4093. flyouts[opts.parent] = [];
  4094. }
  4095. flyouts[opts.parent].push(opts);
  4096. }
  4097. }
  4098. // Bind function to shortcut key
  4099. if (opts.key) {
  4100. // Set shortcut based on options
  4101. var keyval, disInInp = true, fn = opts.fn, pd = false;
  4102. if ($.isArray(opts.key)) {
  4103. keyval = opts.key[0];
  4104. if (opts.key.length > 1) {pd = opts.key[1];}
  4105. if (opts.key.length > 2) {disInInp = opts.key[2];}
  4106. } else {
  4107. keyval = opts.key;
  4108. }
  4109. keyval += '';
  4110. $.each(keyval.split('/'), function(i, key) {
  4111. $(document).bind('keydown', key, function(e) {
  4112. fn();
  4113. if (pd) {
  4114. e.preventDefault();
  4115. }
  4116. // Prevent default on ALL keys?
  4117. return false;
  4118. });
  4119. });
  4120. // Put shortcut in title
  4121. if (opts.sel && !opts.hidekey && btn.attr('title')) {
  4122. var newTitle = btn.attr('title').split('[')[0] + ' (' + keyval + ')';
  4123. key_assocs[keyval] = opts.sel;
  4124. // Disregard for menu items
  4125. if (!btn.parents('#main_menu').length) {
  4126. btn.attr('title', newTitle);
  4127. }
  4128. }
  4129. }
  4130. });
  4131. // Setup flyouts
  4132. setupFlyouts(flyouts);
  4133. // Misc additional actions
  4134. // Make 'return' keypress trigger the change event
  4135. $('.attr_changer, #image_url').bind('keydown', 'return',
  4136. function(evt) {$(this).change();evt.preventDefault();}
  4137. );
  4138. $(window).bind('keydown', 'tab', function(e) {
  4139. if (ui_context === 'canvas') {
  4140. e.preventDefault();
  4141. selectNext();
  4142. }
  4143. }).bind('keydown', 'shift+tab', function(e) {
  4144. if (ui_context === 'canvas') {
  4145. e.preventDefault();
  4146. selectPrev();
  4147. }
  4148. });
  4149. $('#tool_zoom').dblclick(dblclickZoom);
  4150. },
  4151. setTitles: function() {
  4152. $.each(key_assocs, function(keyval, sel) {
  4153. var menu = ($(sel).parents('#main_menu').length);
  4154. $(sel).each(function() {
  4155. var t;
  4156. if (menu) {
  4157. t = $(this).text().split(' [')[0];
  4158. } else {
  4159. t = this.title.split(' [')[0];
  4160. }
  4161. var key_str = '';
  4162. // Shift+Up
  4163. $.each(keyval.split('/'), function(i, key) {
  4164. var mod_bits = key.split('+'), mod = '';
  4165. if (mod_bits.length > 1) {
  4166. mod = mod_bits[0] + '+';
  4167. key = mod_bits[1];
  4168. }
  4169. key_str += (i?'/':'') + mod + (uiStrings['key_'+key] || key);
  4170. });
  4171. if (menu) {
  4172. this.lastChild.textContent = t +' ['+key_str+']';
  4173. } else {
  4174. this.title = t +' ['+key_str+']';
  4175. }
  4176. });
  4177. });
  4178. },
  4179. getButtonData: function(sel) {
  4180. var b;
  4181. $.each(tool_buttons, function(i, btn) {
  4182. if (btn.sel === sel) {b = btn;}
  4183. });
  4184. return b;
  4185. }
  4186. };
  4187. }());
  4188. Actions.setAll();
  4189. // Select given tool
  4190. editor.ready(function() {
  4191. var tool,
  4192. itool = curConfig.initTool,
  4193. container = $('#tools_left, #svg_editor .tools_flyout'),
  4194. pre_tool = container.find('#tool_' + itool),
  4195. reg_tool = container.find('#' + itool);
  4196. if (pre_tool.length) {
  4197. tool = pre_tool;
  4198. } else if (reg_tool.length) {
  4199. tool = reg_tool;
  4200. } else {
  4201. tool = $('#tool_select');
  4202. }
  4203. tool.click().mouseup();
  4204. if (curConfig.wireframe) {
  4205. $('#tool_wireframe').click();
  4206. }
  4207. if (curConfig.showlayers) {
  4208. toggleSidePanel();
  4209. }
  4210. $('#rulers').toggle(!!curConfig.showRulers);
  4211. if (curConfig.showRulers) {
  4212. $('#show_rulers')[0].checked = true;
  4213. }
  4214. if (curConfig.baseUnit) {
  4215. $('#base_unit').val(curConfig.baseUnit);
  4216. }
  4217. if (curConfig.gridSnapping) {
  4218. $('#grid_snapping_on')[0].checked = true;
  4219. }
  4220. if (curConfig.snappingStep) {
  4221. $('#grid_snapping_step').val(curConfig.snappingStep);
  4222. }
  4223. if (curConfig.gridColor) {
  4224. $('#grid_color').val(curConfig.gridColor);
  4225. }
  4226. });
  4227. // init SpinButtons
  4228. $('#rect_rx').SpinButton({ min: 0, max: 1000, callback: changeRectRadius });
  4229. $('#stroke_width').SpinButton({ min: 0, max: 99, smallStep: 0.1, callback: changeStrokeWidth });
  4230. $('#angle').SpinButton({ min: -180, max: 180, step: 5, callback: changeRotationAngle });
  4231. $('#font_size').SpinButton({ min: 0.001, stepfunc: stepFontSize, callback: changeFontSize });
  4232. $('#group_opacity').SpinButton({ min: 0, max: 100, step: 5, callback: changeOpacity });
  4233. $('#blur').SpinButton({ min: 0, max: 10, step: 0.1, callback: changeBlur });
  4234. $('#zoom').SpinButton({ min: 0.001, max: 10000, step: 50, stepfunc: stepZoom, callback: changeZoom })
  4235. // Set default zoom
  4236. .val(svgCanvas.getZoom() * 100);
  4237. $('#workarea').contextMenu({
  4238. menu: 'cmenu_canvas',
  4239. inSpeed: 0
  4240. },
  4241. function(action, el, pos) {
  4242. switch (action) {
  4243. case 'delete':
  4244. deleteSelected();
  4245. break;
  4246. case 'cut':
  4247. cutSelected();
  4248. break;
  4249. case 'copy':
  4250. copySelected();
  4251. break;
  4252. case 'paste':
  4253. svgCanvas.pasteElements();
  4254. break;
  4255. case 'paste_in_place':
  4256. svgCanvas.pasteElements('in_place');
  4257. break;
  4258. case 'group_elements':
  4259. svgCanvas.groupSelectedElements();
  4260. break;
  4261. case 'ungroup':
  4262. svgCanvas.ungroupSelectedElement();
  4263. break;
  4264. case 'move_front':
  4265. moveToTopSelected();
  4266. break;
  4267. case 'move_up':
  4268. moveUpDownSelected('Up');
  4269. break;
  4270. case 'move_down':
  4271. moveUpDownSelected('Down');
  4272. break;
  4273. case 'move_back':
  4274. moveToBottomSelected();
  4275. break;
  4276. default:
  4277. if (svgedit.contextmenu && svgedit.contextmenu.hasCustomHandler(action)) {
  4278. svgedit.contextmenu.getCustomHandler(action).call();
  4279. }
  4280. break;
  4281. }
  4282. if (svgCanvas.clipBoard.length) {
  4283. canv_menu.enableContextMenuItems('#paste,#paste_in_place');
  4284. }
  4285. }
  4286. );
  4287. var lmenu_func = function(action, el, pos) {
  4288. switch ( action ) {
  4289. case 'dupe':
  4290. cloneLayer();
  4291. break;
  4292. case 'delete':
  4293. deleteLayer();
  4294. break;
  4295. case 'merge_down':
  4296. mergeLayer();
  4297. break;
  4298. case 'merge_all':
  4299. svgCanvas.mergeAllLayers();
  4300. updateContextPanel();
  4301. populateLayers();
  4302. break;
  4303. }
  4304. };
  4305. $('#layerlist').contextMenu({
  4306. menu: 'cmenu_layers',
  4307. inSpeed: 0
  4308. },
  4309. lmenu_func
  4310. );
  4311. $('#layer_moreopts').contextMenu({
  4312. menu: 'cmenu_layers',
  4313. inSpeed: 0,
  4314. allowLeft: true
  4315. },
  4316. lmenu_func
  4317. );
  4318. $('.contextMenu li').mousedown(function(ev) {
  4319. ev.preventDefault();
  4320. });
  4321. $('#cmenu_canvas li').disableContextMenu();
  4322. canv_menu.enableContextMenuItems('#delete,#cut,#copy');
  4323. window.addEventListener('beforeunload', function(e) {
  4324. // Suppress warning if page is empty
  4325. if (undoMgr.getUndoStackSize() === 0) {
  4326. editor.showSaveWarning = false;
  4327. }
  4328. // showSaveWarning is set to 'false' when the page is saved.
  4329. if (!curConfig.no_save_warning && editor.showSaveWarning) {
  4330. // Browser already asks question about closing the page
  4331. e.returnValue = uiStrings.notification.unsavedChanges; // Firefox needs this when beforeunload set by addEventListener (even though message is not used)
  4332. return uiStrings.notification.unsavedChanges;
  4333. }
  4334. }, false);
  4335. editor.openPrep = function(func) {
  4336. $('#main_menu').hide();
  4337. if (undoMgr.getUndoStackSize() === 0) {
  4338. func(true);
  4339. } else {
  4340. $.confirm(uiStrings.notification.QwantToOpen, func);
  4341. }
  4342. };
  4343. function onDragEnter(e) {
  4344. e.stopPropagation();
  4345. e.preventDefault();
  4346. // and indicator should be displayed here, such as "drop files here"
  4347. }
  4348. function onDragOver(e) {
  4349. e.stopPropagation();
  4350. e.preventDefault();
  4351. }
  4352. function onDragLeave(e) {
  4353. e.stopPropagation();
  4354. e.preventDefault();
  4355. // hypothetical indicator should be removed here
  4356. }
  4357. // Use HTML5 File API: http://www.w3.org/TR/FileAPI/
  4358. // if browser has HTML5 File API support, then we will show the open menu item
  4359. // and provide a file input to click. When that change event fires, it will
  4360. // get the text contents of the file and send it to the canvas
  4361. if (window.FileReader) {
  4362. var importImage = function(e) {
  4363. $.process_cancel(uiStrings.notification.loadingImage);
  4364. e.stopPropagation();
  4365. e.preventDefault();
  4366. $('#workarea').removeAttr('style');
  4367. $('#main_menu').hide();
  4368. var file = (e.type == 'drop') ? e.dataTransfer.files[0] : this.files[0];
  4369. if (!file) {
  4370. return;
  4371. }
  4372. if (file.type.indexOf('image') != -1) {
  4373. // Detected an image
  4374. // svg handling
  4375. var reader;
  4376. if (file.type.indexOf('svg') != -1) {
  4377. reader = new FileReader();
  4378. reader.onloadend = function(e) {
  4379. svgCanvas.importSvgString(e.target.result, true);
  4380. svgCanvas.ungroupSelectedElement();
  4381. svgCanvas.ungroupSelectedElement();
  4382. svgCanvas.groupSelectedElements();
  4383. svgCanvas.alignSelectedElements('m', 'page');
  4384. svgCanvas.alignSelectedElements('c', 'page');
  4385. };
  4386. reader.readAsText(file);
  4387. } else {
  4388. //bitmap handling
  4389. reader = new FileReader();
  4390. reader.onloadend = function(e) {
  4391. // let's insert the new image until we know its dimensions
  4392. var insertNewImage = function(width, height) {
  4393. var newImage = svgCanvas.addSvgElementFromJson({
  4394. element: 'image',
  4395. attr: {
  4396. x: 0,
  4397. y: 0,
  4398. width: width,
  4399. height: height,
  4400. id: svgCanvas.getNextId(),
  4401. style: 'pointer-events:inherit'
  4402. }
  4403. });
  4404. svgCanvas.setHref(newImage, e.target.result);
  4405. svgCanvas.selectOnly([newImage]);
  4406. svgCanvas.alignSelectedElements('m', 'page');
  4407. svgCanvas.alignSelectedElements('c', 'page');
  4408. updateContextPanel();
  4409. };
  4410. // create dummy img so we know the default dimensions
  4411. var imgWidth = 100;
  4412. var imgHeight = 100;
  4413. var img = new Image();
  4414. img.src = e.target.result;
  4415. img.style.opacity = 0;
  4416. img.onload = function() {
  4417. imgWidth = img.offsetWidth;
  4418. imgHeight = img.offsetHeight;
  4419. insertNewImage(imgWidth, imgHeight);
  4420. };
  4421. };
  4422. reader.readAsDataURL(file);
  4423. }
  4424. }
  4425. };
  4426. workarea[0].addEventListener('dragenter', onDragEnter, false);
  4427. workarea[0].addEventListener('dragover', onDragOver, false);
  4428. workarea[0].addEventListener('dragleave', onDragLeave, false);
  4429. workarea[0].addEventListener('drop', importImage, false);
  4430. var open = $('<input type="file">').change(function() {
  4431. var f = this;
  4432. editor.openPrep(function(ok) {
  4433. if (!ok) {return;}
  4434. svgCanvas.clear();
  4435. if (f.files.length === 1) {
  4436. $.process_cancel(uiStrings.notification.loadingImage);
  4437. var reader = new FileReader();
  4438. reader.onloadend = function(e) {
  4439. loadSvgString(e.target.result);
  4440. updateCanvas();
  4441. };
  4442. reader.readAsText(f.files[0]);
  4443. }
  4444. });
  4445. });
  4446. $('#tool_open').show().prepend(open);
  4447. var imgImport = $('<input type="file">').change(importImage);
  4448. $('#tool_import').show().prepend(imgImport);
  4449. }
  4450. // $(function() {
  4451. updateCanvas(true);
  4452. // });
  4453. // var revnums = "svg-editor.js ($Rev: 2825 $) ";
  4454. // revnums += svgCanvas.getVersion();
  4455. // $('#copyright')[0].setAttribute('title', revnums);
  4456. // For Compatibility with older extensions
  4457. $(function() {
  4458. window.svgCanvas = svgCanvas;
  4459. svgCanvas.ready = editor.ready;
  4460. });
  4461. editor.setLang = function(lang, allStrings) {
  4462. editor.langChanged = true;
  4463. $.pref('lang', lang);
  4464. $('#lang_select').val(lang);
  4465. if (!allStrings) {
  4466. return;
  4467. }
  4468. // var notif = allStrings.notification; // Currently unused
  4469. // $.extend will only replace the given strings
  4470. var oldLayerName = $('#layerlist tr.layersel td.layername').text();
  4471. var rename_layer = (oldLayerName == uiStrings.common.layer + ' 1');
  4472. $.extend(uiStrings, allStrings);
  4473. svgCanvas.setUiStrings(allStrings);
  4474. Actions.setTitles();
  4475. if (rename_layer) {
  4476. svgCanvas.renameCurrentLayer(uiStrings.common.layer + ' 1');
  4477. populateLayers();
  4478. }
  4479. // In case extensions loaded before the locale, now we execute a callback on them
  4480. if (extsPreLang.length) {
  4481. while (extsPreLang.length) {
  4482. var ext = extsPreLang.shift();
  4483. ext.langReady({lang: lang, uiStrings: uiStrings});
  4484. }
  4485. }
  4486. else {
  4487. svgCanvas.runExtensions('langReady', {lang: lang, uiStrings: uiStrings});
  4488. }
  4489. svgCanvas.runExtensions('langChanged', lang);
  4490. // Update flyout tooltips
  4491. setFlyoutTitles();
  4492. // Copy title for certain tool elements
  4493. var elems = {
  4494. '#stroke_color': '#tool_stroke .icon_label, #tool_stroke .color_block',
  4495. '#fill_color': '#tool_fill label, #tool_fill .color_block',
  4496. '#linejoin_miter': '#cur_linejoin',
  4497. '#linecap_butt': '#cur_linecap'
  4498. };
  4499. $.each(elems, function(source, dest) {
  4500. $(dest).attr('title', $(source)[0].title);
  4501. });
  4502. // Copy alignment titles
  4503. $('#multiselected_panel div[id^=tool_align]').each(function() {
  4504. $('#tool_pos' + this.id.substr(10))[0].title = this.title;
  4505. });
  4506. };
  4507. };
  4508. editor.ready = function (cb) {
  4509. if (!isReady) {
  4510. callbacks.push(cb);
  4511. } else {
  4512. cb();
  4513. }
  4514. };
  4515. editor.runCallbacks = function () {
  4516. $.each(callbacks, function() {
  4517. this();
  4518. });
  4519. isReady = true;
  4520. };
  4521. editor.loadFromString = function (str) {
  4522. editor.ready(function() {
  4523. loadSvgString(str);
  4524. });
  4525. };
  4526. editor.disableUI = function (featList) {
  4527. // $(function() {
  4528. // $('#tool_wireframe, #tool_image, #main_button, #tool_source, #sidepanels').remove();
  4529. // $('#tools_top').css('left', 5);
  4530. // });
  4531. };
  4532. editor.loadFromURL = function (url, opts) {
  4533. if (!opts) {opts = {};}
  4534. var cache = opts.cache;
  4535. var cb = opts.callback;
  4536. editor.ready(function() {
  4537. $.ajax({
  4538. 'url': url,
  4539. 'dataType': 'text',
  4540. cache: !!cache,
  4541. beforeSend:function(){
  4542. $.process_cancel(uiStrings.notification.loadingImage);
  4543. },
  4544. success: function(str) {
  4545. loadSvgString(str, cb);
  4546. },
  4547. error: function(xhr, stat, err) {
  4548. if (xhr.status != 404 && xhr.responseText) {
  4549. loadSvgString(xhr.responseText, cb);
  4550. } else {
  4551. $.alert(uiStrings.notification.URLloadFail + ': \n' + err, cb);
  4552. }
  4553. },
  4554. complete:function(){
  4555. $('#dialog_box').hide();
  4556. }
  4557. });
  4558. });
  4559. };
  4560. editor.loadFromDataURI = function(str) {
  4561. editor.ready(function() {
  4562. var base64 = false;
  4563. var pre = str.match(/^data:image\/svg\+xml;base64,/);
  4564. if (pre) {
  4565. base64 = true;
  4566. }
  4567. else {
  4568. pre = str.match(/^data:image\/svg\+xml(?:;(?:utf8)?)?,/);
  4569. }
  4570. if (pre) {
  4571. pre = pre[0];
  4572. }
  4573. var src = str.slice(pre.length);
  4574. loadSvgString(base64 ? Utils.decode64(src) : decodeURIComponent(src));
  4575. });
  4576. };
  4577. editor.addExtension = function () {
  4578. var args = arguments;
  4579. // Note that we don't want this on editor.ready since some extensions
  4580. // may want to run before then (like server_opensave).
  4581. $(function() {
  4582. if (svgCanvas) {svgCanvas.addExtension.apply(this, args);}
  4583. });
  4584. };
  4585. return editor;
  4586. }(jQuery));
  4587. // Run init once DOM is loaded
  4588. $(svgEditor.init);
  4589. }());