PopupLayout.js 5.27 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
"use strict";
(function(root, factory) {
    /* global define, DevExpress, jQuery */
    if (typeof define === 'function' && define.amd) {
        define(function(require, exports, module) {
            module.exports = factory(
                require("jquery"),
                require("framework/html/presets").layoutSets,
                require("framework/html/layout_controller").DefaultLayoutController,
                require("../Simple/SimpleLayout.js").SimpleLayoutController,

                require("ui/popup")
            );
        });
    } else {
        root.DevExpress.layouts = root.DevExpress.layouts || {};
        root.DevExpress.layouts.PopupLayout = factory(
            jQuery,
            DevExpress.framework.html.layoutSets,
            DevExpress.framework.html.DefaultLayoutController,
            DevExpress.layouts.SimpleLayout.SimpleLayoutController
        );
        root.DevExpress.framework.html.OverlayLayoutControllerBase = root.DevExpress.layouts.PopupLayout.OverlayLayoutControllerBase;
        root.DevExpress.framework.html.PopupLayoutController = root.DevExpress.layouts.PopupLayout.PopupLayoutController;
    }
}(window, function($, layoutSets, DefaultLayoutController, SimpleLayoutController) {

    var exports = {},
        abstract = DefaultLayoutController.abstract;

    var OverlayLayoutControllerBase = DefaultLayoutController.inherit({
        ctor: function(options) {
            options = options || {};
            this.callBase(options);
            if (!options.childController) {

                this.childController = new SimpleLayoutController();
            } else {
                this.childController = options.childController;
            }
            this.contentContainerSelector = options.contentContainerSelector;
        },

        _initChildController: function(options) {
            var that = this,
                $targetViewPort = that._$mainLayout.find(this.contentContainerSelector);

            that.childController.init($.extend({}, options, {
                $viewPort: $targetViewPort
            }));

            $.each(["viewRendered", "viewShowing", "viewReleased", "viewHidden"], function(_, callbacksPropertyName) {
                that.childController.on(callbacksPropertyName, function(args) {
                    that.fireEvent(callbacksPropertyName, [args]);
                });
            });
        },

        _ensureChildController: function(controller, layoutName) {
            if (!controller) {
                throw new Error(layoutName + "Controller is not found but it is required by the '" + this.name + "' layout for specified platform and device. Make sure the " + layoutName + ".* files are referenced in your main *.html file or specify other platform and device.");
            }
        },

        _base: function() {
            return DefaultLayoutController.prototype;
        },

        _showContainerWidget: abstract,

        _hideContainerWidget: abstract,

        init: function(options) {
            options = options || {};
            this.callBase(options);
            this._initChildController(options);
        },

        activate: function($target) {
            var that = this,
                result;

            that.childController.activate();
            that._base().activate.call(that, $target);
            result = that._showContainerWidget($target);

            return result;
        },

        deactivate: function() {
            var that = this,
                result;

            result = that._hideContainerWidget();

            result.done(function() {
                that._base().deactivate.call(that);
                that.childController.deactivate();
            });

            return result;
        },

        showView: function(viewInfo, direction) {
            return this.childController.showView(viewInfo, direction);
        }
    });

    var PopupLayoutController = OverlayLayoutControllerBase.inherit({
        ctor: function(options) {
            options = options || {};
            options.name = options.name || "popup";
            options.contentContainerSelector = options.contentContainerSelector || ".child-controller-content";
            this.isOverlay = true;
            this._targetContainer = options.targetContainer;
            this.callBase(options);
        },

        init: function(options) {
            this.callBase(options);
            this._popup = this._$mainLayout.find(".popup-container").dxPopup("instance");
            if (this._targetContainer) {
                this._popup.option("container", this._targetContainer);
            }
        },

        _showContainerWidget: function() {
            return this._popup.show();
        },

        _hideContainerWidget: function() {
            return this._popup.hide();
        }
    });

    $.each(["navbar", "simple", "slideout", "pivot", "split"], function(index, name) {
        layoutSets[name] = layoutSets[name] || [];
        $.each(layoutSets[name], function(index, layoutInfo) {
            layoutInfo.modal = false;
        });
        layoutSets[name].push({
            modal: true,
            controller: new PopupLayoutController()
        });
    });

    exports.OverlayLayoutControllerBase = OverlayLayoutControllerBase;
    exports.PopupLayoutController = PopupLayoutController;

    return exports;

}));