forked from hooray/hoorayos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhros.widget.js
More file actions
308 lines (308 loc) · 9.18 KB
/
hros.widget.js
File metadata and controls
308 lines (308 loc) · 9.18 KB
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
/*
** 小挂件
*/
HROS.widget = (function(){
return {
init: function(){
//挂件上各个按钮
HROS.widget.handle();
//挂件移动
HROS.widget.move();
//还原上次退出系统时widget的状态
HROS.widget.reduction();
$('#desk').on('mouseover', '.widget', function(){
$(this).children('.move').show();
}).on('mouseout', '.widget', function(){
$(this).children('.move').hide();
});
},
/*
** 创建挂件
** 自定义挂件:HROS.widget.createTemp({url,width,height,top,right});
** 示例:HROS.widget.createTemp({url:"http://www.baidu.com",width:800,height:400,top:100,right:100});
*/
createTemp: function(obj){
var appid = obj.appid == null ? Date.parse(new Date()): obj.appid;
//判断窗口是否已打开
var iswidgetopen = false;
$('#desk .widget').each(function(){
if($(this).attr('appid') == appid){
iswidgetopen = true;
return false;
}
});
//如果没有打开,则进行创建
if(!iswidgetopen){
function nextDo(options){
var widgetId = '#w_' + options.appid;
var widgetData = {
'width': options.width,
'height': options.height,
'type': 'widgetTemp',
'id': 'w_' + options.appid,
'appid': options.appid,
'realappid': options.appid,
'top': options.top,
'right': options.right,
'url': options.url,
'zIndex': HROS.CONFIG.widgetIndexid
};
$('#desk').append(HROS.template.widget(widgetData));
$(widgetId).data('info', widgetData).css({
opacity: 0,
scale: 1.1
}).transition({
opacity: 1,
scale: 1
}, 200);
HROS.CONFIG.widgetIndexid += 1;
}
nextDo({
appid: appid,
url: obj.url,
width: obj.width,
height: obj.height,
top: obj.top == null ? 0: obj.top,
right: obj.right == null ? 0: obj.right
});
}
},
create: function(appid, type, realappid){
var type = type == null ? 'widget': type;
//判断窗口是否已打开
var iswidgetopen = false;
$('#desk .widget').each(function(){
if($(this).attr('appid') == appid){
iswidgetopen = true;
return false;
}
});
//如果没有打开,则进行创建
if(!iswidgetopen && $('#d_' + appid).attr('opening') != 1){
$('#d_' + appid).attr('opening', 1);
function nextDo(options){
var widgetId = '#w_' + options.appid;
if(HROS.widget.checkCookie(appid, type)){
var widgetState = JSON.parse(Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID));
$(widgetState).each(function(){
if(this.appid == options.appid && this.type == options.type){
options.top = this.top;
options.right = this.right;
}
});
}else{
HROS.widget.addCookie(options.appid, options.type, 0, 0);
}
var widgetData = {
'title': options.title,
'width': options.width,
'height': options.height,
'type': options.type,
'id': 'w_' + options.appid,
'appid': options.appid,
'realappid': options.realappid == 0 ? options.appid: options.realappid,
'top': typeof(options.top) == 'undefined' ? 0: options.top,
'right': typeof(options.right) == 'undefined' ? 0: options.right,
'url': options.url,
'zIndex': HROS.CONFIG.widgetIndexid
};
$('#desk').append(HROS.template.widget(widgetData));
$(widgetId).data('info', widgetData).css({
opacity: 0,
scale: 1.1
}).transition({
opacity: 1,
scale: 1
}, 200);
HROS.CONFIG.widgetIndexid += 1;
}
$.ajax({
data: {
ac: 'getMyAppById',
id: appid,
type: type
}
}).done(function(widget){
if(widget != null){
if(widget['error'] == 'ERROR_NOT_FOUND'){
swal({
type: 'error',
title: '小挂件不存在,建议删除',
timer: 2000,
showConfirmButton: false
});
HROS.widget.removeCookie(appid, type);
}else if(widget['error'] == 'ERROR_NOT_INSTALLED'){
HROS.window.createTemp({
appid: 'hoorayos-yysc',
title: '应用市场',
url: 'sysapp/appmarket/index.php?id=' + (realappid == null ? $('#d_' + appid).attr('realappid'): realappid),
width: 800,
height: 484,
isflash: false,
refresh: true
});
HROS.widget.removeCookie(appid, type);
}else{
nextDo({
appid: widget['appid'],
realappid: widget['realappid'],
title: widget['name'],
url: widget['url'],
type: widget['type'],
width: widget['width'],
height: widget['height'],
top: 0,
right: 0
});
}
}else{
swal({
type: 'error',
title: '小挂件加载失败',
timer: 2000,
showConfirmButton: false
});
}
$('#d_' + appid).attr('opening', 0);
});
}
},
//还原上次退出系统时widget的状态
reduction: function(){
var widgetState = Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID);
if(typeof widgetState !== 'undefined'){
widgetState = JSON.parse(widgetState);
$(widgetState).each(function(){
HROS.widget.create(this.appid, this.type);
});
}
},
//根据id验证是否存在cookie中
checkCookie: function(appid, type){
var flag = false;
var widgetState = Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID);
if(typeof widgetState !== 'undefined'){
widgetState = JSON.parse(widgetState);
$(widgetState).each(function(){
if(this.appid == appid && this.type == type){
flag = true;
}
});
}
return flag;
},
/*
** 以下2个方法:addCookie、removeCookie
** 用于记录widget打开状态以及摆放位置
** 实现用户再次登入系统时,还原上次widget的状态
*/
addCookie: function(appid, type, top, right){
if(type == 'widget' || type == 'pwidget'){
//检查是否存在,如果存在则更新,反之则添加
if(HROS.widget.checkCookie(appid, type)){
var widgetState = JSON.parse(Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID));
$(widgetState).each(function(){
if(this.appid == appid && this.type == type){
this.top = top;
this.right = right;
}
});
}else{
var widgetState = Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID);
if(typeof widgetState !== 'undefined'){
widgetState = JSON.parse(widgetState);
}else{
widgetState = [];
}
widgetState.push({
appid: appid,
type: type,
top: top,
right: right
});
}
Cookies.set(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID, $.toJSON(widgetState), {expires: 95});
}
},
removeCookie: function(appid, type){
if(type == 'widget' || type == 'pwidget'){
if(HROS.widget.checkCookie(appid, type)){
var widgetState = JSON.parse(Cookies.get(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID));
$(widgetState).each(function(i){
if(this.appid == appid && this.type == type){
widgetState.splice(i, 1);
return false;
}
});
Cookies.set(cookie_prefix + 'widgetState' + HROS.CONFIG.memberID, $.toJSON(widgetState), {expires: 95});
}
}
},
move: function(){
$('#desk').on('mousedown touchstart', '.widget .move', function(e){
e.preventDefault();
var obj = $(this).parents('.widget');
HROS.widget.show2top(obj.attr('appid'));
var cx = e.type == 'mousedown' ? e.clientX : e.originalEvent.targetTouches[0].clientX;
var cy = e.type == 'mousedown' ? e.clientY : e.originalEvent.targetTouches[0].clientY;
var x = cx - obj.offset().left;
var y = cy - obj.offset().top;
var lay;
var t;
var r;
//绑定鼠标移动事件
$(document).on('mousemove touchmove', function(e){
var dcx = e.type == 'mousemove' ? e.clientX : e.originalEvent.targetTouches[0].clientX;
var dcy = e.type == 'mousemove' ? e.clientY : e.originalEvent.targetTouches[0].clientY;
lay = HROS.maskBox.desk();
lay.show();
t = dcy - y < 0 ? 0: dcy - y;
r = $(window).width() - obj.width() - (dcx - x);
obj.css({
top: t,
right: r
});
}).on('mouseup touchend', function(){
$(this).off('mousemove touchmove mouseup touchend');
if(typeof(lay) !== 'undefined'){
lay.hide();
}
if(obj.attr('type') != 'widgetTemp'){
HROS.widget.addCookie(obj.attr('appid'), obj.attr('type'), t, r);
}
});
});
},
close: function(appid){
var widgetId = '#w_' + appid;
HROS.widget.removeCookie($(widgetId).attr('appid'), $(widgetId).attr('type'));
$(widgetId).css({
opacity: 1,
scale: 1
}).transition({
opacity: 0,
scale: 1.1
}, 200, function(){
$(this).removeData('info').html('').remove();
});
},
show2top: function(appid){
var widgetId = '#w_' + appid;
$(widgetId).css('z-index', HROS.CONFIG.widgetIndexid);
HROS.CONFIG.widgetIndexid += 1;
},
handle: function(){
$('#desk').on('mousedown touchstart', '.widget a', function(e){
if(e.type == 'mousedown'){
e.preventDefault();
}
e.stopPropagation();
});
$('#desk').on('click', '.widget .ha-close', function(e){
var obj = $(this).parents('.widget');
HROS.widget.close(obj.attr('appid'));
});
}
}
})();