123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- goog.provide('goog.editor.ClickToEditWrapper');
- goog.require('goog.Disposable');
- goog.require('goog.dom');
- goog.require('goog.dom.Range');
- goog.require('goog.dom.TagName');
- goog.require('goog.editor.BrowserFeature');
- goog.require('goog.editor.Command');
- goog.require('goog.editor.Field');
- goog.require('goog.editor.range');
- goog.require('goog.events.BrowserEvent');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventType');
- goog.editor.ClickToEditWrapper = function(fieldObj) {
- goog.Disposable.call(this);
-
- this.fieldObj_ = fieldObj;
-
- this.originalDomHelper_ =
- goog.dom.getDomHelper(fieldObj.getOriginalElement());
-
- this.savedCaretRange_ = null;
-
- this.fieldEventHandler_ = new goog.events.EventHandler(this);
-
- this.finishMouseUpBound_ = goog.bind(this.finishMouseUp_, this);
-
- this.mouseEventHandler_ = new goog.events.EventHandler(this);
-
- if (!this.fieldObj_.isLoaded()) {
- this.enterDocument();
- }
- this.fieldEventHandler_
- .
-
-
- listen(
- this.fieldObj_, goog.editor.Field.EventType.LOAD,
- this.renderSelection_)
- .
-
-
- listen(
- this.fieldObj_, goog.editor.Field.EventType.UNLOAD,
- this.enterDocument);
- };
- goog.inherits(goog.editor.ClickToEditWrapper, goog.Disposable);
- goog.editor.ClickToEditWrapper.prototype.getFieldObject = function() {
- return this.fieldObj_;
- };
- goog.editor.ClickToEditWrapper.prototype.getOriginalDomHelper = function() {
- return this.originalDomHelper_;
- };
- goog.editor.ClickToEditWrapper.prototype.disposeInternal = function() {
- goog.editor.ClickToEditWrapper.base(this, 'disposeInternal');
- this.exitDocument();
- if (this.savedCaretRange_) {
- this.savedCaretRange_.dispose();
- }
- this.fieldEventHandler_.dispose();
- this.mouseEventHandler_.dispose();
- this.savedCaretRange_ = null;
- delete this.fieldEventHandler_;
- delete this.mouseEventHandler_;
- };
- goog.editor.ClickToEditWrapper.prototype.enterDocument = function() {
- if (this.isInDocument_) {
- return;
- }
- this.isInDocument_ = true;
- this.mouseEventTriggeredLoad_ = false;
- var field = this.fieldObj_.getOriginalElement();
-
-
-
-
-
-
-
-
- this.savedAnchorClicked_ = null;
- this.mouseEventHandler_
- .listen(field, goog.events.EventType.MOUSEUP, this.handleMouseUp_)
- .listen(field, goog.events.EventType.CLICK, this.handleClick_);
-
- this.fieldObj_.execCommand(goog.editor.Command.UPDATE_LOREM);
- };
- goog.editor.ClickToEditWrapper.prototype.exitDocument = function() {
- this.mouseEventHandler_.removeAll();
- this.isInDocument_ = false;
- };
- goog.editor.ClickToEditWrapper.prototype.getElement = function() {
- return this.fieldObj_.isLoaded() ? this.fieldObj_.getElement() :
- this.fieldObj_.getOriginalElement();
- };
- goog.editor.ClickToEditWrapper.prototype.shouldHandleMouseEvent_ = function(e) {
- return e.isButton(goog.events.BrowserEvent.MouseButton.LEFT) &&
- !(e.shiftKey || e.ctrlKey || e.altKey || e.metaKey);
- };
- goog.editor.ClickToEditWrapper.prototype.handleClick_ = function(e) {
-
-
- var anchorAncestor = goog.dom.getAncestorByTagNameAndClass(
- (e.target), goog.dom.TagName.A);
- if (anchorAncestor) {
- e.preventDefault();
- if (!goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
- this.savedAnchorClicked_ = anchorAncestor;
- }
- }
- };
- goog.editor.ClickToEditWrapper.prototype.handleMouseUp_ = function(e) {
-
- if (this.shouldHandleMouseEvent_(e)) {
-
-
-
- this.originalDomHelper_.getWindow().setTimeout(this.finishMouseUpBound_, 0);
- }
- };
- goog.editor.ClickToEditWrapper.prototype.finishMouseUp_ = function() {
-
- if (!this.fieldObj_.isLoaded()) {
- if (this.savedCaretRange_) {
- this.savedCaretRange_.dispose();
- this.savedCaretRange_ = null;
- }
- if (!this.fieldObj_.queryCommandValue(goog.editor.Command.USING_LOREM)) {
-
-
-
-
- this.insertCarets_();
- }
- this.ensureFieldEditable_();
- }
- this.exitDocument();
- this.savedAnchorClicked_ = null;
- };
- goog.editor.ClickToEditWrapper.prototype.ensureFieldEditable_ = function() {
- if (!this.fieldObj_.isLoaded()) {
- this.mouseEventTriggeredLoad_ = true;
- this.makeFieldEditable(this.fieldObj_);
- }
- };
- goog.editor.ClickToEditWrapper.prototype.renderSelection_ = function() {
- if (this.savedCaretRange_) {
-
-
- this.savedCaretRange_.setRestorationDocument(
- this.fieldObj_.getEditableDomHelper().getDocument());
- var startCaret = this.savedCaretRange_.getCaret(true);
- var endCaret = this.savedCaretRange_.getCaret(false);
- var hasCarets = startCaret && endCaret;
- }
-
-
-
-
-
-
-
-
- if (this.mouseEventTriggeredLoad_ || hasCarets) {
- this.focusOnFieldObj(this.fieldObj_);
- }
- if (hasCarets) {
- this.savedCaretRange_.restore();
- this.fieldObj_.dispatchSelectionChangeEvent();
-
-
-
- }
- if (this.savedCaretRange_) {
- this.savedCaretRange_.dispose();
- this.savedCaretRange_ = null;
- }
- this.mouseEventTriggeredLoad_ = false;
- };
- goog.editor.ClickToEditWrapper.prototype.focusOnFieldObj = function(field) {
- field.focusAndPlaceCursorAtStart();
- };
- goog.editor.ClickToEditWrapper.prototype.makeFieldEditable = function(field) {
- field.makeEditable();
- };
- goog.editor.ClickToEditWrapper.createCaretRange_ = function(range) {
- return range && goog.editor.range.saveUsingNormalizedCarets(range);
- };
- goog.editor.ClickToEditWrapper.prototype.insertCarets_ = function() {
- var fieldElement = this.fieldObj_.getOriginalElement();
- this.savedCaretRange_ = null;
- var originalWindow = this.originalDomHelper_.getWindow();
- if (goog.dom.Range.hasSelection(originalWindow)) {
- var range = goog.dom.Range.createFromWindow(originalWindow);
- range = range && goog.editor.range.narrow(range, fieldElement);
- this.savedCaretRange_ =
- goog.editor.ClickToEditWrapper.createCaretRange_(range);
- }
- if (!this.savedCaretRange_) {
-
-
-
-
-
-
- var specialNodeClicked;
- if (goog.editor.BrowserFeature.HAS_ACTIVE_ELEMENT) {
- specialNodeClicked =
- goog.dom.getActiveElement(this.originalDomHelper_.getDocument());
- } else {
- specialNodeClicked = this.savedAnchorClicked_;
- }
- var isFieldElement = function(node) { return node == fieldElement; };
- if (specialNodeClicked &&
- goog.dom.getAncestor(specialNodeClicked, isFieldElement, true)) {
-
-
-
-
-
-
- this.savedCaretRange_ = goog.editor.ClickToEditWrapper.createCaretRange_(
- goog.dom.Range.createFromNodes(
- specialNodeClicked, 0, specialNodeClicked, 0));
- }
- }
- };
|