123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- goog.provide('goog.ui.editor.Bubble');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.ViewportSizeMonitor');
- goog.require('goog.dom.classlist');
- goog.require('goog.editor.style');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventTarget');
- goog.require('goog.events.EventType');
- goog.require('goog.functions');
- goog.require('goog.log');
- goog.require('goog.math.Box');
- goog.require('goog.object');
- goog.require('goog.positioning');
- goog.require('goog.positioning.Corner');
- goog.require('goog.positioning.Overflow');
- goog.require('goog.positioning.OverflowStatus');
- goog.require('goog.string');
- goog.require('goog.style');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.PopupBase');
- goog.require('goog.userAgent');
- goog.ui.editor.Bubble = function(parent, zIndex) {
- goog.ui.editor.Bubble.base(this, 'constructor');
-
- this.dom_ = goog.dom.getDomHelper(parent);
-
- this.eventHandler_ = new goog.events.EventHandler(this);
-
- this.viewPortSizeMonitor_ =
- new goog.dom.ViewportSizeMonitor(this.dom_.getWindow());
-
- this.panels_ = {};
-
- this.bubbleContainer_ = this.dom_.createDom(
- goog.dom.TagName.DIV,
- {'className': goog.ui.editor.Bubble.BUBBLE_CLASSNAME});
- goog.style.setElementShown(this.bubbleContainer_, false);
- goog.dom.appendChild(parent, this.bubbleContainer_);
- goog.style.setStyle(this.bubbleContainer_, 'zIndex', zIndex);
-
- this.bubbleContents_ = this.createBubbleDom(this.dom_, this.bubbleContainer_);
-
- this.closeBox_ = this.dom_.createDom(goog.dom.TagName.DIV, {
- 'className': goog.getCssName('tr_bubble_closebox'),
- 'innerHTML': ' '
- });
- this.bubbleContents_.appendChild(this.closeBox_);
-
-
- goog.editor.style.makeUnselectable(this.bubbleContainer_, this.eventHandler_);
-
- this.popup_ = new goog.ui.PopupBase(this.bubbleContainer_);
- };
- goog.inherits(goog.ui.editor.Bubble, goog.events.EventTarget);
- goog.ui.editor.Bubble.BUBBLE_CLASSNAME = goog.getCssName('tr_bubble');
- goog.ui.editor.Bubble.prototype.createBubbleDom = function(dom, container) {
- return container;
- };
- goog.ui.editor.Bubble.prototype.logger =
- goog.log.getLogger('goog.ui.editor.Bubble');
- goog.ui.editor.Bubble.prototype.disposeInternal = function() {
- goog.ui.editor.Bubble.base(this, 'disposeInternal');
- goog.dom.removeNode(this.bubbleContainer_);
- this.bubbleContainer_ = null;
- this.eventHandler_.dispose();
- this.eventHandler_ = null;
- this.viewPortSizeMonitor_.dispose();
- this.viewPortSizeMonitor_ = null;
- };
- goog.ui.editor.Bubble.prototype.getContentElement = function() {
- return this.bubbleContents_;
- };
- goog.ui.editor.Bubble.prototype.getContainerElement = function() {
- return this.bubbleContainer_;
- };
- goog.ui.editor.Bubble.prototype.getEventHandler = function() {
- return this.eventHandler_;
- };
- goog.ui.editor.Bubble.prototype.handleWindowResize_ = function() {
- if (this.isVisible()) {
- this.reposition();
- }
- };
- goog.ui.editor.Bubble.prototype.setAutoHide = function(autoHide) {
- this.popup_.setAutoHide(autoHide);
- };
- goog.ui.editor.Bubble.prototype.hasPanelOfType = function(type) {
- return goog.object.some(
- this.panels_, function(panel) { return panel.type == type; });
- };
- goog.ui.editor.Bubble.prototype.addPanel = function(
- type, title, targetElement, contentFn, opt_preferTopPosition) {
- var id = goog.string.createUniqueString();
- var panel = new goog.ui.editor.Bubble.Panel_(
- this.dom_, id, type, title, targetElement, !opt_preferTopPosition);
- this.panels_[id] = panel;
-
-
-
-
-
-
- var nextElement;
- for (var i = 0, len = this.bubbleContents_.childNodes.length - 1; i < len;
- i++) {
- var otherChild = this.bubbleContents_.childNodes[i];
- var otherPanel = this.panels_[otherChild.id];
- if (otherPanel.type > type) {
- nextElement = otherChild;
- break;
- }
- }
- goog.dom.insertSiblingBefore(
- panel.element, nextElement || this.bubbleContents_.lastChild);
- contentFn(panel.getContentElement());
- goog.editor.style.makeUnselectable(panel.element, this.eventHandler_);
- var numPanels = goog.object.getCount(this.panels_);
- if (numPanels == 1) {
- this.openBubble_();
- } else if (numPanels == 2) {
- goog.dom.classlist.add(
- goog.asserts.assert(this.bubbleContainer_),
- goog.getCssName('tr_multi_bubble'));
- }
- this.reposition();
- return id;
- };
- goog.ui.editor.Bubble.prototype.removePanel = function(id) {
- var panel = this.panels_[id];
- goog.dom.removeNode(panel.element);
- delete this.panels_[id];
- var numPanels = goog.object.getCount(this.panels_);
- if (numPanels <= 1) {
- goog.dom.classlist.remove(
- goog.asserts.assert(this.bubbleContainer_),
- goog.getCssName('tr_multi_bubble'));
- }
- if (numPanels == 0) {
- this.closeBubble_();
- } else {
- this.reposition();
- }
- };
- goog.ui.editor.Bubble.prototype.openBubble_ = function() {
- this.eventHandler_
- .listen(this.closeBox_, goog.events.EventType.CLICK, this.closeBubble_)
- .listen(
- this.viewPortSizeMonitor_, goog.events.EventType.RESIZE,
- this.handleWindowResize_)
- .listen(
- this.popup_, goog.ui.PopupBase.EventType.HIDE, this.handlePopupHide);
- this.popup_.setVisible(true);
- this.reposition();
- };
- goog.ui.editor.Bubble.prototype.closeBubble_ = function() {
- this.popup_.setVisible(false);
- };
- goog.ui.editor.Bubble.prototype.handlePopupHide = function() {
-
- for (var panelId in this.panels_) {
- goog.dom.removeNode(this.panels_[panelId].element);
- }
-
- this.panels_ = {};
- goog.dom.classlist.remove(
- goog.asserts.assert(this.bubbleContainer_),
- goog.getCssName('tr_multi_bubble'));
- this.eventHandler_.removeAll();
- this.dispatchEvent(goog.ui.Component.EventType.HIDE);
- };
- goog.ui.editor.Bubble.prototype.isVisible = function() {
- return this.popup_.isVisible();
- };
- goog.ui.editor.Bubble.VERTICAL_CLEARANCE_ = goog.userAgent.IE ? 4 : 2;
- goog.ui.editor.Bubble.MARGIN_BOX_ = new goog.math.Box(
- goog.ui.editor.Bubble.VERTICAL_CLEARANCE_, 0,
- goog.ui.editor.Bubble.VERTICAL_CLEARANCE_, 0);
- goog.ui.editor.Bubble.prototype.getMarginBox = function() {
- return goog.ui.editor.Bubble.MARGIN_BOX_;
- };
- goog.ui.editor.Bubble.prototype.reposition = function() {
- var targetElement = null;
- var preferBottomPosition = true;
- for (var panelId in this.panels_) {
- var panel = this.panels_[panelId];
-
- targetElement = panel.targetElement;
- preferBottomPosition = preferBottomPosition && panel.preferBottomPosition;
- }
- var status = goog.positioning.OverflowStatus.FAILED;
-
-
-
- var reverseLayout =
- (goog.style.isRightToLeft(this.bubbleContainer_) !=
- goog.style.isRightToLeft(targetElement));
-
-
- if (preferBottomPosition) {
- status = this.positionAtAnchor_(
- reverseLayout ? goog.positioning.Corner.BOTTOM_END :
- goog.positioning.Corner.BOTTOM_START,
- goog.positioning.Corner.TOP_START,
- goog.positioning.Overflow.ADJUST_X | goog.positioning.Overflow.FAIL_Y);
- }
- if (status & goog.positioning.OverflowStatus.FAILED) {
-
-
- status = this.positionAtAnchor_(
- reverseLayout ? goog.positioning.Corner.TOP_END :
- goog.positioning.Corner.TOP_START,
- goog.positioning.Corner.BOTTOM_START,
- goog.positioning.Overflow.ADJUST_X | goog.positioning.Overflow.FAIL_Y);
- }
- if (status & goog.positioning.OverflowStatus.FAILED) {
-
-
- status = this.positionAtAnchor_(
- reverseLayout ? goog.positioning.Corner.BOTTOM_END :
- goog.positioning.Corner.BOTTOM_START,
- goog.positioning.Corner.TOP_START, goog.positioning.Overflow.ADJUST_X |
- goog.positioning.Overflow.ADJUST_Y);
- if (status & goog.positioning.OverflowStatus.FAILED) {
- goog.log.warning(
- this.logger,
- 'reposition(): positionAtAnchor() failed with ' + status);
- }
- }
- };
- goog.ui.editor.Bubble.prototype.positionAtAnchor_ = function(
- targetCorner, bubbleCorner, overflow) {
- var targetElement = null;
- for (var panelId in this.panels_) {
-
-
-
-
- var candidate = this.panels_[panelId].targetElement;
- if (!targetElement || goog.dom.contains(candidate, targetElement)) {
- targetElement = this.panels_[panelId].targetElement;
- }
- }
- return goog.positioning.positionAtAnchor(
- targetElement, targetCorner, this.bubbleContainer_, bubbleCorner, null,
- this.getMarginBox(), overflow, null, this.getViewportBox());
- };
- goog.ui.editor.Bubble.prototype.getViewportBox = goog.functions.NULL;
- goog.ui.editor.Bubble.Panel_ = function(
- dom, id, type, title, targetElement, preferBottomPosition) {
-
- this.type = type;
-
- this.targetElement = targetElement;
-
- this.preferBottomPosition = preferBottomPosition;
-
- this.element = dom.createDom(
- goog.dom.TagName.DIV,
- {className: goog.getCssName('tr_bubble_panel'), id: id},
- dom.createDom(
- goog.dom.TagName.DIV,
- {className: goog.getCssName('tr_bubble_panel_title')},
- title ? title + ':' : ''),
- dom.createDom(
- goog.dom.TagName.DIV,
- {className: goog.getCssName('tr_bubble_panel_content')}));
- };
- goog.ui.editor.Bubble.Panel_.prototype.getContentElement = function() {
- return (this.element.lastChild);
- };
|