Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Fix for #532 and #537
Browse files Browse the repository at this point in the history
Shrink rule list when host editor width shrinks.
Update rule list position when scroll bars appear in the host editor.
  • Loading branch information
jasonsanjose committed Apr 4, 2012
1 parent 05888b1 commit 8f15329
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/editor/CSSInlineEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ define(function (require, exports, module) {

// Container to hold all editors
var self = this,
hostScroller = hostEditor._codeMirror.getScrollerElement(),
$ruleItem,
$location;

Expand All @@ -62,9 +61,9 @@ define(function (require, exports, module) {

// Outer container for border-left and scrolling
this.$relatedContainer = $(document.createElement("div")).addClass("relatedContainer");
// Position immediately to the left of the main editor's scrollbar.
var rightOffset = $(document.body).outerWidth() - ($(hostScroller).offset().left + $(hostScroller).get(0).clientWidth);
this.$relatedContainer.css("right", rightOffset + "px");
this._relatedContainerInserted = false;
this._relatedContainerInsertedHandler = this._relatedContainerInsertedHandler.bind(this);
this.$relatedContainer.on("DOMNodeInserted", this._relatedContainerInsertedHandler);

// List "selection" highlight
this.$selectedMarker = $(document.createElement("div")).appendTo(this.$relatedContainer).addClass("selection");
Expand Down Expand Up @@ -108,6 +107,10 @@ define(function (require, exports, module) {

// Listen to the editor's scroll event to reposition the relatedContainer.
$(this.hostEditor).on("scroll", this._updateRelatedContainer);

// Listen to the window resize event to reposition the relatedContainer
// when the hostEditor's scrollbars visibility changes
$(window).on("resize", this._updateRelatedContainer);

return (new $.Deferred()).resolve();
};
Expand Down Expand Up @@ -191,6 +194,16 @@ define(function (require, exports, module) {
$(this.hostEditor).off("change", this._updateRelatedContainer);
$(this.editors[0]).off("change", this._updateRelatedContainer);
$(this.hostEditor).off("scroll", this._updateRelatedContainer);
$(window).off("resize", this._updateRelatedContainer);
};

/**
* @private
* Set _relatedContainerInserted flag once the $relatedContainer is inserted in the DOM.
*/
CSSInlineEditor.prototype._relatedContainerInsertedHandler = function () {
this.$relatedContainer.off("DOMNodeInserted", this._relatedContainerInsertedHandler);
this._relatedContainerInserted = true;
};

/**
Expand All @@ -209,13 +222,36 @@ define(function (require, exports, module) {
rcHeight = this.$relatedContainer.outerHeight(),
rcBottom = rcTop + rcHeight,
scrollerTop = $(hostScroller).offset().top,
scrollerBottom = scrollerTop + hostScroller.clientHeight;
scrollerBottom = scrollerTop + hostScroller.clientHeight,
scrollerLeft = $(hostScroller).offset().left,
rightOffset = $(document.body).outerWidth() - (scrollerLeft + hostScroller.clientWidth);
if (rcTop < scrollerTop || rcBottom > scrollerBottom) {
this.$relatedContainer.css("clip", "rect(" + Math.max(scrollerTop - rcTop, 0) + "px, auto, " +
(rcHeight - Math.max(rcBottom - scrollerBottom, 0)) + "px, auto)");
} else {
this.$relatedContainer.css("clip", "");
}

// Constrain relatedContainer width to half of the scroller width
var relatedContainerWidth = this.$relatedContainer.width();
if (this._relatedContainerInserted) {
if (this._relatedContainerDefaultWidth === undefined) {
this._relatedContainerDefaultWidth = relatedContainerWidth;
}

var halfWidth = hostScroller.clientWidth / 2;

if (this._relatedContainerDefaultWidth >= halfWidth) {
relatedContainerWidth = halfWidth;
} else {
relatedContainerWidth = this._relatedContainerDefaultWidth;
}

this.$relatedContainer.width(Math.floor(relatedContainerWidth));
}

// Position immediately to the left of the main editor's scrollbar.
this.$relatedContainer.css("right", rightOffset + "px");
};

/**
Expand Down

0 comments on commit 8f15329

Please sign in to comment.