-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPWWidgetJS.m
More file actions
96 lines (75 loc) · 2.17 KB
/
PWWidgetJS.m
File metadata and controls
96 lines (75 loc) · 2.17 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
//
// ProWidgets
//
// 1.0.0
//
// Created by Alan Yip on 18 Jan 2014
// Copyright 2014 Alan Yip. All rights reserved.
//
#import "PWWidgetJS.h"
#import "JSBridge/PWJSBridge.h"
#import "JSBridge/PWJSBridgeWidgetWrapper.h"
#import "JSBridge/PWJSBridgeWidgetItemWrapper.h"
#define PW_IMP_HANDLER(ivar) - (void)ivar {\
JSValue *callback = [_bridge.widget ivar];\
if (callback != nil)\
[callback callWithArguments:nil];\
else\
[super ivar];\
}
@implementation PWWidgetJS
- (instancetype)initWithJSFile:(NSString *)filename withName:(NSString *)name inBundle:(NSBundle *)bundle {
if ((self = [super init])) {
// set widget information
self.name = name;
self.bundle = bundle;
// set JS file name
self.filename = filename;
// construct file path
NSString *path = [NSString stringWithFormat:@"%@/%@", [bundle bundlePath], filename];
self.path = path;
// initialize JSBridge
_bridge = [[PWJSBridge alloc] initWithWidget:self];
// read JS file
[_bridge readJSFile:path];
}
return self;
}
- (void)itemValueChangedEventHandler:(PWWidgetItem *)item oldValue:(id)oldValue {
JSValue *callback = [_bridge.widget itemValueChangedEventHandler];
if (callback != nil) {
PWJSBridgeWidgetItemWrapper *wrapper = [PWJSBridgeWidgetItemWrapper wrapperOfItem:item];
NSArray *arguments = nil;
if (wrapper != nil && oldValue == nil) arguments = @[wrapper];
if (wrapper != nil && oldValue != nil) arguments = @[wrapper, oldValue];
[callback callWithArguments:arguments];
}
}
- (void)submitEventHandler:(NSDictionary *)values {
JSValue *callback = [_bridge.widget submitEventHandler];
if (callback != nil) {
[callback callWithArguments:(values == nil ? nil : @[values])];
}
}
// Callback methods
PW_IMP_HANDLER(configure)
PW_IMP_HANDLER(load)
PW_IMP_HANDLER(willPresent)
PW_IMP_HANDLER(didPresent)
PW_IMP_HANDLER(willDismiss)
PW_IMP_HANDLER(didDismiss)
///// JS Bridge /////
- (void)dealloc {
// release JSBridge
[_bridge widgetDismissed];
RELEASE(_bridge)
// release file name and path
RELEASE(_filename)
RELEASE(_path)
[super dealloc];
}
@end
@implementation PWContentItemViewControllerJS
// Callback method
PW_IMP_HANDLER(configureFirstResponder)
@end