<commit_before>/* ========================================================================
 * ZUI: display.js
 * http://zui.sexy
 * ========================================================================
 * Copyright (c) 2014 <EMAIL>; Licensed MIT
 * ======================================================================== */

!(function($, undefined, window){
    'use strict';

    var uuid                = 1200,
        TAP_EVENT_NAME      = $.TapName,
        STR_DISPLAY         = 'display',
        STR_ORIGINAL_CLASS  = 'orginalClass',
        STR_HIDDEN          = 'hidden',
        STR_BODY            = 'body',
        NAME                = 'mzui.' + STR_DISPLAY,
        inverseSide         = {left: 'right', bottom: 'top', top: 'bottom', right: 'left'};

    // Display constructor([$element], options)
    // $element is optional
    var Display = function($element, options) {
        var that = this;
        if($.isPlainObject($element)) {
            options  = $element;
            $element = null;
        }
        options = $.extend({element: $element}, Display.DEFAULT, $element ? $element.data() : null, options);

        var display = options.display;
        if(display && Display.plugs['_' + display]) {
            options = $.calValue(Display.plugs['_' + display], that, options);
        }

        var selector = options.selector;

        if(!$element)     options.trigger = null;
        if(!options.name) options.name    = STR_DISPLAY + uuid++;

        var triggerCallback = function(e) {
            var $this = $(this);
            var thisOptions = $this.data() || {};
            thisOptions.element = this;
            if($this.is('a')) {
                var href = $this.attr('href');
                if(href && href !== '#' && href.indexOf('##') < 0) {
                    if(!thisOptions.target && /^#[a-z]/i.test(href)) {
                        thisOptions.target = href;
                    } else if(!thisOptions.remote) {
                        thisOptions[(thisOptions.load === true || options.load) ? 'load' : 'remote'] = href;
                    }
                }
                if(e) e.preventDefault();
            }
            that[options.triggerMethod](thisOptions);
        };

        if(options.trigger) {
            selector ? $element.on(options.trigger, selector, triggerCallback) : $element.on(options.trigger, triggerCallback);
        }

        that.$       = $element;
        that.options = options;

        if(options.displayAuto) that.show();
        if(selector && $element) {
            var activeClass = options.activeClass;
            triggerCallback.call($element.find(selector).filter('[data-display-auto]' + (activeClass ? (',.' + activeClass) : '')));
        }
    };

    Display.prototype._getTarget = function(options) {
        var that = this;

        var target = $.calValue(options.target, that, options);
        if(target === '#displayTarget') {
            var targetId = 'displayTarget-' + options.name,
                layerId = 'displayLayer-' + options.name;
            $('#' + targetId).remove();
            target = $('<div class="display ' + STR_HIDDEN + '"/>', {id: targetId});
            var $layer = $('#' + layerId);
            if(!$layer.length) $layer = $('<div class="display-layer"/>', {id: layerId}).appendTo(STR_BODY);
            options.layer = options.container = $layer.append(target);
        }

        target = $(target).addClass(STR_DISPLAY).attr('data-display-name', options.name);
        if(!target.parent().length) {
            target.appendTo(options.container);
        }
        if(!target.data(STR_ORIGINAL_CLASS)) target.data(STR_ORIGINAL_CLASS, target.attr('class'));
        return target;
    };

    Display.prototype.update = function($target, options, callback) {
        var that = this;
        var fillContent = function(content) {
            var template = options.template;
            if($.isFunction(template)) {
                content = template(content, options);
            } else if($.isStr(template)) {
                content = $.format(template, content);
            }

            if(content !== false) {
                if($.is$(content)) {
                    $target.empty().append(content);
                } else {
                    $target[options.contentType === 'text' ? 'text' : 'html'](content);
                }
            }
        };

        var remote = options.remote;
        if(options.load || remote) {
            var loadingClass = options.loadingClass;
            var ajaxEventName = 'ajaxError.mzui.' + STR_DISPLAY;
            var remoteError = options.remoteError;
            var isRemoteErrorAble = remoteError !== false && remoteError !== undefined;
            var stopLoading = function() {
                if(isRemoteErrorAble) $(document).off(ajaxEventName);
                $target.removeClass(loadingClass);
                $(STR_BODY).removeClass(STR_DISPLAY + '-' + loadingClass);
                $.callEvent('loaded', options['loaded'], that, that.$, options);
                callback && callback();
            };
            $target.addClass(loadingClass);
            $(STR_BODY).addClass(STR_DISPLAY + '-' + loadingClass);

            if(isRemoteErrorAble) {
                $(document).one(ajaxEventName, function(xhr, ajaxOptions, error) {
                    fillContent($.calValue(remoteError, that, [options, xhr, ajaxOptions, error]))
                });
            }

            if(options.load) {
                $target.load(options.load, stopLoading);
            } else if(options.remote) {
                $[options.remoteType === 'json' ? 'getJSON' : 'get'](remote, function(data, status) {
                    fillContent(data);
                    stopLoading();
                });
            }
        } else {
            var content = $.calValue(options.content, that, options),
                source = options.source;
            if(source) {
                source = $($.calValue(source, that, options));
                source = source.parent().length ? source.clone() : source;
                content = content ? source.html(content) : source;
            }
            fillContent(content);
            callback && callback();
        }
    };

    Display.prototype._getOptions = function(extraOptions) {
        var that = this;
        var options   = $.extend({}, that.options, extraOptions);

        var display = options.display;
        if(display && Display.plugs[display]) {
            options = $.calValue(Display.plugs[display], that, options);
        }

        if(!options.$target) options.$target = that._getTarget(options);

        return options;
    };

    Display.prototype.show = function(extraOptions, callback) {
        if($.isFunction(extraOptions)) {
            callback = extraOptions;
            extraOptions = null;
        }

        var that = this;
        var options = that._getOptions(extraOptions);
        that.last = options;

        var $target        = options.$target,
            activeClass    = options.activeClass,
            animate        = options.animate,
            element        = options.element,
            placement      = options.placement,
            $layer         = options.layer,
            suggestAnimate = 'bottom', 
            suggestArrow   = '',
            displayName    = options.name,
            arrow          = options.arrow,
            backdrop       = options.backdrop,
            $element       = $(element);

        $target.attr('class', 'invisible ' + $target.data(STR_ORIGINAL_CLASS) + ' ' + (options.targetClass || '')).removeClass(STR_HIDDEN);

        if($.callEvent('show', options.show, that, that.$, options) === false) return callback && callback();

        if($layer) $layer.removeClass(STR_HIDDEN);

        console.log(this, options);

        if(backdrop) {
            var backdropId = 'backdrop-' + displayName;
            $('#' + backdropId).remove();
            var $backdrop = $('<div class="display-backdrop"/>', {
                id: backdropId,
                type: options.display,
                zIndex: uuid++,
                'data-display-name': displayName
            }).appendTo(STR_BODY);
            if(backdrop === true) backdrop = 'fade';<commit_msg> * remove console.log.<commit_after>/* ========================================================================
 * ZUI: display.js
 * http://zui.sexy
 * ========================================================================
 * Copyright (c) 2014 cnezsoft.com; Licensed MIT
 * ======================================================================== */

!(function($, undefined, window){
    'use strict';

    var uuid                = 1200,
        TAP_EVENT_NAME      = $.TapName,
        STR_DISPLAY         = 'display',
        STR_ORIGINAL_CLASS  = 'orginalClass',
        STR_HIDDEN          = 'hidden',
        STR_BODY            = 'body',
        NAME                = 'mzui.' + STR_DISPLAY,
        inverseSide         = {left: 'right', bottom: 'top', top: 'bottom', right: 'left'};

    // Display constructor([$element], options)
    // $element is optional
    var Display = function($element, options) {
        var that = this;
        if($.isPlainObject($element)) {
            options  = $element;
            $element = null;
        }
        options = $.extend({element: $element}, Display.DEFAULT, $element ? $element.data() : null, options);

        var display = options.display;
        if(display && Display.plugs['_' + display]) {
            options = $.calValue(Display.plugs['_' + display], that, options);
        }

        var selector = options.selector;

        if(!$element)     options.trigger = null;
        if(!options.name) options.name    = STR_DISPLAY + uuid++;

        var triggerCallback = function(e) {
            var $this = $(this);
            var thisOptions = $this.data() || {};
            thisOptions.element = this;
            if($this.is('a')) {
                var href = $this.attr('href');
                if(href && href !== '#' && href.indexOf('##') < 0) {
                    if(!thisOptions.target && /^#[a-z]/i.test(href)) {
                        thisOptions.target = href;
                    } else if(!thisOptions.remote) {
                        thisOptions[(thisOptions.load === true || options.load) ? 'load' : 'remote'] = href;
                    }
                }
                if(e) e.preventDefault();
            }
            that[options.triggerMethod](thisOptions);
        };

        if(options.trigger) {
            selector ? $element.on(options.trigger, selector, triggerCallback) : $element.on(options.trigger, triggerCallback);
        }

        that.$       = $element;
        that.options = options;

        if(options.displayAuto) that.show();
        if(selector && $element) {
            var activeClass = options.activeClass;
            triggerCallback.call($element.find(selector).filter('[data-display-auto]' + (activeClass ? (',.' + activeClass) : '')));
        }
    };

    Display.prototype._getTarget = function(options) {
        var that = this;

        var target = $.calValue(options.target, that, options);
        if(target === '#displayTarget') {
            var targetId = 'displayTarget-' + options.name,
                layerId = 'displayLayer-' + options.name;
            $('#' + targetId).remove();
            target = $('<div class="display ' + STR_HIDDEN + '"/>', {id: targetId});
            var $layer = $('#' + layerId);
            if(!$layer.length) $layer = $('<div class="display-layer"/>', {id: layerId}).appendTo(STR_BODY);
            options.layer = options.container = $layer.append(target);
        }

        target = $(target).addClass(STR_DISPLAY).attr('data-display-name', options.name);
        if(!target.parent().length) {
            target.appendTo(options.container);
        }
        if(!target.data(STR_ORIGINAL_CLASS)) target.data(STR_ORIGINAL_CLASS, target.attr('class'));
        return target;
    };

    Display.prototype.update = function($target, options, callback) {
        var that = this;
        var fillContent = function(content) {
            var template = options.template;
            if($.isFunction(template)) {
                content = template(content, options);
            } else if($.isStr(template)) {
                content = $.format(template, content);
            }

            if(content !== false) {
                if($.is$(content)) {
                    $target.empty().append(content);
                } else {
                    $target[options.contentType === 'text' ? 'text' : 'html'](content);
                }
            }
        };

        var remote = options.remote;
        if(options.load || remote) {
            var loadingClass = options.loadingClass;
            var ajaxEventName = 'ajaxError.mzui.' + STR_DISPLAY;
            var remoteError = options.remoteError;
            var isRemoteErrorAble = remoteError !== false && remoteError !== undefined;
            var stopLoading = function() {
                if(isRemoteErrorAble) $(document).off(ajaxEventName);
                $target.removeClass(loadingClass);
                $(STR_BODY).removeClass(STR_DISPLAY + '-' + loadingClass);
                $.callEvent('loaded', options['loaded'], that, that.$, options);
                callback && callback();
            };
            $target.addClass(loadingClass);
            $(STR_BODY).addClass(STR_DISPLAY + '-' + loadingClass);

            if(isRemoteErrorAble) {
                $(document).one(ajaxEventName, function(xhr, ajaxOptions, error) {
                    fillContent($.calValue(remoteError, that, [options, xhr, ajaxOptions, error]))
                });
            }

            if(options.load) {
                $target.load(options.load, stopLoading);
            } else if(options.remote) {
                $[options.remoteType === 'json' ? 'getJSON' : 'get'](remote, function(data, status) {
                    fillContent(data);
                    stopLoading();
                });
            }
        } else {
            var content = $.calValue(options.content, that, options),
                source = options.source;
            if(source) {
                source = $($.calValue(source, that, options));
                source = source.parent().length ? source.clone() : source;
                content = content ? source.html(content) : source;
            }
            fillContent(content);
            callback && callback();
        }
    };

    Display.prototype._getOptions = function(extraOptions) {
        var that = this;
        var options   = $.extend({}, that.options, extraOptions);

        var display = options.display;
        if(display && Display.plugs[display]) {
            options = $.calValue(Display.plugs[display], that, options);
        }

        if(!options.$target) options.$target = that._getTarget(options);

        return options;
    };

    Display.prototype.show = function(extraOptions, callback) {
        if($.isFunction(extraOptions)) {
            callback = extraOptions;
            extraOptions = null;
        }

        var that = this;
        var options = that._getOptions(extraOptions);
        that.last = options;

        var $target        = options.$target,
            activeClass    = options.activeClass,
            animate        = options.animate,
            element        = options.element,
            placement      = options.placement,
            $layer         = options.layer,
            suggestAnimate = 'bottom', 
            suggestArrow   = '',
            displayName    = options.name,
            arrow          = options.arrow,
            backdrop       = options.backdrop,
            $element       = $(element);

        $target.attr('class', 'invisible ' + $target.data(STR_ORIGINAL_CLASS) + ' ' + (options.targetClass || '')).removeClass(STR_HIDDEN);

        if($.callEvent('show', options.show, that, that.$, options) === false) return callback && callback();

        if($layer) $layer.removeClass(STR_HIDDEN);

        if(backdrop) {
            var backdropId = 'backdrop-' + displayName;
            $('#' + backdropId).remove();
            var $backdrop = $('<div class="display-backdrop"/>', {
                id: backdropId,
                type: options.display,
                zIndex: uuid++,
                'data-display-name': displayName
            }).appendTo(STR_BODY);
            if(backdrop === true) backdrop = 'fade';
{'fraction_non_alphanumeric': 0.16366917293233083, 'fraction_numerical': 0.0010827067669172932, 'mean_word_length': 1.8328505707957063, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1500976', 'n_tokens_mistral': 4444, 'n_tokens_neox': 4230, 'n_words': 825}