1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FOREACH_FUNCTION_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FOREACH_FUNCTION_H
18 
19 #include <map>
20 
21 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
23 
24 namespace OHOS::Ace::Framework {
25 
26 class JsForEachFunction : public JsFunction {
27     DECLARE_ACE_TYPE(JsForEachFunction, JsFunction)
28 
29 private:
30     JSWeak<JSFunc> jsIdentityMapperFunc_;
31     JSWeak<JSFunc> jsViewMapperFunc_;
32 
33 public:
JsForEachFunction(JSRef<JSObject> jsArray,JSRef<JSFunc> jsIdentityMapperFunc,JSRef<JSFunc> jsViewMapperFunc)34     JsForEachFunction(JSRef<JSObject> jsArray, JSRef<JSFunc> jsIdentityMapperFunc, JSRef<JSFunc> jsViewMapperFunc)
35         : JsFunction(jsArray, JSRef<JSFunc>::Cast(jsArray->GetProperty("map")))
36     {
37         if (!jsIdentityMapperFunc.IsEmpty()) {
38             jsIdentityMapperFunc_ = jsIdentityMapperFunc;
39         }
40         jsViewMapperFunc_ = jsViewMapperFunc;
41     }
42 
JsForEachFunction(JSRef<JSObject> jsArray,JSRef<JSFunc> jsViewMapperFunc)43     JsForEachFunction(JSRef<JSObject> jsArray, JSRef<JSFunc> jsViewMapperFunc)
44         : JsFunction(jsArray, JSRef<JSFunc>::Cast(jsArray->GetProperty("map")))
45     {
46         jsViewMapperFunc_ = jsViewMapperFunc;
47     }
48 
~JsForEachFunction()49     ~JsForEachFunction() override {}
50 
51     // This executes the IdentifierFunction on all items  in a array
52     // returns the vector of keys/ids in the same order.
53     std::vector<std::string> ExecuteIdentityMapper();
54 
55     // This executes the BuilderFunction on a specific index
56     // returns the native JsView for the item in index.
57     void ExecuteBuilderForIndex(int32_t index);
58 }; // JsForEachFunction
59 
60 } // namespace OHOS::Ace::Framework
61 
62 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FOREACH_FUNCTION_H
63