2013-09-12 17:44:37 +10:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
|
|
|
|
|
*
|
|
|
|
|
* @licstart
|
|
|
|
|
* The JavaScript code in this page is free software: you can redistribute it
|
|
|
|
|
* and/or modify it under the terms of the GNU Affero General Public License
|
|
|
|
|
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
|
|
|
|
|
* the License, or (at your option) any later version. The code is distributed
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
|
|
|
|
|
*
|
2013-10-21 17:55:16 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
2013-09-12 17:44:37 +10:00
|
|
|
* As additional permission under GNU AGPL version 3 section 7, you
|
|
|
|
|
* may distribute non-source (e.g., minimized or compacted) forms of
|
|
|
|
|
* that code without the copy of the GNU GPL normally required by
|
|
|
|
|
* section 4, provided you include this license notice and a URL
|
|
|
|
|
* through which recipients can access the Corresponding Source.
|
|
|
|
|
*
|
|
|
|
|
* As a special exception to the AGPL, any HTML file which merely makes function
|
|
|
|
|
* calls to this code, and for that purpose includes it by reference shall be
|
|
|
|
|
* deemed a separate work for copyright law purposes. In addition, the copyright
|
|
|
|
|
* holders of this code give you permission to combine this code with free
|
|
|
|
|
* software libraries that are released under the GNU LGPL. You may copy and
|
|
|
|
|
* distribute such a system following the terms of the GNU AGPL for this code
|
|
|
|
|
* and the LGPL for the libraries. If you modify this code, you may extend this
|
|
|
|
|
* exception to your version of the code, but you are not obligated to do so.
|
|
|
|
|
* If you do not wish to do so, delete this exception statement from your
|
|
|
|
|
* version.
|
|
|
|
|
*
|
|
|
|
|
* This license applies to this entire compilation.
|
|
|
|
|
* @licend
|
|
|
|
|
* @source: http://www.webodf.org/
|
2013-10-23 18:20:30 +02:00
|
|
|
* @source: https://github.com/kogmbh/WebODF/
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*global odf, runtime, core, Node*/
|
|
|
|
|
|
|
|
|
|
runtime.loadClass("core.DomUtils");
|
|
|
|
|
runtime.loadClass("core.Utils");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper object for generating unique object names. Each name is only reported once per instance,
|
|
|
|
|
* irrespective of whether it is actually then inserted into the dom tree in the odfContainer.
|
|
|
|
|
*
|
|
|
|
|
* There is expected to be a single instance of the object name generator created per session. This is necessary
|
|
|
|
|
* to close a potential race condition when generating unique names for operations. As there is no guarantee
|
|
|
|
|
* when a given op is executed, it is insufficient to simply rely on all previously generated names to be now present
|
|
|
|
|
* in the document definitions. To cope with this, the names generated by this instance are also cached for
|
|
|
|
|
* the lifetime of this object.
|
|
|
|
|
*
|
|
|
|
|
* Failure to do this could result in a situation like the following
|
|
|
|
|
* 1. SessionController generates new OpAddStyle & adds to session's queue
|
|
|
|
|
* 2. SessionController generates another OpAddStyle & adds to session's queue
|
|
|
|
|
*
|
|
|
|
|
* At step 2, as the session's queue implementation has no requirement that it immediately executes the operation from
|
|
|
|
|
* step 1, it is likely that the style created in step 1 is not yet present in the document DOM.
|
|
|
|
|
*
|
|
|
|
|
* @param {!odf.OdfContainer} odfContainer
|
|
|
|
|
* @param {!string} memberId
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
odf.ObjectNameGenerator = function ObjectNameGenerator(odfContainer, memberId) {
|
|
|
|
|
"use strict";
|
|
|
|
|
var stylens = odf.Namespaces.stylens,
|
|
|
|
|
drawns = odf.Namespaces.drawns,
|
|
|
|
|
xlinkns = odf.Namespaces.xlinkns,
|
|
|
|
|
domUtils = new core.DomUtils(),
|
|
|
|
|
utils = new core.Utils(),
|
|
|
|
|
memberIdHash = utils.hashString(memberId),
|
|
|
|
|
styleNameGenerator = null,
|
|
|
|
|
frameNameGenerator = null,
|
|
|
|
|
imageNameGenerator = null,
|
|
|
|
|
existingFrameNames = {},
|
|
|
|
|
existingImageNames = {};
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-30 01:20:49 +01:00
|
|
|
* @param {string} prefix Prefix to use for unique name generation
|
|
|
|
|
* @param {function():!Object.<string,boolean>} findExistingNames
|
2013-09-12 17:44:37 +10:00
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
function NameGenerator(prefix, findExistingNames) {
|
2013-11-30 01:20:49 +01:00
|
|
|
var /**@type{!Object.<string,boolean>}*/
|
|
|
|
|
reportedNames = {};
|
2013-09-12 17:44:37 +10:00
|
|
|
/**
|
|
|
|
|
* Generate a unique name
|
2014-01-31 00:14:00 +01:00
|
|
|
* @return {string}
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
this.generateName = function () {
|
|
|
|
|
var existingNames = findExistingNames(),
|
|
|
|
|
startIndex = 0,
|
|
|
|
|
name;
|
|
|
|
|
do {
|
|
|
|
|
name = prefix + startIndex;
|
|
|
|
|
startIndex += 1;
|
|
|
|
|
} while (reportedNames[name] || existingNames[name]);
|
|
|
|
|
reportedNames[name] = true;
|
|
|
|
|
return name;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-30 01:20:49 +01:00
|
|
|
* Get all the style names defined in the style:style elements of the
|
|
|
|
|
* current document including automatic styles.
|
2013-09-12 17:44:37 +10:00
|
|
|
*
|
2014-01-31 00:14:00 +01:00
|
|
|
* @return {!Object.<string,boolean>}
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
function getAllStyleNames() {
|
2013-11-30 01:20:49 +01:00
|
|
|
var styleElements = [
|
|
|
|
|
odfContainer.rootElement.automaticStyles,
|
|
|
|
|
odfContainer.rootElement.styles
|
|
|
|
|
],
|
2013-09-12 17:44:37 +10:00
|
|
|
styleNames = {};
|
|
|
|
|
|
2013-11-30 01:20:49 +01:00
|
|
|
/**
|
|
|
|
|
* @param {!Element} styleListElement
|
|
|
|
|
*/
|
|
|
|
|
function getStyleNames(styleListElement) {
|
|
|
|
|
var e = styleListElement.firstElementChild;
|
|
|
|
|
while (e) {
|
|
|
|
|
if (e.namespaceURI === stylens && e.localName === "style") {
|
|
|
|
|
styleNames[e.getAttributeNS(stylens, 'name')] = true;
|
2013-09-12 17:44:37 +10:00
|
|
|
}
|
2013-11-30 01:20:49 +01:00
|
|
|
e = e.nextElementSibling;
|
2013-09-12 17:44:37 +10:00
|
|
|
}
|
2013-11-30 01:20:49 +01:00
|
|
|
}
|
|
|
|
|
styleElements.forEach(getStyleNames);
|
2013-09-12 17:44:37 +10:00
|
|
|
return styleNames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a unique style name across the style:style elements
|
2014-01-31 00:14:00 +01:00
|
|
|
* @return {!string}
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
this.generateStyleName = function () {
|
|
|
|
|
if (styleNameGenerator === null) {
|
|
|
|
|
styleNameGenerator = new NameGenerator(
|
|
|
|
|
"auto" + memberIdHash + "_",
|
|
|
|
|
function () {
|
|
|
|
|
// TODO: can cache the existing names once we fix the todo in formatting.applyStyle
|
|
|
|
|
return getAllStyleNames();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return styleNameGenerator.generateName();
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* Generate a unique frame name
|
2014-01-31 00:14:00 +01:00
|
|
|
* @return {!string}
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
this.generateFrameName = function () {
|
|
|
|
|
if (frameNameGenerator === null) {
|
|
|
|
|
var nodes = domUtils.getElementsByTagNameNS(odfContainer.rootElement.body, drawns, 'frame');
|
|
|
|
|
nodes.forEach(function (frame) {
|
|
|
|
|
existingFrameNames[frame.getAttributeNS(drawns, 'name')] = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
frameNameGenerator = new NameGenerator(
|
|
|
|
|
"fr" + memberIdHash + "_",
|
|
|
|
|
function () {
|
|
|
|
|
return existingFrameNames;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return frameNameGenerator.generateName();
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* Generate a unique image name
|
2014-01-31 00:14:00 +01:00
|
|
|
* @return {!string}
|
2013-09-12 17:44:37 +10:00
|
|
|
*/
|
|
|
|
|
this.generateImageName = function () {
|
|
|
|
|
if (imageNameGenerator === null) {
|
|
|
|
|
var nodes = domUtils.getElementsByTagNameNS(odfContainer.rootElement.body, drawns, 'image');
|
|
|
|
|
nodes.forEach(function (image) {
|
|
|
|
|
var path = image.getAttributeNS(xlinkns, 'href');
|
|
|
|
|
path = path.substring("Pictures/".length, path.lastIndexOf('.'));
|
|
|
|
|
existingImageNames[path] = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
imageNameGenerator = new NameGenerator(
|
|
|
|
|
"img" + memberIdHash + "_",
|
|
|
|
|
function () {
|
|
|
|
|
return existingImageNames;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return imageNameGenerator.generateName();
|
|
|
|
|
};
|
|
|
|
|
};
|