-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCCWebScriptPageController.m
277 lines (233 loc) · 9.42 KB
/
MCCWebScriptPageController.m
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
//
// MCCWebScriptPageController.m
// MailCommon
//
// Created by smorr on 2013-09-24.
// Copyright (c) 2013 Indev Software. All rights reserved.
//
#import "MCCWebScriptPageController.h"
/*
* Each method of this controller works handler for a javascript function call by the webpage.
*
* The webpage should in the name of the class for handling the javascript
*
* The Window controller will instantiate the controller object when the page loads
* eg
* <script>
* // set the objective-c page controller class for this web page.
* windowController.setPageControllerClassName("WelcomePageController");
*
* </script>
*
* Calling each method is a simple javascript.function evocation to the pageController object
* eg
* <button type="button" onclick="pageController.buyOnline()">Buy Online</button>
*
* Naming Convention
*
* OBJC Functions with 0 parameters will have JS names identical to obj-c names
* eg -(void)buyOnline pageController.buyOnline();
*
* OBJC Functions with 1 parameters will drop the final : in the JS function name
* eg -(void)setAValue: pageController.setAValue(1);
*
* OBJC Functions with 2+ parameters will drop the final : in the JS function name
* and convert remaining : to _
* eg -(void)setValue:forKey: pageController.setValue_forKey(1,"test");
*
*
* Elements contents
*
* The pageController can access and set the text of html elements by ID with the following methods
* - (void)setContentsOfPageElement:(NSString*) pageObjectID toString:(NSString*)string;
* - (NSString*)contentsOfPageElement:(NSString*) pageObjectID;
*
*
*/
@implementation MCC_PREFIXED_NAME(WebScriptPageController)
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
return NO;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)property {
return YES;
}
+ (NSString *)webScriptNameForSelector:(SEL)sel {
NSString *selectorString = NSStringFromSelector(sel);
NSString *result= [selectorString stringByReplacingOccurrencesOfString:@":" withString:@""];
return result;
}
+ (NSString *)webScriptNameForKey:(const char *)name {
return nil;
}
#pragma mark - Getting/Setting Page Content
- (void)localizeElementID:(NSString*)elementID withString:(NSString*)unlocalizedString fromTable:(NSString*)table {
static NSBundle * bundle = nil;
if (!bundle) bundle =[NSBundle bundleForClass:[self class]];
NSString * localizedString = [bundle localizedStringForKey:unlocalizedString value:unlocalizedString table:table];
[self setContentOfElementId:elementID toString:localizedString];
}
- (void)localizeElementID:(NSString*)elementID usingStringsTable:(NSString*)table {
[self localizeElementID:elementID withString:elementID fromTable:table];
}
- (void)localizePrefixedElementsWithStringsFromTable:(NSString *)table {
static NSBundle * bundle = nil;
if (!bundle) bundle =[NSBundle bundleForClass:[self class]];
WebScriptObject *scriptObject = [self.webView windowScriptObject];
DOMNodeList * nodeList = nil;
if (scriptObject) {
nodeList = [scriptObject evaluateWebScript:@"document.getElementsByTagName('*')"];
NSUInteger idx = [nodeList length];
while (idx--) {
DOMElement * element = (DOMElement *)[nodeList item:(unsigned int)idx];
NSString * elementId = [element getAttribute:@"id"];
if ([elementId hasPrefix:@"@"]){
NSString * localizationKey = [elementId substringFromIndex:1];
NSString * localizedString = [bundle localizedStringForKey:localizationKey value:localizationKey table:table];
[self setContentOfElementId:elementId toString:localizedString];
}
}
}
}
- (NSString *)contentOfElementId:(NSString*)pageObjectID {
WebScriptObject *scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result) {
if ([result respondsToSelector:@selector(value)]) {
DOMHTMLInputElement *inputElement = (DOMHTMLInputElement *)result;
return [inputElement value];
}
else if ([result isKindOfClass:[DOMHTMLElement class]]){
return [result innerText];
}
}
}
return nil;
}
-(NSWindow*)window{
return self.webView.window;
}
-(id)windowController{
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
return [scriptObject valueForKey:@"windowController"];
}
return nil;
}
-(id)valueForUndefinedKey:(NSString *)key{
NSLog (@"Key: %@ not found -- returning nil",key);
return nil;
}
- (void)setContentOfElementId:(NSString*)pageObjectID toString:(NSString*)string {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result) {
if ([result isKindOfClass:[DOMHTMLElement class]]){
[result setInnerText:string];
}
}
}
}
- (NSString*)imagePathOnElementId:(NSString*)pageObjectID {
WebScriptObject *scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result) {
if ([result respondsToSelector:@selector(src)]) {
return [result performSelector:@selector(src)];
}
}
}
return nil;
}
- (void)setImagePath:(NSString*)path onElementId:(NSString*)pageObjectID {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result) {
if ([result respondsToSelector:@selector(src)]) {
[result performSelector:@selector(setSrc:) withObject:path];
}
}
}
}
- (NSString*)htmlOfElementId:(NSString*)pageObjectID {
WebScriptObject *scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]){
return [result innerHTML];
}
}
return nil;
}
- (void)setHtmlOfElementId:(NSString*)pageObjectID toString:(NSString*)string {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]){
[result setInnerHTML:string];
}
}
}
- (void)setHtmlOfElementId:(NSString*)pageObjectID toNode:(DOMHTMLElement*)element {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]){
[result setInnerHTML:[element outerHTML]];
}
}
}
- (void)replaceElementId:(NSString *)objectID withHTML:(NSString *)html {
WebScriptObject * scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')", objectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]) {
[result setOuterHTML:html];
}
}
}
#pragma mark - Attributes
- (void)setAttributeValue:(NSString *)attrValue forName:(NSString *)attrName onElementId:(NSString *)pageObjectID {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]) {
NSString *setDisabledScript = [NSString stringWithFormat:@"document.getElementById('%@').%@ = %@", pageObjectID, attrName, attrValue];
[scriptObject evaluateWebScript:setDisabledScript];
}
}
}
- (BOOL)attributeValueForName:(NSString *)attrName onElementId:(NSString *)pageObjectID {
WebScriptObject* scriptObject = [self.webView windowScriptObject];
if (scriptObject) {
id result = [scriptObject evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('%@')",pageObjectID]];
if (result && [result isKindOfClass:[DOMHTMLElement class]]) {
NSString *setDisabledScript = [NSString stringWithFormat:@"document.getElementById('%@').%@", pageObjectID, attrName];
id disabledValue = [scriptObject evaluateWebScript:setDisabledScript];
return [disabledValue boolValue];
}
}
return NO;
}
- (void)setDisabled:(BOOL)disabled onElementId:(NSString *)pageObjectID {
[self setAttributeValue:(disabled?@"true":@"false") forName:@"disabled" onElementId:pageObjectID];
}
- (BOOL)disabledOnElementId:(NSString *)pageObjectID {
return [self attributeValueForName:@"disabled" onElementId:pageObjectID];
}
- (void)setHidden:(BOOL)hidden onElementId:(NSString *)pageObjectID {
[self setAttributeValue:(hidden?@"true":@"false") forName:@"hidden" onElementId:pageObjectID];
}
- (BOOL)hiddenOnElementId:(NSString *)pageObjectID {
return [self attributeValueForName:@"hidden" onElementId:pageObjectID];
}
-(void)initPage {
}
- (void)dealloc {
self.webView = nil;
MCC_DEALLOC();
}
@end