Meddelande

Minska
No announcement yet.

JavascriptByggare (ASP.NET)

Minska
X
  • Filter
  • Klockan
  • Show
Clear All
new posts

  • JavascriptByggare (ASP.NET)

    En application för att bygga ihop valda javascript ifrån en resourcefil.

    Kod:
    Imports System.Resources
    Imports System.ComponentModel
    Imports Dean.Edwards
    
    Namespace InovArt.Common
        Public Class JavascriptAssembler
            Private rm As ResourceManager = ResourceManager.CreateFileBasedResourceManager("scripts.JavaScripts", _
                HttpContext.Current.Server.MapPath("App_GlobalResources"), Nothing)
            Private sb As New StringBuilder
            Private WithEvents _page As Page
            Private _encode As ECMAScriptPacker.PackerEncoding = Nothing
            Dim L_reg As New Generic.List(Of String)
    
            Public Sub New(ByVal page As Page)
                _page = page
            End Sub
    
            Public Sub New(ByVal page As Page, ByVal Encoding As ECMAScriptPacker.PackerEncoding)
                Me.New(page)
                _encode = Encoding
            End Sub
    
            Protected Sub Pre_Render(ByVal sender As Object, ByVal e As System.EventArgs) Handles _page.PreRender
                _page.ClientScript.RegisterClientScriptBlock(_page.GetType, "RenderContent", RenderContent, True)
                '_page.Trace.Warn("Write Javascript")
            End Sub
    
            <Description("Adds resource value based on the resource name parameter")> _
            Public Sub AddScript(ByVal ResourceName As String)
                If Not isScriptRegistred(ResourceName) Then
                    RenderContent = rm.GetString(ResourceName)
                    RegisterScript(ResourceName)
                End If
            End Sub
    
            <Description("Adds resource values based on the resource name parameter")> _
            Public Sub AddScript(ByVal ResourceNames As String())
                For i As Integer = 0 To ResourceNames.Length - 1
                    AddScript(ResourceNames(i))
                Next
            End Sub
    
            <Description("Adds a custom script as a string. The ´Name´-variable handles the registration.")> _
            Public Sub AddScript(ByVal Script As String, ByVal Name As String)
                If Not isScriptRegistred(Name) Then
                    RenderContent = Script
                    RegisterScript(Name)
                End If
            End Sub
    
            Private Property RenderContent() As String
                Set(ByVal value As String)
                    sb.AppendLine(value)
                End Set
                Get
                    If Not Me._encode = Nothing Then Return Obfuscate(sb.ToString())
                    Return sb.ToString()
                End Get
            End Property
    
            Private Function Obfuscate(ByVal str As String) As String
                Dim obfuscator As New ECMAScriptPacker()
                obfuscator.Encoding = _encode
                obfuscator.SpecialChars = True
                Return obfuscator.Pack(str)
            End Function
    
            Private Sub RegisterScript(ByVal str As String)
                L_reg.Add(str)
            End Sub
    
            Private Function isScriptRegistred(ByVal str As String) As Boolean
                Return L_reg.Contains(str)
            End Function
    
        End Class
    End Namespace
    Maskinöversatt till C#:
    Kod:
    using System.Resources; 
    using System.ComponentModel; 
    using Dean.Edwards; 
    
    namespace InovArt.Common 
    { 
        public class JavascriptAssembler 
        { 
            private ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("scripts.JavaScripts", HttpContext.Current.Server.MapPath("App_GlobalResources"), null); 
            private StringBuilder sb = new StringBuilder(); 
            private Page _page; 
            private ECMAScriptPacker.PackerEncoding _encode = null; 
            Generic.List<string> L_reg = new Generic.List<string>(); 
            
            public JavascriptAssembler(Page page) 
            { 
                _page = page; 
            } 
            
            public JavascriptAssembler(Page page, ECMAScriptPacker.PackerEncoding Encoding) : this(page) 
            { 
                _encode = Encoding; 
            } 
            
            protected void Pre_Render(object sender, System.EventArgs e) 
            { 
                _page.ClientScript.RegisterClientScriptBlock(_page.GetType, "RenderContent", RenderContent, true); 
                //_page.Trace.Warn("Write Javascript") 
            } 
            
            [Description("Adds resource value based on the resource name parameter")] 
            public void AddScript(string ResourceName) 
            { 
                if (!isScriptRegistred(ResourceName)) { 
                    RenderContent = rm.GetString(ResourceName); 
                    RegisterScript(ResourceName); 
                } 
            } 
            
            [Description("Adds resource values based on the resource name parameter")] 
            public void AddScript(string[] ResourceNames) 
            { 
                for (int i = 0; i <= ResourceNames.Length - 1; i++) { 
                    AddScript(ResourceNames(i)); 
                } 
            } 
            
            [Description("Adds a custom script as a string. The ´Name´-variable handles the registration.")] 
            public void AddScript(string Script, string Name) 
            { 
                if (!isScriptRegistred(Name)) { 
                    RenderContent = Script; 
                    RegisterScript(Name); 
                } 
            } 
            
            private string RenderContent { 
                get { 
                    if (!(this._encode == null)) 
                        return Obfuscate(sb.ToString()); 
                    return sb.ToString(); 
                } 
                set { sb.AppendLine(value); } 
            } 
            
            private string Obfuscate(string str) 
            { 
                ECMAScriptPacker obfuscator = new ECMAScriptPacker(); 
                obfuscator.Encoding = _encode; 
                obfuscator.SpecialChars = true; 
                return obfuscator.Pack(str); 
            } 
            
            private void RegisterScript(string str) 
            { 
                L_reg.Add(str); 
            } 
            
            private bool isScriptRegistred(string str) 
            { 
                return L_reg.Contains(str); 
            } 
            
        } 
    }

    Implementeras så hära:
    Kod:
    'Utan Obfuscator
    Dim jsa As New JavascriptAssembler(Me.Page)
    
    'Med Obfuscator
    Dim jsa As New JavascriptAssembler(Me.Page, Dean.Edwards.ECMAScriptPacker.PackerEncoding.None)
    
    'Som string() från Resourcefilen
    Dim functions As String() = {"getCookie", "PreLoadImages", "setCookie"}
    jsa.AddScript(functions)
    
    'Som string från Resourcefilen
    jsa.AddScript("Funktion")
    
    'Som Custom (Inte resourcefilen)
    jsa.AddScript("funktion egen(){//content};", "Eget_Namn")
    Maskinöversatt till C#:
    Kod:
    //Utan Obfuscator 
        JavascriptAssembler jsa = new JavascriptAssembler(this.Page); 
        
        //Med Obfuscator 
        JavascriptAssembler jsa = new JavascriptAssembler(this.Page, Dean.Edwards.ECMAScriptPacker.PackerEncoding.None); 
        
        //Som string() från Resourcefilen 
        string[] functions = {"getCookie", "PreLoadImages", "setCookie"}; 
        jsa.AddScript(functions); 
        
        //Som string från Resourcefilen 
        jsa.AddScript("Funktion"); 
        
        //Som Custom (Inte resourcefilen) 
        jsa.AddScript("funktion egen(){//content};", "Eget_Namn");
    Vill gärna ha kritik på denna applikation.
    I denna tråd.
    Bifogade filer
    3
    Host...kastas i papperskorgen
    33.33%
    1
    Bra-å-ha-sak
    66.67%
    2
    Exakt vad jag behövde
    0.00%
    0
    Last edited by Jesper T; 2007-12-12, 19:58. Anledning: Lagt till översättning till C#
    Vänligen Jesper
    Etisk kommunikation
Working...
X