-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_lib_html.js
125 lines (113 loc) · 3.94 KB
/
content_lib_html.js
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
class Html {
// Remove dom elements from document
// Html.remove(['img', 'a', 'div.ad']);
// Html.remove('div.ad');
static remove(filters) {
HtmlInnerService.distribute(filters, function(nodes) { nodes.remove(); } );
}
// hide dom elements in document
static hide(filters) {
HtmlInnerService.distribute(filters, function(nodes) { nodes.hide(); } );
}
// hide dom elements in document
static show(filters) {
HtmlInnerService.distribute(filters, function(nodes) { nodes.show(); } );
}
static click(filters) {
HtmlInnerService.distribute(filters, function(nodes) {
Html.dispatchMouseEvent(nodes[0], 'click', true, true);
});
}
// dispatch mouse event on specified dom
// e.g. Html.dispatchMouseEvent($('#id')[0], 'mouseover', true, true);
// e.g. Html.dispatchMouseEvent($('#id')[0], 'mousedown', true, true);
// e.g. Html.dispatchMouseEvent($('#id')[0], 'click', true, true);
// e.g. Html.dispatchMouseEvent($('#id')[0], 'mouseup', true, true);
static dispatchMouseEvent(target, var_args) {
var e = document.createEvent("MouseEvents");
// If you need clientX, clientY, etc., you can call
// initMouseEvent instead of initEvent
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
// e.g. var canceled = ! Html.dispatchKeyboardEvent(el, 'keydown', true, true, null, 'h', 0, '');
// // type, bubble, cacelable, window, key,
// // location (0=standard, 1=left, 2=right, 3=numpad, 4=mobile, 5=joystick)
// // space-sparated (Shift, Control, Alt, etc)
// e.g. Html.dispatchKeyboardEvent(el, 'keypress', true, true, null, 'h', 0, '');
static dispatchKeyboardEvent(target, initKeyboradEvent_args) {
var e = document.createEvent("KeyboardEvents");
e.initKeyboardEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
// e.g. Html.dispatchTextEvent(element, 'textInput', true, true, null, 'h', 0)
static dispatchTextEvent(target, initTextEvent_args) {
var e = document.createEvent("TextEvent");
e.initTextEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
// e.g. Html.dispatchSimpleEvent(element, 'input', false, false);
// e.g. Html.dispatchSimpleEvent(element, 'change', false, false);
static dispatchSimpleEvent(target, type, canBubble, cancelable) {
var e = document.createEvent("Event");
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
}
// html dom 内部服务
class HtmlInnerService {
static distribute(cssFilterArray, callback) {
if( cssFilterArray instanceof Array && cssFilterArray.length > 0 ) {
cssFilterArray.map(function(cssFilter){
HtmlInnerService.process($(cssFilter), callback);
});
}
else {
var cssFilter = cssFilterArray;
HtmlInnerService.process($(cssFilter), callback);
}
}
// callback(jqNodes)
static process(jqNodes, callabck) {
if( jqNodes ) {
ScCallback(callabck, 1, jqNodes);
}
}
// 处理单个css选择器操作
static removeElement(cssFilter, type) {
if( typeof cssFilter == 'string') {
try {
if( type == 'remove') {
return $(cssFilter).remove();
}
if( type == 'hide') {
return $(cssFilter).hide();
}
if( type == 'show') {
return $(cssFilter).show();
}
}
catch(err) {
console.log('element operate failed: ' + cssFilter + ' [' + type + ']');
}
}
}
}
class NetworkUtils {
// ajax post
static post(url, data, callback) {
$.ajax({
type: 'POST',
url: url,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(data||{}),
success: function(data) {
ScCallback(callback, this, true, data);
},
error: function() {
ScCallback(callback, this, false, null);
}
})
}
}