poll.js 2.16 KB
/**
 * This extension displays polls.
 */
(function () {
    function poll() { 
        return [{
            type: 'lang',
            filter: function(text) {
                return text.replace(/^\[poll id=([a-f\d]+)\]\s*$/gm, function(match, id) {
                    var string;

                    if (window['pollCache-' + id]) {
                        string = window['pollCache-' + id]; 
                        string = string.replace(/fr-poll(-preview)?="(.*?)"/g, '');
                        string = string.replace(/ng-/g, 'not-ng-');
                        string = string.replace(/ngIf/g, 'not-ngIf');
                        string = string.replace(/ngRepeat/g, 'not-ngRepeat');
                        string = string.replace(/\s+/g, ' ');
                    } else {
                        string = '<div ng-cloak class="poll" fr-poll="' + id + '" fr-poll-preview></div>';
                    }

                    return string;
                });
            }
        }];
    }

    angular.module('footyroom').directive("frPollPreview", [function() {
        return {
            require: 'frPoll',

            link: function(scope, el, attrs, ctrl) {
                // Prevent clicks on the poll.
                el.prepend('<div style="position: absolute; width: 100%; height: 100%"></div>');

                scope.$watch(
                    function() {
                        return ctrl.poll;
                    },
                    function(newValue) {
                        if (newValue) {
                            // Show prestine poll condition.
                            delete ctrl.showPollResult;
                            delete ctrl.selectedChoice;

                            // Cache poll's html.
                            setTimeout(function() {
                                window['pollCache-' + ctrl.frPoll] = el[0].outerHTML;
                            }, 0);
                        }
                    }
                );
            }
        };
    }]);

    if (window && window.showdown) {
        window.showdown.extensions.poll = poll;
    }

    if (typeof module !== 'undefined') {
        module.exports = poll;
    }
})();