2014-08-24 18:53:55 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
var weechat = angular.module('weechat');
|
|
|
|
|
2014-12-22 22:17:01 +01:00
|
|
|
weechat.directive('plugin', ['$rootScope', 'settings', function($rootScope, settings) {
|
2014-08-28 18:25:40 +02:00
|
|
|
/*
|
|
|
|
* Plugin directive
|
|
|
|
* Shows additional plugin content
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
templateUrl: 'directives/plugin.html',
|
|
|
|
|
|
|
|
scope: {
|
|
|
|
plugin: '=data'
|
|
|
|
},
|
|
|
|
|
|
|
|
controller: ['$scope', function($scope) {
|
|
|
|
|
|
|
|
$scope.displayedContent = "";
|
|
|
|
|
2014-10-16 15:50:36 +02:00
|
|
|
// Auto-display embedded content only if it isn't NSFW
|
2014-12-22 22:17:01 +01:00
|
|
|
$scope.plugin.visible = !settings.noembed && !$scope.plugin.nsfw;
|
2014-08-28 18:25:40 +02:00
|
|
|
|
2014-09-28 20:52:53 +02:00
|
|
|
// user-accessible hash key that is a valid CSS class name
|
|
|
|
$scope.plugin.className = "embed_" + $scope.plugin.$$hashKey.replace(':','_');
|
|
|
|
|
|
|
|
$scope.plugin.getElement = function() {
|
|
|
|
return document.querySelector("." + $scope.plugin.className);
|
|
|
|
};
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
$scope.hideContent = function() {
|
|
|
|
$scope.plugin.visible = false;
|
|
|
|
};
|
|
|
|
|
2014-09-23 02:08:27 +02:00
|
|
|
$scope.showContent = function(automated) {
|
2014-08-28 18:25:40 +02:00
|
|
|
/*
|
|
|
|
* Shows the plugin content.
|
|
|
|
* displayedContent is bound to the DOM.
|
|
|
|
* Actual plugin content is only fetched when
|
|
|
|
* content is shown.
|
|
|
|
*/
|
|
|
|
|
2014-09-28 20:52:53 +02:00
|
|
|
var embed = $scope.plugin.getElement();
|
2014-09-25 18:00:54 +02:00
|
|
|
|
|
|
|
// If the plugin is asynchronous / lazy, execute it now and let it insert itself
|
|
|
|
// TODO store the result between channel switches
|
|
|
|
if ($scope.plugin.content instanceof Function){
|
|
|
|
// Don't rerun if the result is already there
|
2015-02-08 11:50:49 +01:00
|
|
|
if (!embed || embed.innerHTML === "") {
|
|
|
|
// if we're autoshowing, the element doesn't exist yet, and we need
|
|
|
|
// to do this async (wrapped in a setTimeout)
|
|
|
|
setTimeout(function() {
|
|
|
|
$scope.plugin.content();
|
|
|
|
});
|
2014-09-25 18:00:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$scope.displayedContent = $scope.plugin.content;
|
2014-08-28 18:25:40 +02:00
|
|
|
}
|
|
|
|
$scope.plugin.visible = true;
|
|
|
|
|
|
|
|
// Scroll embed content into view
|
2014-09-23 02:08:27 +02:00
|
|
|
var scroll;
|
|
|
|
if (automated) {
|
|
|
|
var wasBottom = $rootScope.bufferBottom;
|
|
|
|
scroll = function() {
|
|
|
|
$rootScope.updateBufferBottom(wasBottom);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
scroll = function() {
|
|
|
|
if (embed && embed.scrollIntoViewIfNeeded !== undefined) {
|
|
|
|
embed.scrollIntoViewIfNeeded();
|
|
|
|
$rootScope.updateBufferBottom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
setTimeout(scroll, 500);
|
2014-08-28 18:25:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($scope.plugin.visible) {
|
2014-09-23 02:08:27 +02:00
|
|
|
$scope.showContent(true);
|
2014-08-28 18:25:40 +02:00
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}]);
|
2014-08-24 18:53:55 +02:00
|
|
|
})();
|