﻿/// Filename: Repeater.js
/// Description: Corvus ClientSide Repeater Control
/// Copyright (C) CorvusInfo d.d. All rights reserved.
/// 2008. a.d.

Type.registerNamespace("CorvusCMS.UI");

///***** CORVUS Client-Side Repeater class START *****

CorvusCMS.UI.Repeater = function(element)
{
    CorvusCMS.UI.Repeater.initializeBase(this, [element]);
    ///Collection to bind to
    this._arrayCollection = null;
    this._pagedListCollection = null;
    
    ///Templates properties (CorvusCMS.UI.Template)
    this._headerTemplate = null;
    this._itemTemplate = null;
    this._separatorTemplate = null;
    this._footerTemplate = null;
    this._pagerTemplate = null;
    
    ///Private Fields section
    this._preRenderTemplate = null;
    this._itemOutput = null;
    this._finalOutput = '';
    this._currentItemIndex = 0;
    
    //repeater container click handler delegate
    this._rptOnClickHandler = null;
    //if we use PagedList as input, set default items per page value
    this._itemsPerPage = 10;
    
    //Some flags
    this._renderTopPager = false; 
    this._renderBottomPager = false;
    
    this._listenControl = true;
    
    this._renderAsText = false;
    
}

CorvusCMS.UI.Repeater.prototype =
{
    initialize: function() {
        CorvusCMS.UI.Repeater.callBaseMethod(this, 'initialize');

        if (this.get_initialPagedList() || this._pagedListCollection || this._arrayList)
            this.onFirstPage();
        //        else
        //            this.set_enabled(true);

    },

    dispose: function() {
        if (this.get_repeaterContent())
            document.body.removeChild(this.get_repeaterContent());

        CorvusCMS.UI.Repeater.callBaseMethod(this, 'dispose');
    },

    ///*****Properties section
    get_Collection: function() {
        if (this._initialPagedList != null) {
            var iC = this._initialPagedList;
            this._initialPagedList = null;
            return iC;
        }
        return this._pagedListCollection != null ? this._pagedListCollection.Items : this._arrayCollection;
    },

    //*** Returns collection that repeater was bound with
    set_Collection: function(value) {
        if (value && value.__type && value.__type == 'Corvus.Web.PagedList') {
            this._pagedListCollection = value;
        }
        else
            this._arrayCollection = value;
    },

    get_pagedList: function() {
        return this._pagedListCollection;
    },

    get_headerTemplate: function() {
        return this._headerTemplate;
    },

    set_headerTemplate: function(value) {
    if (this._headerTemplate !== value) {
        this._headerTemplate = value;
        }
    },

    get_itemTemplate: function() {
        return this._itemTemplate;
    },

    set_itemTemplate: function(value) {
    if (this._itemTemplate !== value) {
            this._itemTemplate = value; 
        }
    },

    get_separatorTemplate: function() {
        return this._separatorTemplate;
    },

    set_separatorTemplate: function(value) {
        if (this._separatorTemplate !== value) {
            this._separatorTemplate = value;
            }
    },

    get_footerTemplate: function() {
        return this._footerTemplate;
    },

    set_footerTemplate: function(value) {
    if (this._footerTemplate !== value) {
            this._footerTemplate = value;
        }
    },

    get_pagerTemplate: function() {
        return this._pagedListCollection != null ? this._pagerTemplate : null;
    },

    set_pagerTemplate: function(value) {
    if (this._pagerTemplate !== value) {
            this._pagerTemplate = value;
        }
    },

    //Buffer element where we render all templates and then apply it to get_element() at once
    get_repeaterContent: function() {
        if (this._repeaterContent == null) {
            var a = document.createElement('div');
            a.style.display = 'none';
            a.id = 'rpt_cnt_' + this.get_id();
            document.body.appendChild(a);
            this._repeaterContent = $get('rpt_cnt_' + this.get_id());
        }
        return this._repeaterContent;
    },

    get_renderTopPager: function() {
        return this._renderTopPager;
    },

    set_renderTopPager: function(value) {
        if (this._renderTopPager !== value) {
            this._renderTopPager = value;
        }
    },

    get_renderBottomPager: function() {
        return this._renderBottomPager;
    },

    set_renderBottomPager: function(value) {
        if (this._renderBottomPager !== value) {
            this._renderBottomPager = value;
        }
    },

    get_ItemsPerPage: function() {
        return this._itemsPerPage;
    },

    set_ItemsPerPage: function(value) {
        if (this._itemsPerPage !== value) {
            this._itemsPerPage = value;
        }
    },

    ///<summary>Public DataBind Method</summary>
    bindCollection: function(collection) {
        if (collection != null)
            this.set_Collection(collection);
        else if (this._arrayList != null)
            this.set_Collection(this.getPageFromArray(this._arrayList));

        this._bindCollection();
    },
    
    get_renderAsText: function() {
        return this._renderAsText;
    },

    set_renderAsText: function(value) {
    
        if(this.get_itemDataBoundHandler())
            throw new Error("Repeater can not be rendered as text when ItemDataBound handler is set.");
            
        if (this._renderAsText !== value) {
            this._renderAsText = value;
        }
    },

    ///Binds repeater control with collection in get_Collection() property to the appropriate template for given property.
    ///Applies rendered template markup to repeaters parent element [ this.get_element() ]
    _bindCollection: function() {
        this.get_element().innerHTML =
         '';

        this.set_enabled(true);

        if (!this._renderAsText) {
            //Bind top pager
            if (this.get_pagerTemplate() && this.get_renderTopPager()) {
                this.get_pagerTemplate().renderHtmlInElement(this.get_pagedList(), this.get_element(), 0, this.get_id());
                this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_pagedList(), 0, CorvusCMS.UI.TemplateType.PagerTemplate));
            }

            //Bind header
            if (this.get_headerTemplate()) {
                this.get_headerTemplate().renderHtmlInElement(null, this.get_element(), 0, this.get_id());
                this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_Collection()[item], 0, CorvusCMS.UI.TemplateType.HeaderTemplate));
            }

            //Bind items
            for (var item = 0; item < this.get_Collection().length; item++) {
                ///bind in template
                this.get_itemTemplate().renderHtmlInElement(this.get_Collection()[item], this.get_element(), item, this.get_id());
                this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_Collection()[item], this._currentItemIndex, CorvusCMS.UI.TemplateType.ItemTemplate));

                //Bind separator START
                if (this.get_separatorTemplate() && item < this.get_Collection().length - 1) {
                    this.get_separatorTemplate().renderHtmlInElement(null, this.get_element(), this._currentItemIndex, this.get_id());
                    this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_Collection()[item], this._currentItemIndex, CorvusCMS.UI.TemplateType.SeparatorTemplate));
                } //Bind separator END

                this._currentItemIndex++;
            }

            //Bind footer
            if (this.get_footerTemplate()) {
                this.get_footerTemplate().renderHtmlInElement(null, this.get_element(), 0, this.get_id());
                this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_Collection()[item], 0, CorvusCMS.UI.TemplateType.FooterTemplate));
            }

            //Bind bottom pager
            if (this.get_pagerTemplate() && this.get_renderBottomPager()) {
                this.get_pagerTemplate().renderHtmlInElement(this.get_pagedList(), this.get_element(), 1, this.get_id());
                this._raiseEvent("itemDataBound", new CorvusCMS.UI.ItemDataBoundArgs(this.get_id(), this.get_pagedList(), 1, CorvusCMS.UI.TemplateType.PagerTemplate));
            }
        }
        else {

            var tempContainer = "";

            if (this.get_pagerTemplate() && this.get_renderTopPager()) {
                tempContainer += this.get_pagerTemplate().getOutputAsString(this.get_pagedList());
            }

            if (this.get_headerTemplate()) {
                tempContainer += this.get_headerTemplate().getOutputAsString(null);
            }

            var items = this.get_Collection().Items;
            if (!items)
                items = this.get_Collection();

            for (var item = 0; item < items.length; item++) {
                tempContainer += this.get_itemTemplate().getOutputAsString(this.get_Collection()[item], item);

                if (this.get_separatorTemplate() && item < this.get_Collection().length - 1) {
                    tempContainer += this.get_separatorTemplate().getOutputAsString(null);
                }
                this._currentItemIndex++;
            }

            if (this.get_footerTemplate()) {
                tempContainer += this.get_footerTemplate().getOutputAsString(null);
            }

            if (this.get_pagerTemplate() && this.get_renderBottomPager()) {
                tempContainer += this.get_pagerTemplate().getOutputAsString(this.get_pagedList());
            }

            this.get_element().innerHTML = tempContainer;
        }

        this._currentItemIndex = 0;
    },

    //    _onPage: function(args) {
    //        CorvusCMS.UI.Repeater.callBaseMethod(this, '_onPage', [args]);
    //    },

    _onSucceded: function(pagedList, customArgument) {
        this.set_Collection(pagedList);
        this._bindCollection();
        CorvusCMS.UI.Repeater.callBaseMethod(this, '_onSucceded', [pagedList, customArgument]);
        this.set_enabled(true);
    }

}
CorvusCMS.UI.Repeater.registerClass("CorvusCMS.UI.Repeater", CorvusCMS.UI.PagedListControl);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();