﻿// AjaxWebParts.js
// Ajax WebParts Framework.

Type.registerNamespace('Rostrud.Web.UI.Controls.WebParts');

Rostrud.Web.UI.Controls.WebParts.WebPart = function (element) {
    Rostrud.Web.UI.Controls.WebParts.WebPart.initializeBase(this, [element]);

    this._titleElement = null;
    this._zone = null;
    this._zoneIndex = 0;
    this._allowZoneChange = true;
}

Rostrud.Web.UI.Controls.WebParts.WebPart.prototype = {

    get_allowZoneChange : function() {
        return this._allowZoneChange;
    },

    set_allowZoneChange : function(value) {
        this._allowZoneChange = value;
    },

    get_titleElement : function() {
        return this._titleElement;
    },

    set_titleElement : function(value) {
        this._titleElement = value;
    },

    get_zone : function() {
        return this._zone;
    },

    set_zone : function(value) {
        this._zone = value;
    },

    get_zoneIndex : function() {
        return this._zoneIndex;
    },

    set_zoneIndex : function(value) {
        this._zoneIndex = value;
    },

    initialize : function() {
        Rostrud.Web.UI.Controls.WebParts.WebPart.callBaseMethod(this, "initialize");
        if (this._titleElement && this._zone.get_webPartManager().get_allowPageDesign() && this._zone.get_allowLayoutChange()) {
            var element = this.get_element();
            $addHandler(this._titleElement, "mousedown", Function.createDelegate(this, this._mouseDownHandler));
            this._titleElement.style.cursor = "move";
        }
    },
    
    dispose : function () {
        Rostrud.Web.UI.Controls.WebParts.WebPart.callBaseMethod(this, "dispose");
    },

    _mouseDownHandler: function(domEvent) {
        window._event = domEvent;
        this._zone.startDragDrop(this);
        domEvent.preventDefault();
    }
}

Rostrud.Web.UI.Controls.WebParts.WebPart.descriptor = {
    properties: [ {name: 'titleElement', isDomElement: true},
                  {name: 'zone', type: Object},
                  {name: 'zoneIndex', type: Number},
                  {name: 'allowZoneChange', type: Boolean} ]
}

Rostrud.Web.UI.Controls.WebParts.WebPart.registerClass("Rostrud.Web.UI.Controls.WebParts.WebPart", Sys.UI.Control);

Rostrud.Web.UI.Controls.WebParts.WebPartManager = function (element) {
    Rostrud.Web.UI.Controls.WebParts.WebPartManager.initializeBase(this, [element]);

    this._allowPageDesign = false;
}

Rostrud.Web.UI.Controls.WebParts.WebPartManager.prototype = {

    get_allowPageDesign : function() {
        return this._allowPageDesign;
    },

    set_allowPageDesign : function(value) {
        this._allowPageDesign = value;
    },

    initialize : function() {
        Rostrud.Web.UI.Controls.WebParts.WebPartManager.callBaseMethod(this, "initialize");

        var baseShowHelp = Function.createDelegate(__wpm, __wpm.ShowHelp);
        __wpm.ShowHelp = function(helpUrl, helpMode) {
            var supportedHelpMode;
            if (helpMode == 0 && !window.showModalDialog) {
                supportedHelpMode = 1;
            }
            else {
                supportedHelpMode = helpMode;
            }

            baseShowHelp(helpUrl, supportedHelpMode);
        }
    }
}

Rostrud.Web.UI.Controls.WebParts.WebPartManager.descriptor = {
    properties: [ {name: 'allowPageDesign', type: Boolean} ]
}

Rostrud.Web.UI.Controls.WebParts.WebPartManager.registerClass("Rostrud.Web.UI.Controls.WebParts.WebPartManager", Sys.UI.Control);

Rostrud.Web.UI.Controls.WebParts.WebPartZone = function (element) {
    Rostrud.Web.UI.Controls.WebParts.WebPartZone.initializeBase(this, [element]);

    this._dataType = "WebPart";

    this._allowLayoutChange = true;
    this._uniqueId = 0;
    this._webPartManager = null;

    this._dropIndex = -1;
    this._floatContainer = null;
    this._whidbeyZone = null;
}

Rostrud.Web.UI.Controls.WebParts.WebPartZone.prototype = {

    get_allowLayoutChange : function() {
        return this._allowLayoutChange;
    },

    set_allowLayoutChange : function(value) {
        this._allowLayoutChange = value;
    },

    get_uniqueId : function() {
        return this._uniqueId;
    },

    set_uniqueId : function(value) {
        this._uniqueId = value;
    },

    get_webPartManager : function() {
        return this._webPartManager;
    },

    set_webPartManager : function(value) {
        this._webPartManager = value;
    },

    _createFloatContainer : function(webPart) {
        var floatContainer = document.createElement("div");
        floatContainer.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(opacity=0.75);";
        floatContainer.style.opacity = "0.75";
        floatContainer.style.position = "absolute";
        floatContainer.style.zIndex = 32000;

        var webPartElement = webPart.get_element();
        var currentLocation = Sys.UI.DomElement.getLocation(webPartElement);
        Sys.UI.DomElement.setLocation(floatContainer, currentLocation.x, currentLocation.y);
        floatContainer.style.display = "block";
        floatContainer.style.width = webPartElement.offsetWidth + "px";
        floatContainer.style.height = webPartElement.offsetHeight + "px";
        floatContainer.appendChild(webPartElement.cloneNode(true));

        return floatContainer;
    },

    initialize : function() {
        Rostrud.Web.UI.Controls.WebParts.WebPartZone.callBaseMethod(this, "initialize");
        var element = this.get_element();
        for (var i=0; i < __wpm.zones.length; i++) {
            if (__wpm.zones[i].zoneElement == element) {
                this._whidbeyZone = __wpm.zones[i];
                break;
            }
        }
                        
        if (this._webPartManager.get_allowPageDesign() && this.get_allowLayoutChange) {
            AjaxControlToolkit.DragDropManager.registerDropTarget(this);
        }
    },
    
    dispose : function() {
        if (this._webPartManager.get_allowPageDesign() && this.get_allowLayoutChange) {
            AjaxControlToolkit.DragDropManager.unregisterDropTarget(this);
        }
        this._whidbeyZone = null;
        
        Rostrud.Web.UI.Controls.WebParts.WebPartZone.callBaseMethod(this, "dispose");
    },

    startDragDrop : function (webPart) {
        __wpm.UpdatePositions();

        this._floatContainer = this._createFloatContainer(webPart);
        document.body.appendChild(this._floatContainer);
        AjaxControlToolkit.DragDropManager.startDragDrop(this, this._floatContainer, webPart);
    },

    get_dragDataType : function() {
        return this._dataType;
    },

    getDragData : function(context) {
        return context;
    },

    get_dragMode : function() {
        return AjaxControlToolkit.DragMode.Copy;
    },

    onDragStart : function() {
    },

    onDrag : function() {
    },

    onDragEnd : function(cancelled) {
        Sys.Debug.assert(this._floatContainer != null, "_floatContainer is null");
        Sys.Debug.assert(this._floatContainer.parentNode == document.body, "_floatContainer is not parented to document.body");

        document.body.removeChild(this._floatContainer);
    },

    get_dropTargetElement : function() {
        return this._whidbeyZone.webPartTable;
    },

    canDrop : function(dragMode, dataType, data) {
        var webPart = data;
        return ((dragMode == AjaxControlToolkit.DragMode.Copy) &&
                (dataType == this._dataType) &&
                (Rostrud.Web.UI.Controls.WebParts.WebPart.isInstanceOfType(webPart)) &&
                (webPart.get_allowZoneChange() || (webPart.get_zone() == this)) &&
                (this._getDropIndex() != -1));
    },

    drop : function(dragMode, dataType, data) {
        Sys.Debug.assert(this._dropIndex != -1);
        this._whidbeyZone.ToggleDropCues(false, this._dropIndex, false);

        var webPart = data;
        if (this._webPartMoved(webPart, this, this._dropIndex)) {
            var eventTarget = this._uniqueId;
            var eventArgument = "Drag:" + webPart.get_id() + ":" + this._dropIndex;
            __doPostBack(eventTarget, eventArgument);
        }
    },

    _webPartMoved : function(webPart, dropZone, dropIndex) {
        if (dropZone != webPart.get_zone()) {
            return true;
        }
        if (dropIndex == webPart.get_zoneIndex() || dropIndex == (webPart.get_zoneIndex() + 1)) {
            return false;
        }
        return true;
    },

    onDragEnterTarget : function(dragMode, dataType, data) {
        var dropIndex = this._getDropIndex();
        this._whidbeyZone.ToggleDropCues(true, dropIndex, false);
        this._dropIndex = dropIndex;
    },

    onDragLeaveTarget : function(dragMode, dataType, data) {
        this._whidbeyZone.ToggleDropCues(false, this._dropIndex, false);
    },

    onDragInTarget : function() {
        var dropIndex = this._getDropIndex();
        if (dropIndex != this._dropIndex) {
            this._whidbeyZone.ToggleDropCues(false, this._dropIndex, true);
            this._whidbeyZone.ToggleDropCues(true, dropIndex, true);
            this._dropIndex = dropIndex;
        }
    },

    _getDropIndex : function() {
        var pageLocation = __wpGetPageEventLocation(window._event, false);
        return this._whidbeyZone.GetWebPartIndex(pageLocation);
    }
}

Rostrud.Web.UI.Controls.WebParts.WebPartZone.descriptor = {
    properties: [ {name: 'uniqueId', type: String},
                  {name: 'webPartManager', type: Object},
                  {name: 'allowLayoutChange', type: Boolean} ]
}

Rostrud.Web.UI.Controls.WebParts.WebPartZone.registerClass("Rostrud.Web.UI.Controls.WebParts.WebPartZone",
    Sys.UI.Control, AjaxControlToolkit.IDragSource, AjaxControlToolkit.IDropTarget);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();