forked from ricardomansano/minipost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminipost.prw
346 lines (300 loc) · 13.1 KB
/
minipost.prw
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "totvs.ch"
/*/ -----------------------------------------------------------------/
Exemplo para o segundo video sobre o CloudBridge
/-------------------------------------------------------------------*/
function u_miniPost()
local tempPath := GetTempPath()
local i, oDlg, cFile, nHandle, globalLink
local cOS := getOS()
local aFiles := {"bootstrap.min.css",;
"jquery-1.10.2.min.js",;
"totvstec.js",;
"minipost.html",;
"minipost.css",;
"img_avatar1.png",;
"img_avatar2.png",;
"img_avatar3.png",;
"img_avatar4.png",;
"img_avatar5.png",;
"img_avatar6.png"}
private oMiniPost, oMobile
private isMobile := .F.
oDlg := TWindow():New(10, 10, 800, 600, "TOTVS - Exemplo video CloudBridge #2")
// --------------------------------------------------------------------------------
// Nos dispositivos moveis os arquivos HTML5 sao copiados para pasta ASSETS
// durante a compilacao do projeto e nao precisam ser baixados do RPO
// --------------------------------------------------------------------------------
if cOS == "ANDROID" .or. cOS == "IPHONEOS"
// --------------------------------------------------------------------------------
// O componente TMobile eh o reponsavel por acessar o dispositivo
// Mais informacoes em: http://tdn.totvs.com/display/tec/TMobile
// --------------------------------------------------------------------------------
oMobile := TMobile():New()
oMobile:SetScreenOrientation(-1)
isMobile := .T.
if cOS == "ANDROID"
tempPath := "file:///android_asset/web/"
else
tempPath := GetPvProfString("ENVIRONMENT", "ASSETSPATH", "Erro", GetSrvIniName()) + "/web/"
endif
else
// --------------------------------------------------------------------------------
// Baixa arquivos do RPO no TEMP
// --------------------------------------------------------------------------------
for i := 1 to len(aFiles)
cFile := + aFiles[i]
cFileContent := getApoRes(cFile)
if cFileContent == Nil
msgAlert("Arquivo " + cFile + " nao existe no RPO, favor compilar o arquivo")
return
else
nHandle := fCreate(tempPath+cFile)
fWrite(nHandle, cFileContent)
fClose(nHandle)
endif
next i
endif
// --------------------------------------------------------------------------------
// TWebChannel eh responsavel pelo trafego entre o SmartClient e o JavaScript
// --------------------------------------------------------------------------------
oWebChannel := TWebChannel():New()
oWebChannel:connect()
if !oWebChannel:lConnected
msgStop("Erro na conexao com o WebSocket")
return
endif
// --------------------------------------------------------------------------------
// IMPORTANTE: Aqui definimos a porta WebSocket que sera utilizada para comunicacao
// --------------------------------------------------------------------------------
globalLink := tempPath + "minipost.html?port=" + cValToChar(oWebChannel:nPort)
// --------------------------------------------------------------------------------
// O comando JavaScript dialog.jsToAdvpl()sera tratado por este bloco de codigo
// --------------------------------------------------------------------------------
oWebChannel:bJsToAdvpl := {|self,codeType,codeContent| jsToAdvpl(self,codeType,codeContent) }
// --------------------------------------------------------------------------------
// Componente do navegador embutido
// --------------------------------------------------------------------------------
oMiniPost := miniPost():New(oDlg,0,0,0,0)
oMiniPost:oWebEngine:navigate(globalLink)
oMiniPost:oWebEngine:Align := CONTROL_ALIGN_ALLCLIENT
oDlg:Activate("MAXIMIZED")
Return
/*/ -----------------------------------------------------------------/
Captura imagem
/-------------------------------------------------------------------*/
static function takePicture()
if !isMobile
jsAlert("TakePicture", "TakePicture sera executado apenas nos dispositivos")
return
endif
// --------------------------------------------------------------------------------
// Exibe imagem capturada
// --------------------------------------------------------------------------------
cFile := oMobile:TakePicture(0)
conout("", "TakePicrure", cFile)
if !empty(cFile)
beginContent var cImage
<div id="takePicture">
<button class="close" onclick="removeImage(this);">
<span aria-hidden="true"><font size=12>×</font></span></button>
<img src="%Exp:cFile%" width="90%">
</div>
endContent
oWebChannel:advplToJs("showImage", cImage)
endif
return
/*/ -----------------------------------------------------------------/
Retorna Geolocalizacao
/-------------------------------------------------------------------*/
static function getGeoCoordinate()
if !isMobile
jsAlert("GetGeoCoordinate", "GetGeoCoordinate sera executado apenas nos dispositivos")
return
endif
// --------------------------------------------------------------------------------
// Exibe geolocalizacao
// --------------------------------------------------------------------------------
cLocation := oMobile:GetGeoCoordinate(0)
jsAlert("Geolocalizacao", cLocation)
conout("", "Geolocalizacao", cLocation)
return
/*/ -----------------------------------------------------------------/
Retorna o Sistema Operacional em uso
/-------------------------------------------------------------------*/
static function getOS()
local stringOS := Upper(GetRmtInfo()[2])
if ("ANDROID" $ stringOS)
return "ANDROID"
elseif ("IPHONEOS" $ stringOS)
return "IPHONEOS"
elseif GetRemoteType() == 0 .or. GetRemoteType() == 1
return "WINDOWS"
elseif GetRemoteType() == 2
return "UNIX" // Linux ou MacOS
elseif GetRemoteType() == 5
return "HTML" // Smartclient HTML
endif
return ""
/*/ -----------------------------------------------------------------/
Bloco de codigo que recebera as chamadas JavaScript
/-------------------------------------------------------------------*/
static function jsToAdvpl(self,codeType,codeContent)
// Exibe mensagens trocadas
conout("",;
"jsToAdvpl->codeType: " + codeType,;
"jsToAdvpl->codeContent: " + codeContent)
do case
// ------------------------------------------
// Termino da carga da pagina HTML
// ------------------------------------------
case codeType == "pageStarted"
loadPage()
// ------------------------------------------
// Decrementa contador quando o post foi deletado via JavaScript
// ------------------------------------------
case codeType == "deletePost"
oMiniPost:nCount--
// ------------------------------------------
// Insere posts
// ------------------------------------------
case codeType == "inserePosts"
oMiniPost:insere()
// ------------------------------------------
// Deleta primeiro post da lista
// ------------------------------------------
case codeType == "deleteFirst"
oMiniPost:deletaFirst()
// ------------------------------------------
// Exibe quantidade de posts visiveis
// ------------------------------------------
case codeType =="countPosts"
jsAlert("Count()", cValTochar(oMiniPost:count())+" posts incluidos")
// ------------------------------------------
// Captura imagem
// ------------------------------------------
case codeType == "takePicture"
takePicture()
// ------------------------------------------
// Retorna geolocalizacao
// ------------------------------------------
case codeType == "getGeoCoordinate"
getGeoCoordinate()
// ------------------------------------------
// Fecha aplicacao
// ------------------------------------------
case codeType == "closeApp"
__Quit()
endCase
return
/*/ -----------------------------------------------------------------/
Carrega itens do menu lateral e o JavaScript para exclusão dos posts
/-------------------------------------------------------------------*/
static function loadPage()
// --------------------------------------------------------------------------------
// Insere itens no menu lateral
// --------------------------------------------------------------------------------
BeginContent var cMenu
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">☚ </a>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('inserePosts', 'Insere posts');">Insere posts</a>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('deleteFirst', 'Deleta primeiro post');">Deleta 1o post</a>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('countPosts', 'Exibe a quantidade de posts');">Posts visiveis</a>
<hr>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('takePicture', 'Captura imagem');">Captura foto</a>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('getGeoCoordinate', 'Retorna geolocalizacao');">Geolocalização</a>
<hr>
<a href="#" onclick="closeNav(); dialog.jsToAdvpl('closeApp', 'Aplicacao fechada manualmente');">Sair</a>
endContent
oWebChannel:advplToJs("lateralMenu", cMenu)
// --------------------------------------------------------------------------------
// Insere trecho JavaScript para delecao dos posts
// --------------------------------------------------------------------------------
// Serao 2 tipos de deleção: 1o Apaga o primeiro post da lista, chamado pelo menu lateral
// 2o Apaga o post atraves do botao fechar (X) do proprio post
// --------------------------------------------------------------------------------
// *IMPORTANTE: Nunca utilize comentarios ao inserir o codigo JavaScript via AdvPL
// --------------------------------------------------------------------------------
BeginContent var cFunction
function removeFirstPanel() {
var lenPosts = document.getElementById("mainPanel").childNodes.length;
for (i=0; i<lenPosts; i++) {
if (document.getElementById("mainPanel").childNodes[i].id == "panelID") {
document.getElementById("mainPanel").childNodes[i].remove();
break;
}
}
}
function removePanel(obj, avatarNum) {
obj.parentElement.parentElement.remove();
dialog.jsToAdvpl("deletePost", avatarNum);
}
endContent
oWebChannel:advplToJs("js", cFunction)
return
/*/ -----------------------------------------------------------------/
Alert que sera executado via JavaScript
/-------------------------------------------------------------------*/
static function jsAlert(cTitle, cText)
oMiniPost:oWebEngine:runJavaScript('jsAlert("' +cTitle+ '", "' +cText+ '");')
return
/*/ -----------------------------------------------------------------/
Classe AdvPL para manipulação do componente TWebEngine
/-------------------------------------------------------------------*/
class miniPost
Data nCount
Data nLastPost
Data oWebEngine
Method new(oWnd,nRow,nCol,nWidth,nHeight,cUrl) CONSTRUCTOR
Method count()
Method insere()
Method deletaFirst()
endClass
/*/ -----------------------------------------------------------------/
Construtor
/-------------------------------------------------------------------*/
Method New(oWnd,nRow,nCol,nWidth,nHeight,cUrl) class miniPost
::oWebEngine := TWebEngine():New(oWnd,nRow,nCol,nWidth,nHeight,cUrl)
::nCount := 0
::nLastPost := 0
return
/*/ -----------------------------------------------------------------/
Retorna quantidade de posts
/-------------------------------------------------------------------*/
Method count() class miniPost
return ::nCount
/*/ -----------------------------------------------------------------/
Insere 6 posts
/-------------------------------------------------------------------*/
Method insere() class miniPost
local cAvatar, cField, i
for i := 1 to 6
::nCount++
::nLastPost++
cAvatar := cValToChar(::nLastPost)
cAvatarImage := cValToChar(i)
// Insere posts
BeginContent var cField
<div id="panelID" class="panel panel-default" style="border-spacing:5px;">
<div class="media-left">
<img onclick="jsAlert('Avatar', 'Selecionado <b>Avatar%Exp:cAvatar%</b>, mensagem enviada ao Servidor'); dialog.jsToAdvpl('Selecionado avatar', 'Avatar%Exp:cAvatar%')"
src="img_avatar%Exp:cAvatarImage%.png" class="media-object" style="width:60px">
</div>
<div class="media-body">
<button class="close" onclick="removePanel(this, 'Avatar%Exp:cAvatar%');">
<span aria-hidden="true">×</span></button>
<h4 class="media-heading">Avatar%Exp:cAvatar%</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
endContent
oWebChannel:advplToJs("insertPost", cField)
next i
return
/*/ -----------------------------------------------------------------/
Deleta primeiro post
/-------------------------------------------------------------------*/
Method deletaFirst() class miniPost
if ::nCount > 0
::nCount--
::oWebEngine:runJavaScript("removeFirstPanel()")
endif
return