SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/knowledge-article/fix-ckeditor-top-bar-direct...

66 lines
3.7 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('knowledgeArticleModule')
.directive('fixCkeditorTopBar', ['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function (scope, iElement) {
var isTopPanelDefined, topPanelDefaultClass = 'ka-ckeditor__top-bar', topPanelFixedClass = 'ka-ckeditor__top-bar_fixed', topPanel, editorWrapper, scrollLimit, containerOffset = iElement.offset(), modalOffset = iElement.parents('.modal-content').offset() || { top: 0 };
iElement.css('overflow-y', 'hidden');
scope.$on('ckeditor.ready', function () {
if (!isTopPanelDefined) {
iElement.css('overflow-y', 'auto');
topPanel = iElement.find('.' + topPanelDefaultClass);
editorWrapper = topPanel.parent();
recalculateScrollLimit();
}
});
scope.$watch('state.similarArticles', function () {
if (topPanel) {
$timeout(recalculateScrollLimit, 0);
}
});
scope.$watch('state.showSimilarArticles', function () {
if (topPanel) {
$timeout(recalculateScrollLimit, 0);
}
});
function scrollEventHandler() {
$timeout(function () {
var delta = Math.floor(scrollLimit - iElement.scrollTop()), oldScrollPos = iElement.scrollTop();
if ((delta > 0) && topPanel.hasClass(topPanelFixedClass)) {
console.log('making top bar normal');
topPanel.removeClass(topPanelFixedClass).addClass(topPanelDefaultClass).css('top', 'auto').width('auto');
topPanel.next().css('margin-top', 0);
iElement.scrollTop(oldScrollPos);
}
else if ((delta <= 0) && topPanel.hasClass(topPanelDefaultClass)) {
console.log('making top bar fixed');
topPanel.removeClass(topPanelDefaultClass).addClass(topPanelFixedClass).css('top', (containerOffset.top - modalOffset.top) + 'px').width(editorWrapper.outerWidth());
topPanel.next().css('margin-top', topPanel.height());
iElement.scrollTop(oldScrollPos);
}
}, 0);
}
iElement.on('scroll', scrollEventHandler);
scope.$on("$destroy", function () {
console.log("fixCkeditorTopBar: unbind events");
iElement.off('scroll', scrollEventHandler);
iElement.off();
});
function recalculateScrollLimit() {
if (topPanel.hasClass(topPanelFixedClass)) {
var revertClass = true;
topPanel.removeClass(topPanelFixedClass).addClass(topPanelDefaultClass);
}
scrollLimit = (topPanel.offset().top + iElement.scrollTop()) - containerOffset.top;
if (revertClass) {
topPanel.removeClass(topPanelDefaultClass).addClass(topPanelFixedClass);
}
}
}
};
}]);
}());