Skip to content

Commit

Permalink
v. 3.3.0 - Added PostBack method (client script) and corresponding se…
Browse files Browse the repository at this point in the history
…rver event.
  • Loading branch information
jesperhoy committed May 1, 2021
1 parent 07770c4 commit 78fae02
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Note: When using .vue files, the JavaScript output is cached between requests (i
- `Options` - Vue options for the component in JavaScript format. Only used with simple in-line templates.
- `SquashWS` - Boolean value (default true) indicating if all white space in HTML templates should be squashed (sequences of space, `<LF>`, `<CR>`, `<Tab>` are replaced with a single space).

Events (server side):
- `PostBack(eventArgument)`- Raised when script in the Vue.js component calls `this.$options.PostBack(eventArgument)`. Note that the server control must have an `ID` attribute for this to be available.

- **App**

Makes it easy to render a Vue.js application instance.
Expand All @@ -126,6 +129,9 @@ Note: When using .vue files, the JavaScript output is cached between requests (i
- `Mount` - Boolean value (default true) indicating if a `<div>` tag with a random id should be generated and the Vue instance mounted to this.
- `SquashWS` - Boolean value (default true) indicating if all white space in HTML templates should be squashed (sequences of space, `<LF>`, `<CR>`, `<Tab>` are replaced with a single space).

Events (server side):
- `PostBack(eventArgument)`- Raised when script in the Vue.js application calls `this.$options.PostBack(eventArgument)`. Note that the server control must have an `ID` attribute for this to be available.


- **ServerTemplate**

Expand Down
8 changes: 4 additions & 4 deletions src/Module1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

Friend Module Module1

Friend Function MakeVueOptions(content As String, prpFile As String, prpOptions As String, prpsquashWS As Boolean) As String
Friend Function MakeVueOptions(content As String, prpFile As String, prpOptions As String, prpsquashWS As Boolean, embedExtra As String) As String
Dim ctx = System.Web.HttpContext.Current
If String.IsNullOrEmpty(prpFile) Then
REM In-line template
If content.Length = 0 Then Throw New Exception("Template is empty")
If content.StartsWith("<template>", StringComparison.InvariantCultureIgnoreCase) Then
If Not String.IsNullOrEmpty(prpOptions) Then Throw New Exception("Control cannot have 'Options' property when content starts with <template>")
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, prpsquashWS, content) & ")()"
Return "(" & VueFilesToJS.Compile(ctx.Server.MapPath("~/"), ctx.Request.Url.AbsolutePath, embedExtra, prpsquashWS, content) & ")()"
Else
prpOptions = If(prpOptions, "").Trim
If prpOptions.Length = 0 Then prpOptions = "{}"
If Not prpOptions.StartsWith("{") OrElse Not prpOptions.EndsWith("}") Then Throw New Exception("Invalid 'Options' property")
If prpsquashWS Then content = VueFilesToJS.SquashWhiteSpace(content)
Return "{" &
Return "{" & embedExtra &
"template:" & VueFilesToJS.JSStringEncode(content) & "," &
prpOptions.Substring(1)
End If
Expand All @@ -32,7 +32,7 @@ Friend Module Module1
Dim obj = ctx.Cache.Get(CacheKey)
If obj Is Nothing Then
Dim FileList As New List(Of String)
f = VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, prpsquashWS, Nothing, AddressOf FileList.Add)
f = VueFilesToJS.Compile(ctx.Server.MapPath("~/"), prpFile, embedExtra, prpsquashWS, Nothing, AddressOf FileList.Add)
ctx.Cache.Add(CacheKey, f, New Web.Caching.CacheDependency(FileList.ToArray), Web.Caching.Cache.NoAbsoluteExpiration, Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Normal, Nothing)
Else
f = DirectCast(obj, String)
Expand Down
6 changes: 3 additions & 3 deletions src/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("Vue.js ASP.NET Web Forms helpers")>
<Assembly: AssemblyCopyright("Copyright © 2018-2019 Jesper Høy")>
<Assembly: AssemblyCopyright("Copyright © 2018-2021 Jesper Høy")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>
Expand All @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("3.2.0.0")>
<Assembly: AssemblyFileVersion("3.2.0.0")>
<Assembly: AssemblyVersion("3.3.0.0")>
<Assembly: AssemblyFileVersion("3.3.0.0")>
10 changes: 9 additions & 1 deletion src/VueApp.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
<ParseChildren>
Public Class App
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler

Property File As String
Property VarName As String
Property Options As String
Property Mount As Boolean = True
Property SquashWS As Boolean = True

Public Event PostBack(eventArgument As String)

Protected Overrides Sub Render(writer As HtmlTextWriter)
Dim sw = New IO.StringWriter
Dim htw = New HtmlTextWriter(sw)
MyBase.RenderChildren(htw)
Dim Content = sw.ToString.Trim

Dim opt = MakeVueOptions(Content, File, Options, SquashWS)
Dim opt = MakeVueOptions(Content, File, Options, SquashWS,
If(Me.ID Is Nothing, "", "PostBack(ea) {" & Me.Page.ClientScript.GetPostBackEventReference(Me, "#").Replace("'#'", "ea") & "},"))

If Mount Then
Dim DivName = "VueApp"
Expand Down Expand Up @@ -46,4 +50,8 @@ Public Class App
REM nothing
End Sub

Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
RaiseEvent PostBack(eventArgument)
End Sub

End Class
11 changes: 10 additions & 1 deletion src/VueComponent.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
<ParseChildren>
Public Class Component
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler

Property File As String
Property Name As String
Property Options As String
Property SquashWS As Boolean = True

Public Event PostBack(eventArgument As String)

Protected Overrides Sub Render(writer As HtmlTextWriter)
If String.IsNullOrEmpty(Name) Then
If String.IsNullOrEmpty(File) Then Throw New Exception("'Name' or 'File' property is required")
Expand All @@ -21,7 +24,9 @@ Public Class Component
MyBase.RenderChildren(htw)
Dim Content = sw.ToString.Trim

Dim opt = MakeVueOptions(Content, File, Options, SquashWS)
Dim opt = MakeVueOptions(Content, File, Options, SquashWS,
If(Me.ID Is Nothing, "", "PostBack(ea) {" & Me.Page.ClientScript.GetPostBackEventReference(Me, "#").Replace("'#'", "ea") & "},"))

writer.WriteLine("<script>")
writer.WriteLine("Vue.component('" & Name & "'," & opt & ");")
writer.WriteLine("</script>")
Expand All @@ -35,4 +40,8 @@ Public Class Component
REM nothing
End Sub

Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
RaiseEvent PostBack(eventArgument)
End Sub

End Class
7 changes: 4 additions & 3 deletions src/VueFilesToJS.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
Private SquashWS As Boolean
Private FileReadCallback As Action(Of String) = Nothing

Public Shared Function Compile(wsRootPath As String, sourceFile As String, Optional squashWS As Boolean = True, Optional rootFileContent As String = Nothing, Optional fileReadCallback As Action(Of String) = Nothing) As String
Public Shared Function Compile(wsRootPath As String, sourceFile As String, embedExtra As String, Optional squashWS As Boolean = True, Optional rootFileContent As String = Nothing, Optional fileReadCallback As Action(Of String) = Nothing) As String
If wsRootPath.EndsWith("\") Then wsRootPath = wsRootPath.Substring(0, wsRootPath.Length - 1)
Dim inst = New VueFilesToJS With {.WsRoot = wsRootPath, .SquashWS = squashWS, .FileReadCallback = fileReadCallback}
Return inst.ProcRoot(sourceFile, rootFileContent)
Return inst.ProcRoot(sourceFile, rootFileContent, embedExtra)
End Function

Private Sub New()
REM private constructor so only Compile function can create instance
End Sub

Private Function ProcRoot(vueFile As String, FileContent As String) As String
Private Function ProcRoot(vueFile As String, FileContent As String, embedExtra As String) As String
Dim res = ParseVueFile(vueFile, Nothing, FileContent)

Dim sb As New System.Text.StringBuilder
Expand All @@ -25,6 +25,7 @@
Next

sb.AppendLine(" return {" & vbCrLf &
embedExtra &
" template: " & JSStringEncode(res.Template) & "," & vbCrLf &
res.Script.Substring(1).Trim & ";")
sb.AppendLine("}")
Expand Down

0 comments on commit 78fae02

Please sign in to comment.