/
dhtml.lua
184 lines (132 loc) · 3.42 KB
/
dhtml.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
local PANEL = {}
AccessorFunc( PANEL, "m_bScrollbars", "Scrollbars", FORCE_BOOL )
AccessorFunc( PANEL, "m_bAllowLua", "AllowLua", FORCE_BOOL )
function PANEL:Init()
self:SetScrollbars( true )
self:SetAllowLua( false )
self.JS = {}
self.Callbacks = {}
--
-- Implement a console.log - because awesomium doesn't provide it for us anymore.
--
self:AddFunction( "console", "log", function( param ) self:ConsoleMessage( param ) end )
end
function PANEL:Think()
if ( self.JS && !self:IsLoading() ) then
for k, v in pairs( self.JS ) do
self:RunJavascript( v )
end
self.JS = nil
end
end
function PANEL:Paint()
if ( self:IsLoading() ) then
return true
end
end
function PANEL:QueueJavascript( js )
--
-- Can skip using the queue if there's nothing else in it
--
if ( !self.JS && !self:IsLoading() ) then
return self:RunJavascript( js )
end
self.JS = self.JS or {}
table.insert( self.JS, js )
self:Think()
end
function PANEL:Call( js )
self:QueueJavascript( js )
end
function PANEL:ConsoleMessage( msg, file, line )
if ( !isstring( msg ) ) then msg = "*js variable*" end
--
-- Handle error messages
--
if ( isstring( file ) && isnumber( line ) ) then
if ( #file > 64 ) then
file = string.sub( file, 1, 64 ) .. "..."
end
MsgC( Color( 255, 160, 255 ), "[HTML] " )
MsgC( Color( 255, 255, 255 ), file, ":", line, ": ", msg, "\n" )
return
end
--
-- Handle Lua execution
--
if ( self.m_bAllowLua && msg:StartWith( "RUNLUA:" ) ) then
local strLua = msg:sub( 8 )
SELF = self
RunString( strLua )
SELF = nil
return
end
--
-- Plain ol' console.log
--
MsgC( Color( 255, 160, 255 ), "[HTML] " )
MsgC( Color( 255, 255, 255 ), msg, "\n" )
end
--
-- Called by the engine when a callback function is called
--
function PANEL:OnCallback( obj, func, args )
--
-- Use AddFunction to add functions to this.
--
local f = self.Callbacks[ obj .. "." .. func ]
if ( f ) then
return f( unpack( args ) )
end
end
--
-- Add a function to Javascript
--
function PANEL:AddFunction( obj, funcname, func )
--
-- Create the `object` if it doesn't exist
--
if ( !self.Callbacks[ obj ] ) then
self:NewObject( obj )
self.Callbacks[ obj ] = true
end
--
-- This creates the function in javascript (which redirects to c++ which calls OnCallback here)
--
self:NewObjectCallback( obj, funcname )
--
-- Store the function so OnCallback can find it and call it
--
self.Callbacks[ obj .. "." .. funcname ] = func
end
--
-- Called when this panel begins loading a page
--
function PANEL:OnBeginLoadingDocument( url )
end
--
-- Called when this panel successfully loads a page
--
function PANEL:OnFinishLoadingDocument( url )
end
--
-- Called when this panel's DOM has been set up. You can run JavaScript in here
--
function PANEL:OnDocumentReady( url )
end
--
-- Called when a this panel tries to open a child (such as a popup or new tab)
--
function PANEL:OnChildViewCreated( sourceURL, targetURL, isPopup )
end
--
-- Called when the title of the loaded document has changed
--
function PANEL:OnChangeTitle( title )
end
--
-- Called when the target URL of the frame has changed, this happens when you hover over a link
--
function PANEL:OnChangeTargetURL( url )
end
derma.DefineControl( "DHTML", "A shape", PANEL, "Awesomium" )