Path: blob/trunk/third_party/closure/goog/editor/plugins/loremipsum.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A plugin that fills the field with lorem ipsum text when it's16* empty and does not have the focus. Applies to both editable and uneditable17* fields.18*19* @author [email protected] (Nick Santos)20*/2122goog.provide('goog.editor.plugins.LoremIpsum');2324goog.require('goog.asserts');25goog.require('goog.dom');26goog.require('goog.editor.Command');27goog.require('goog.editor.Field');28goog.require('goog.editor.Plugin');29goog.require('goog.editor.node');30goog.require('goog.functions');31goog.require('goog.html.SafeHtml');32goog.require('goog.userAgent');33343536/**37* A plugin that manages lorem ipsum state of editable fields.38* @param {string} message The lorem ipsum message.39* @constructor40* @extends {goog.editor.Plugin}41* @final42*/43goog.editor.plugins.LoremIpsum = function(message) {44goog.editor.Plugin.call(this);4546/**47* The lorem ipsum message.48* @type {string}49* @private50*/51this.message_ = message;52};53goog.inherits(goog.editor.plugins.LoremIpsum, goog.editor.Plugin);545556/** @override */57goog.editor.plugins.LoremIpsum.prototype.getTrogClassId =58goog.functions.constant('LoremIpsum');596061/** @override */62goog.editor.plugins.LoremIpsum.prototype.activeOnUneditableFields =63goog.functions.TRUE;646566/**67* Whether the field is currently filled with lorem ipsum text.68* @type {boolean}69* @private70*/71goog.editor.plugins.LoremIpsum.prototype.usingLorem_ = false;727374/**75* Handles queryCommandValue.76* @param {string} command The command to query.77* @return {boolean} The result.78* @override79*/80goog.editor.plugins.LoremIpsum.prototype.queryCommandValue = function(command) {81return command == goog.editor.Command.USING_LOREM && this.usingLorem_;82};838485/**86* Handles execCommand.87* @param {string} command The command to execute.88* Should be CLEAR_LOREM or UPDATE_LOREM.89* @param {*=} opt_placeCursor Whether to place the cursor in the field90* after clearing lorem. Should be a boolean.91* @override92*/93goog.editor.plugins.LoremIpsum.prototype.execCommand = function(94command, opt_placeCursor) {95if (command == goog.editor.Command.CLEAR_LOREM) {96this.clearLorem_(!!opt_placeCursor);97} else if (command == goog.editor.Command.UPDATE_LOREM) {98this.updateLorem_();99}100};101102103/** @override */104goog.editor.plugins.LoremIpsum.prototype.isSupportedCommand = function(105command) {106return command == goog.editor.Command.CLEAR_LOREM ||107command == goog.editor.Command.UPDATE_LOREM ||108command == goog.editor.Command.USING_LOREM;109};110111112/**113* Set the lorem ipsum text in a goog.editor.Field if needed.114* @private115*/116goog.editor.plugins.LoremIpsum.prototype.updateLorem_ = function() {117// Try to apply lorem ipsum if:118// 1) We have lorem ipsum text119// 2) There's not a dialog open, as that screws120// with the dialog's ability to properly restore the selection121// on dialog close (since the DOM nodes would get clobbered in FF)122// 3) We're not using lorem already123// 4) The field is not currently active (doesn't have focus).124var fieldObj = this.getFieldObject();125if (!this.usingLorem_ && !fieldObj.inModalMode() &&126goog.editor.Field.getActiveFieldId() != fieldObj.id) {127var field = fieldObj.getElement();128if (!field) {129// Fallback on the original element. This is needed by130// fields managed by click-to-edit.131field = fieldObj.getOriginalElement();132}133134goog.asserts.assert(field);135if (goog.editor.node.isEmpty(field)) {136this.usingLorem_ = true;137138// Save the old font style so it can be restored when we139// clear the lorem ipsum style.140this.oldFontStyle_ = field.style.fontStyle;141field.style.fontStyle = 'italic';142fieldObj.setSafeHtml(143true, goog.html.SafeHtml.htmlEscapePreservingNewlines(this.message_),144true);145}146}147};148149150/**151* Clear an EditableField's lorem ipsum and put in initial text if needed.152*153* If using click-to-edit mode (where Trogedit manages whether the field154* is editable), this works for both editable and uneditable fields.155*156* TODO(user): Is this really necessary? See TODO below.157* @param {boolean=} opt_placeCursor Whether to place the cursor in the field158* after clearing lorem.159* @private160*/161goog.editor.plugins.LoremIpsum.prototype.clearLorem_ = function(162opt_placeCursor) {163// Don't mess with lorem state when a dialog is open as that screws164// with the dialog's ability to properly restore the selection165// on dialog close (since the DOM nodes would get clobbered)166var fieldObj = this.getFieldObject();167if (this.usingLorem_ && !fieldObj.inModalMode()) {168var field = fieldObj.getElement();169if (!field) {170// Fallback on the original element. This is needed by171// fields managed by click-to-edit.172field = fieldObj.getOriginalElement();173}174175goog.asserts.assert(field);176this.usingLorem_ = false;177field.style.fontStyle = this.oldFontStyle_;178fieldObj.setSafeHtml(true, null, true);179180// TODO(nicksantos): I'm pretty sure that this is a hack, but talk to181// Julie about why this is necessary and what to do with it. Really,182// we need to figure out where it's necessary and remove it where it's183// not. Safari never places the cursor on its own willpower.184if (opt_placeCursor && fieldObj.isLoaded()) {185if (goog.userAgent.WEBKIT) {186goog.dom.getOwnerDocument(fieldObj.getElement()).body.focus();187fieldObj.focusAndPlaceCursorAtStart();188} else if (goog.userAgent.OPERA) {189fieldObj.placeCursorAtStart();190}191}192}193};194195196