/
JasonComponentFactory.m
85 lines (74 loc) 路 2.64 KB
/
JasonComponentFactory.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
//
// JasonComponentFactory.m
// Jasonette
//
// Copyright 漏 2016 gliechtenstein. All rights reserved.
//
#import "JasonComponentFactory.h"
@implementation JasonComponentFactory
static NSMutableDictionary *_imageLoaded = nil;
static NSMutableDictionary *_stylesheet = nil;
+ (UIView *)build:(UIView *)component withJSON:(NSDictionary *)child withOptions:(NSMutableDictionary *)options{
NSString *capitalizedType = [child[@"type"] capitalizedString];
NSString *componentClassName = [NSString stringWithFormat:@"Jason%@Component", capitalizedType];
if(componentClassName){
Class<JasonComponentProtocol> ComponentClass = NSClassFromString(componentClassName);
child = [self applyStylesheet:child];
UIView *generated_component = [ComponentClass build:component withJSON:child withOptions:options];
if (child[@"focus"]) {
[[Jason client] getVC].focusField = generated_component;
}
[generated_component setNeedsLayout];
[generated_component layoutIfNeeded];
return generated_component;
} else {
return [[UIView alloc] init];
}
}
+ (NSMutableDictionary *)imageLoaded{
if (_imageLoaded == nil) {
_imageLoaded = [[NSMutableDictionary alloc] init];
}
return _imageLoaded;
}
+ (void)setImageLoaded:(NSMutableDictionary *)imageLoaded{
if (imageLoaded != _imageLoaded) {
_imageLoaded = [imageLoaded mutableCopy];
}
}
+ (NSMutableDictionary *)stylesheet{
if(_stylesheet == nil){
_stylesheet = [[NSMutableDictionary alloc] init];
}
return _stylesheet;
}
+ (void)setStylesheet:(NSMutableDictionary *)stylesheet{
if (stylesheet != _stylesheet){
_stylesheet = [stylesheet mutableCopy];
}
}
// Common
+ (NSMutableDictionary *)applyStylesheet:(NSDictionary *)item{
NSMutableDictionary *new_style = [[NSMutableDictionary alloc] init];
if(item[@"class"]){
NSString *class_string = item[@"class"];
NSMutableArray *classes = [[class_string componentsSeparatedByString:@" "] mutableCopy];
[classes removeObject:@""];
for(NSString *c in classes){
NSString *class_selector = c;
NSDictionary *class_style = self.stylesheet[class_selector];
for(NSString *key in [class_style allKeys]){
new_style[key] = class_style[key];
}
}
}
if(item[@"style"]){
for(NSString *key in item[@"style"]){
new_style[key] = item[@"style"][key];
}
}
NSMutableDictionary *stylized_item = [item mutableCopy];
stylized_item[@"style"] = new_style;
return stylized_item;
}
@end