Author: nettings
Date: Wed Jul 25 16:59:41 2007
New Revision: 559652
URL: http://svn.apache.org/viewvc?view=rev&rev=559652
Log:
added javascript helpers for new generic editor usecases.
org.apache.lenya.editors.js is kinda the new api between generic
usecases and the editor modules.
work in progress, not active yet (need to port bxe first..)
Added:
lenya/trunk/src/modules/editors/resources/javascript/insertAsset.js
lenya/trunk/src/modules/editors/resources/javascript/insertLink.js
lenya/trunk/src/modules/editors/resources/javascript/oneform.js
lenya/trunk/src/modules/editors/resources/javascript/org.apache.lenya.editors.js
Added: lenya/trunk/src/modules/editors/resources/javascript/insertAsset.js
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/javascript/insertAsset.js?view=auto&rev=559652
==============================================================================
--- lenya/trunk/src/modules/editors/resources/javascript/insertAsset.js (added)
+++ lenya/trunk/src/modules/editors/resources/javascript/insertAsset.js Wed Jul 25 16:59:41 2007
@@ -0,0 +1,50 @@
+/**
+ * updates the hidden form data whenever the user selects a new asset radiobutton.
+ */
+function updateData(url, text, type, size, height, width) {
+ // we store the ratio with every image to allow preview re-scaling.
+ var ratio = 1;
+ if (width != 0) {
+ ratio = height / width;
+ }
+ document.forms["insertAsset"].ratio.value = ratio;
+
+ org.apache.lenya.editors.setFormValues("insertAsset", {
+ url : url,
+ text : text,
+ size : size,
+ height : height,
+ width : width,
+ type : type
+ });
+ /* alert("Setting hidden form data:\n"
+ + "url : " + url + "\n"
+ + "text : " + text + "\n"
+ + "size : " + size + "\n"
+ + "height : " + height + "\n"
+ + "width : " + width + "\n"
+ + "type : " + type);
+ */
+}
+
+/**
+ * updates the height to maintain correct ratio when the user changes the width
+ */
+function scaleHeight(width) {
+ var ratio = document.forms['insertAsset'].ratio.value;
+ document.forms['insertAsset'].height.value = width * ratio;
+ focus();
+}
+
+/**
+ * updates the width to maintain correct ratio when the user changes the height
+ */
+function scaleWidth(height) {
+ var ratio = document.forms['insertAsset'].ratio.value;
+ document.forms['insertAsset'].width.value = height * ratio;
+ focus();
+}
+
+window.onload = function() {
+ org.apache.lenya.editors.handleFormLoad("insertAsset");
+};
Added: lenya/trunk/src/modules/editors/resources/javascript/insertLink.js
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/javascript/insertLink.js?view=auto&rev=559652
==============================================================================
--- lenya/trunk/src/modules/editors/resources/javascript/insertLink.js (added)
+++ lenya/trunk/src/modules/editors/resources/javascript/insertLink.js Wed Jul 25 16:59:41 2007
@@ -0,0 +1,53 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+/* Constructor for LinkTree object */
+function LinkTree(doc, treeElement) {
+ this.doc = doc;
+ this.treeElement = treeElement;
+ this.selected = null;
+}
+
+/**
+ * FIXME: CHOSEN_LANGUAGE should not be a global variable!
+ * a callback used by the LinkTree object.
+ */
+function setLink(uuid) {
+ var language = CHOSEN_LANGUAGE;
+ document.forms["insertLink"].url.value = "lenya-document:" + uuid + ",lang=" + language;
+}
+
+
+function buildTree() {
+ var placeholder = document.getElementById('tree');
+ var tree = new LinkTree(document, placeholder);
+ tree.init(PUBLICATION_ID);
+ tree.render();
+ tree.loadInitialTree(AREA, DOCUMENT_ID);
+}
+
+LinkTree.prototype = new NavTree;
+LinkTree.prototype.handleItemClick = function(item, event) {
+ setLink(item.uuid);
+}
+
+window.onload = function() {
+ buildTree();
+ org.apache.lenya.editors.handleFormLoad('insertLink');
+};
+
+
\ No newline at end of file
Added: lenya/trunk/src/modules/editors/resources/javascript/oneform.js
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/javascript/oneform.js?view=auto&rev=559652
==============================================================================
--- lenya/trunk/src/modules/editors/resources/javascript/oneform.js (added)
+++ lenya/trunk/src/modules/editors/resources/javascript/oneform.js Wed Jul 25 16:59:41 2007
@@ -0,0 +1,70 @@
+org.apache.lenya.editors.setObjectData = function(objectData, windowName) {
+ var currentUsecase = usecaseMap[windowName];
+ // alert("windowName: "+ windowName);
+ var beforeSelection, afterSelection, replaceSelection;
+
+ switch (currentUsecase) {
+ case "insertLink":
+ beforeSelection = '<a'
+ + (objectData.url ? ' href="' + objectData.url + '"' : '')
+ + (objectData.title ? ' title="' + objectData.title + '"' : '')
+ + '>';
+ afterSelection = '</a>';
+ replaceSelection =
+ objectData.text ? objectData.text : undefined;
+ break;
+ case "insertImage":
+ beforeSelection = '<img'
+ + (objectData.url ? ' src="' + objectData.url + '"' : '')
+ + (objectData.title ? ' title="' + objectData.title + '"' : '')
+ + (objectData.text ? ' alt="' + objectData.title + '"' : '')
+ + '/>';
+ afterSelection = undefined;
+ replaceSelection = undefined;
+ break;
+ case "insertAsset":
+ beforeSelection = '<a'
+ + (objectData.url ? ' href="' + objectData.url + '"' : '')
+ + (objectData.title ? ' title="' + objectData.title + '"' : '')
+ + ' class="asset">';
+ afterSelection = '</a>';
+ replaceSelection =
+ objectData.text ? objectData.text : undefined;
+ break;
+ default:
+ alert("setObjectData: Unknown usecase " + currentUsecase + ". This is likely a programming error.");
+ return;
+ }
+ org.apache.lenya.editors.insertContent(
+ document.forms['oneform'].elements['content'],
+ beforeSelection,
+ afterSelection,
+ replaceSelection
+ );
+ usecaseMap[windowName] = undefined;
+ objectData[windowName] = undefined;
+}
+
+org.apache.lenya.editors.getObjectData = function(windowName) {
+ return objectData[windowName];
+}
+
+function triggerUsecase(usecase) {
+ var windowName = org.apache.lenya.editors.generateUniqueWindowName();
+ org.apache.lenya.editors.openUsecaseWindow(usecase, windowName);
+ objectData[windowName] = {
+ url : "",
+ text : "",
+ title : ""
+ }
+ usecaseMap[windowName] = usecase;
+ /* alert("Stored values:"
+ + "\n windowName = '" + windowName
+ + "'\n objectData[windowName] = '" + objectData[windowName]
+ + "'\n usecaseMap[windowName] = '" + usecaseMap[windowName]
+ + "'");
+ */
+}
+
+var objectData = new Array();
+var usecaseMap = new Array();
Added: lenya/trunk/src/modules/editors/resources/javascript/org.apache.lenya.editors.js
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/editors/resources/javascript/org.apache.lenya.editors.js?view=auto&rev=559652
==============================================================================
--- lenya/trunk/src/modules/editors/resources/javascript/org.apache.lenya.editors.js (added)
+++ lenya/trunk/src/modules/editors/resources/javascript/org.apache.lenya.editors.js Wed Jul 25 16:59:41 2007
@@ -0,0 +1,228 @@
+// create a domain-based "package" to keep the global namespace clean
+var org;
+if (!org) org = {};
+if (!org.apache) org.apache = {};
+if (!org.apache.lenya) org.apache.lenya = {};
+if (!org.apache.lenya.editors) org.apache.lenya.editors = {};
+
+
+/**
+ * defines the interface between generic editor usecases and editor implementations
+ *
+ * The idea is to use the same data structure for links, images and assets.
+ * Thus, insertLink, insertImage and insertAsset can all share most of the javascript code.
+ *
+ * FIXME: objectData is an exceptionally stupid term. Please fix if you can think of
+ * something that encompasses "data for to-be-inserted links, images and assets in general".
+ */
+org.apache.lenya.editors.objectDataTemplate = {
+ url : "", // href for links, src for assets and images
+ title : "", // xhtml title attribute
+ text : "", // element content for links and assets, alt text for images
+ width : "", // width for images
+ height : "", // height for images
+ type : "", // MIME Type for images and assets.
+/* probably not necessary now that we have windowName...
+ editorID : "" // an optional field that can be used by the calling editor instance
+ // to identify the transaction in case multiple usecases are triggered
+ // at the same time or multiple editors are active on one page.
+*/
+}
+
+/**
+ * set objectData object in editor
+ *
+ * This callback must be implemented in the editor window.
+ * @param objectData a data object as defined by objectDataTemplate
+ * @param windowName the ID of the usecase window (window.name).
+ * @see org.apache.lenya.editors.objectDataTemplate
+ */
+org.apache.lenya.editors.setObjectData = undefined;
+
+
+/**
+ * get objectData object from editor
+ *
+ * This callback must be implemented in the editor window.
+ * @param windowName the ID of the usecase window (window.name).
+ * @returns an objectData object.
+ * @see org.apache.lenya.editors.objectDataTemplate
+ */
+org.apache.lenya.editors.getObjectData = undefined;
+
+
+/**
+ * sets default values of the usecase form
+ *
+ * The form field names must correspond to the properties of
+ * objectDataTemplate.
+ * Note: if a value in objectData is undefined (as opposed to ""),
+ * the corresponding form field will be disabled (greyed out).
+ * Editors should use this to deactivate properties they wish
+ * to handle themselves.
+ * @param formName the "name" attribute of the form
+ * @param objectData
+ * @see org.apache.lenya.editors.objectDataTemplate
+ */
+org.apache.lenya.editors.setFormValues = function(formName,objectData) {
+ var form = document.forms[formName];
+ for (var i in org.apache.lenya.editors.objectDataTemplate) {
+ if (form[i] !== undefined) {
+ if (objectData[i] !== undefined) {
+ form[i].value = objectData[i];
+ } else {
+ form[i].disabled = true;
+ form[i].title = "disabled by editor";
+ }
+ }
+ }
+}
+
+
+/**
+ * reads the values from the usecase form
+ *
+ * The form field names must correspond to the properties of
+ * objectDataTemplate.
+ * @param formName the "name" attribute of the form
+ * @returns objectData
+ */
+org.apache.lenya.editors.getFormValues = function(formName) {
+ var form = document.forms[formName];
+ var objectData = new Object();
+ for (var i in org.apache.lenya.editors.objectDataTemplate) {
+ if (form[i] !== undefined) {
+ objectData[i] = form[i].value;
+ }
+ }
+ return objectData;
+}
+
+/**
+ * handle the submit event of the form
+ *
+ * @param formName the "name" attribute of the form
+ */
+org.apache.lenya.editors.handleFormSubmit = function(formName) {
+ var objectData = org.apache.lenya.editors.getFormValues(formName);
+ window.opener.org.apache.lenya.editors.setObjectData(objectData, window.name);
+ window.close();
+}
+
+/**
+ * handle the load event of the form
+ *
+ * @param formName the "name" attribute of the form
+ */
+org.apache.lenya.editors.handleFormLoad = function(formName) {
+ var objectData = window.opener.org.apache.lenya.editors.getObjectData(window.name);
+ org.apache.lenya.editors.setFormValues(formName, objectData);
+}
+
+
+/**
+ * default attributes for usecase windows (window.open()...)
+ */
+org.apache.lenya.editors.usecaseWindowOptions =
+ "toolbar=no,"
+ + "scrollbars=yes,"
+ + "status=no,"
+ + "resizable=yes,"
+ + "dependent=yes,"
+ + "width=600,"
+ + "height=700";
+
+org.apache.lenya.editors.generateUniqueWindowName = function() {
+ return new String("windowName-" + Math.random().toString().substr(2));
+}
+
+/**
+ * a helper function to open new usecase windows.
+ *
+ * If everyone used this, we'd save some maintenance work
+ * in the long run and can ensure consistent
+ * behaviour across different editors.
+ * @param usecase the name of the usecase to invoke, one of
+ * ("insertLink" | "insertImage" | "insertAsset")
+ * @param windowName the name of the new window, in case the editor needs
+ * that info later on.
+ * @returns the new window object
+ */
+org.apache.lenya.editors.openUsecaseWindow = function(usecase, windowName) {
+ var currentBaseURL;
+ var usecaseWindow;
+
+ switch (usecase) {
+ case "insertLink":
+ case "insertAsset":
+ case "insertImage":
+ currentBaseURL = window.location.href.replace(/\?.*$/,"");
+ usecaseWindow = window.open(
+ currentBaseURL + "?lenya.usecase=editors." + usecase,
+ windowName,
+ org.apache.lenya.editors.usecaseWindowOptions
+ );
+ break;
+ default:
+ alert("openUsecaseWindow: Unknown usecase '" + usecase + "'. This is likely a programming error.");
+ }
+ return usecaseWindow;
+}
+/**
+ * a cross-browser helper to insert data at the selected position in a form field (textarea etc.)
+ *
+ * @param formElement a XHTML form element (document.forms[foo].bar)
+ * @param beforeSelection the text snippet that should be inserted before the selected text
+ * @param afterSelection the text snippet that should be inserted after the selected text
+ * @param replaceSelection (optional) replaces the currently selected text (retained if absent)
+ *
+ * inspired by http://aktuell.de.selfhtml.org/artikel/javascript/bbcode/
+ */
+org.apache.lenya.editors.insertContent = function(
+ formElement,
+ beforeSelection,
+ afterSelection,
+ replaceSelection) {
+
+ /* alert("beforeSelection: '" + beforeSelection + "'\n"
+ + "afterSelection: '" + afterSelection + "'\n"
+ + "replaceSelection: '" + replaceSelection + "'\n"
+ );*/
+
+ // Danger, Will Robinson: you are leaving the w3c sector!
+ // Selections are not properly standardized yet...
+ formElement.focus();
+ // Firefox and friends will support this for textareas etc.
+ if (formElement.selectionStart !== undefined) {
+ var begin = formElement.selectionStart;
+ var end = formElement.selectionEnd;
+ var content = formElement.value;
+ var selection = content.substring(begin, end);
+ // alert("Found selection beginning at [" + begin + "], ending at [" + end + "].");
+ formElement.value = content.substr(0, begin)
+ + (beforeSelection ? beforeSelection : "")
+ + (replaceSelection ? replaceSelection : selection)
+ + (afterSelection ? afterSelection : "")
+ + content.substr(end);
+ // update cursor position:
+ formElement.selectionStart = begin;
+ formElement.selectionEnd = begin;
+ } else
+ // IE does it thusly:
+ if (document.selection !== undefined) {
+ alert("Hey, you are using IE, right? Please get in touch with [email protected] to test this feature!");
+ var range = document.selection.createRange();
+ var selection = range.text;
+ range.text = (beforeSelection ? beforeSelection : "")
+ + (replaceSelection ? replaceSelection : selection)
+ + (afterSelection ? afterSelection : "");
+ range.select();
+ } else {
+ // for all other browsers, paste the stuff at the end...
+ alert("Hey, what kind of browser is this? Please get in touch with [email protected] to make this feature work properly for you!");
+ formElement.value = formElement.value
+ + (beforeSelection ? beforeSelection : "")
+ + (replaceSelection ? replaceSelection : selection)
+ + (afterSelection ? afterSelection : "");
+ }
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.