(function ($, B) {

    /**
     * 相关 Web 界面的类集合
     * @module Beyondbit.Web
     * @submodule Web
     */
    var web = B.registerNameSpace("Web");

    /**
       * Layout 布局类
       * @class Layout
       * @static
       */

    var layout = B.registerNameSpace("Web.Layout");

    /**
        * 自适应布局
        * @method fix
        * @static
        * @param {String} jElement 自适应元素或容器元素,当options.autoElement元素为空,则当前元素为自适应元素
        * @param {json} [options] 可选项
        * @param {String} [options.autoElement] 自适应元素
        * @param {number} [options.reserve] 保留像素
        * @param {Function} [options.onCompleted] 自适应完成后事件
        * @param {number} [totalHeight] 总高度 
        * @example
        *
        *     <html>
        *         <div class="layout-fit">
        *           <div class="header">
        *               头部
        *           </div>
        *           <div class="middle layout-fit-item-auto">
        *               自适应
        *           </div>
        *           <div class="footer">
        *               底部
        *           </div>
        *       </div>
        *     </html>
        *
        *     <script>
        *     B.Web.Layout.fit(".layout-fit-item-auto");
        * 
        *     B.Web.Layout.fit(".layout-fit", {
        *         autoElement: ".layout-fit-item-auto"
        *     });
        *     </script>
        * 
        */
    layout.fit = function (jElement, options, height) {
        options = $.extend(true, {
            autoElement: null,
            reserve: 0,
            onCompleted: null
        }, options, {});

        var $target = $(jElement);
        var $container;

        var $autoElement;
        if (options.autoElement) {
            $autoElement = $(options.autoElement);
            $container = $target;
        } else {
            $autoElement = $target;
            $container = $target.parent();
        }

        height = height || $container.height();

        var $siblings = $container.children(":visible:not(script)").not($autoElement[0]);

        var boxHeight = $autoElement.outerHeight() - $autoElement.height();

        var totalOuterHeight = 0;
        $siblings.each(function () {
            totalOuterHeight += $(this).outerHeight();
        });

        var fitHeight = height - totalOuterHeight - boxHeight - options.reserve;
        $autoElement.height(fitHeight);

        if (options.onCompleted) {
            options.onCompleted(fitHeight);
        }
    };

})(jQuery, Beyondbit);
    
Top