cxfeng1 closed pull request #1458: Add event listener scroller, add enumerate 
component method.
URL: https://github.com/apache/incubator-weex/pull/1458
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.h 
b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.h
index 8f2b58720a..ec85b80f48 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.h
+++ b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.h
@@ -22,7 +22,9 @@
 
 @interface WXScrollerComponent : WXComponent <WXScrollerProtocol, 
UIScrollViewDelegate>
 
-@property (nonatomic, copy) void (^onScroll)(UIScrollView *);
+@property (nonatomic, copy) void (^onScroll)(UIScrollView *scrollView);
+
+@property (nonatomic, copy) void (^scrollEventListener)(WXScrollerComponent* 
sender, NSString* event, NSDictionary* params);
 
 @property (nonatomic, assign) NSUInteger loadmoreretry;
 
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm 
b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm
index 33f9439062..b59bc9a417 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm
+++ b/ios/sdk/WeexSDK/Sources/Component/WXScrollerComponent.mm
@@ -599,11 +599,19 @@ - (void)scrollViewWillBeginDragging:(UIScrollView 
*)scrollView
     
     _scrollStartPoint = scrollView.contentOffset;
     
-    if (_scrollStartEvent) {
+    if (_scrollStartEvent || _scrollEventListener) {
         CGFloat scaleFactor = self.weexInstance.pixelScaleFactor;
-        NSDictionary *contentSizeData = @{@"width":[NSNumber 
numberWithFloat:scrollView.contentSize.width / scaleFactor],@"height":[NSNumber 
numberWithFloat:scrollView.contentSize.height / scaleFactor]};
-        NSDictionary *contentOffsetData = @{@"x":[NSNumber 
numberWithFloat:-scrollView.contentOffset.x / scaleFactor],@"y":[NSNumber 
numberWithFloat:-scrollView.contentOffset.y / scaleFactor]};
-        [self fireEvent:@"scrollstart" 
params:@{@"contentSize":contentSizeData,@"contentOffset":contentOffsetData} 
domChanges:nil];
+        NSDictionary *contentSizeData = 
@{@"width":@(scrollView.contentSize.width / scaleFactor),
+                                          
@"height":@(scrollView.contentSize.height / scaleFactor)};
+        NSDictionary *contentOffsetData = @{@"x":@(-scrollView.contentOffset.x 
/ scaleFactor),
+                                            @"y":@(-scrollView.contentOffset.y 
/ scaleFactor)};
+        
+        if (_scrollStartEvent) {
+            [self fireEvent:@"scrollstart" 
params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} 
domChanges:nil];
+        }
+        if (_scrollEventListener) {
+            _scrollEventListener(self, @"scrollstart", 
@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData});
+        }
     }
     
     NSHashTable *delegates = [_delegates copy];
@@ -656,10 +664,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
     if (self.onScroll) {
         self.onScroll(scrollView);
     }
-    if (_scrollEvent) {
-        NSDictionary *contentSizeData = [[NSDictionary alloc] 
initWithObjectsAndKeys:[NSNumber numberWithFloat:scrollView.contentSize.width / 
scaleFactor],@"width",[NSNumber numberWithFloat:scrollView.contentSize.height / 
scaleFactor],@"height", nil];
-        //contentOffset values are replaced by 
(-contentOffset.x,-contentOffset.y) ,in order to be consistent with Android 
client.
-        NSDictionary *contentOffsetData = [[NSDictionary alloc] 
initWithObjectsAndKeys:[NSNumber numberWithFloat:-scrollView.contentOffset.x / 
scaleFactor],@"x",[NSNumber numberWithFloat:-scrollView.contentOffset.y / 
scaleFactor],@"y", nil];
+    if (_scrollEvent || _scrollEventListener) {
         CGFloat distance = 0;
         if (_scrollDirection == WXScrollDirectionHorizontal) {
             distance = scrollView.contentOffset.x - 
_lastScrollEventFiredOffset.x;
@@ -667,7 +672,18 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
             distance = scrollView.contentOffset.y - 
_lastScrollEventFiredOffset.y;
         }
         if (fabs(distance) >= _offsetAccuracy) {
-            [self fireEvent:@"scroll" 
params:@{@"contentSize":contentSizeData,@"contentOffset":contentOffsetData} 
domChanges:nil];
+            NSDictionary *contentSizeData = @{@"width": 
@(scrollView.contentSize.width / scaleFactor),
+                                              @"height": 
@(scrollView.contentSize.height / scaleFactor)};
+            //contentOffset values are replaced by 
(-contentOffset.x,-contentOffset.y), in order to be consistent with Android 
client.
+            NSDictionary *contentOffsetData = @{@"x": 
@(-scrollView.contentOffset.x / scaleFactor),
+                                                @"y": 
@(-scrollView.contentOffset.y / scaleFactor)};
+            
+            if (_scrollEvent) {
+                [self fireEvent:@"scroll" 
params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} 
domChanges:nil];
+            }
+            if (_scrollEventListener) {
+                _scrollEventListener(self, @"scroll", 
@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData});
+            }
             _lastScrollEventFiredOffset = scrollView.contentOffset;
         }
     }
@@ -708,12 +724,20 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView 
*)scrollView
 
 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 {
-    if (_scrollEndEvent) {
+    if (_scrollEndEvent || _scrollEventListener) {
         if (!_isScrolling) {
             CGFloat scaleFactor = self.weexInstance.pixelScaleFactor;
-            NSDictionary *contentSizeData = @{@"width":[NSNumber 
numberWithFloat:scrollView.contentSize.width / scaleFactor],@"height":[NSNumber 
numberWithFloat:scrollView.contentSize.height / scaleFactor]};
-            NSDictionary *contentOffsetData = @{@"x":[NSNumber 
numberWithFloat:-scrollView.contentOffset.x / scaleFactor],@"y":[NSNumber 
numberWithFloat:-scrollView.contentOffset.y / scaleFactor]};
-            [self fireEvent:@"scrollend" 
params:@{@"contentSize":contentSizeData,@"contentOffset":contentOffsetData} 
domChanges:nil];
+            NSDictionary *contentSizeData = 
@{@"width":@(scrollView.contentSize.width / scaleFactor),
+                                              
@"height":@(scrollView.contentSize.height / scaleFactor)};
+            NSDictionary *contentOffsetData = 
@{@"x":@(-scrollView.contentOffset.x / scaleFactor),
+                                                
@"y":@(-scrollView.contentOffset.y / scaleFactor)};
+            
+            if (_scrollEndEvent) {
+                [self fireEvent:@"scrollend" 
params:@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData} 
domChanges:nil];
+            }
+            if (_scrollEventListener) {
+                _scrollEventListener(self, @"scrollend", 
@{@"contentSize":contentSizeData, @"contentOffset":contentOffsetData});
+            }
         }
     }
     if (!_isScrolling) {
diff --git a/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h 
b/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h
index f6b02c97c5..e7bce57ff4 100644
--- a/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h
+++ b/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h
@@ -198,4 +198,11 @@ void WXPerformBlockSyncOnComponentThread(void 
(^block)(void));
  * @abstract handleStyle will be add to a queue to be executed every frame, 
but handleStyleOnMainThread will switch to main thread and execute imediately, 
you can call this for your execution time sequence.
  */
 - (void)handleStyleOnMainThread:(NSDictionary*)styles 
forComponent:(WXComponent *)component isUpdateStyles:(BOOL)isUpdateStyles;
+
+///--------------------------------------
+/// @name Enumerating
+///--------------------------------------
+
+- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL 
*stop))block;
+
 @end
diff --git a/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm 
b/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
index 43622dca68..a9146cba73 100644
--- a/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
+++ b/ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
@@ -986,6 +986,40 @@ - (void)removeFixFlexNode:(WeexCore::WXCoreLayoutNode* 
)fixNode{
     }
 }
 
+#pragma mark Enumerating
+
+- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL 
*stop))block
+{
+    if (block == nil || _rootComponent == nil) {
+        return;
+    }
+    
+    NSMutableArray* components = [[NSMutableArray alloc] init];
+    [components addObject:_rootComponent];
+    
+    while ([components count] > 0) {
+        BOOL stop = NO;
+        
+        NSArray* thisLevelComponents = [components copy];
+        [components removeAllObjects];
+        
+        // enumerate thisLevelComponents and add next level components to 
components
+        for (WXComponent* c in thisLevelComponents) {
+            block(c, &stop);
+            if (stop) {
+                break;
+            }
+            
+            for (WXComponent* nextLevelComponent in c->_subcomponents) {
+                [components addObject:nextLevelComponent];
+            }
+        }
+        
+        if (stop) {
+            break;
+        }
+    }
+}
 
 @end
 
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.h 
b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.h
index 1e333c796b..56e6a6b934 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.h
+++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.h
@@ -287,6 +287,12 @@ typedef NS_ENUM(NSInteger, WXErrorCode) {//error.code
  */
 - (NSUInteger)numberOfComponents;
 
+/**
+ * Enumerate components using breadth-first search algorithm,
+ must be called on component thread by calling WXPerformBlockOnComponentThread
+ */
+- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *component, BOOL 
*stop))block;
+
 /**
  * check whether the module eventName is registered
  */
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m 
b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
index 30b717b8d5..310f7b09e5 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
+++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
@@ -610,6 +610,13 @@ - (NSUInteger)numberOfComponents
     return [_componentManager numberOfComponents];
 }
 
+- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL 
*stop))block
+{
+    WXAssertComponentThread();
+    
+    [_componentManager enumerateComponentsUsingBlock:block];
+}
+
 - (void)fireGlobalEvent:(NSString *)eventName params:(NSDictionary *)params
 {
     if (!params){


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to