HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/CMSWebParts/Pux/HTML/JavascriptAppBase.ascx.cs
using CMS.Helpers;
using CMS.PortalControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CMSWebParts_Pux_HTML_JavascriptAppBase : CMSAbstractWebPart
{
    public bool UseNoJSClass
    {
        get
        {
            return ValidationHelper.GetBoolean(GetValue("UseNoJSClass"), true);
        }
        set
        {
            SetValue("UseNoJSClass", value);
        }
    }

    Literal _addedLiteral = null;
    string _customHeadKey = "<!--appbase-->";

    string Script
    {
        get
        {
            string s = @"
app = (function() {
	var onReadyCallbacks = [];

	var executeCallbacks = function(collection) {
		for (var i = 0; i < collection.length; i++) {
			collection[i].call(this);
		};
	}

	return {
		ready: function(callback) {
			onReadyCallbacks.push(callback);
		},
		fireReady: function() {
			executeCallbacks(onReadyCallbacks);
		}
	}
})();
";
            if (UseNoJSClass)
            {
                s += "document.getElementsByTagName( 'html' )[0].setAttribute( 'class', document.getElementsByTagName('html')[0].getAttribute('class').replace('no-js', 'js'))";
            }

            return s;
        }
    }

    string readyScript = @"
app.fireReady();
";

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        var cacheKey = "pux.html.javascriptappbase";
        var isInitialized = ValidationHelper.GetBoolean(RequestStockHelper.GetItem(cacheKey), false);
        if (!isInitialized)
        {
            _addedLiteral = new Literal();
            _addedLiteral.Text = string.Format("<script>{0}</script>", CMS.New.Instance<IJavaScriptMinifier>().Minify(Script));

            Page.Header.Controls.Add(_addedLiteral);

            RequestStockHelper.Add(cacheKey, true);
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (_addedLiteral != null)
        {
            // check if there is a custom place in the header to move the javascript
            foreach (var control in Page.Header.Controls)
            {
                if (control is Literal)
                {
                    var ltl = control as Literal;
                    if (ltl.Text.Contains(_customHeadKey))
                    {
                        // append app javascript to the custom location
                        ltl.Text = ltl.Text.Replace(_customHeadKey, _addedLiteral.Text);
                        // and remove the originally generated custom literal
                        Page.Header.Controls.Remove(_addedLiteral);

                        break;
                    }
                }
            }

            // append ready event to the end of the page
            ScriptHelper.RegisterStartupScript(Page, typeof(string), "AppReady", string.Format("<script>{0}</script>", readyScript));
        }

        if (UseNoJSClass)
        {
            CMS.UIControls.ContentPage page = this.Page as CMS.UIControls.ContentPage;
            page.XmlNamespace += " class='no-js'";
        }
    }
}