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 #include "frameworks/bridge/js_frontend/engine/quickjs/list_bridge.h"
17
18 #include "frameworks/bridge/common/dom/dom_list.h"
19
20 namespace OHOS::Ace::Framework {
21
JsGetCurrentOffset(JSContext * ctx,NodeId nodeId)22 JSValue ListBridge::JsGetCurrentOffset(JSContext* ctx, NodeId nodeId)
23 {
24 auto instance = static_cast<QjsEngineInstance*>(JS_GetContextOpaque(ctx));
25 auto page = instance->GetRunningPage();
26 if (!page) {
27 return JS_NULL;
28 }
29 double offsetX = 0.0;
30 double offsetY = 0.0;
31 auto task = [nodeId, page, &offsetX, &offsetY]() {
32 auto domDoc = page->GetDomDocument();
33 if (!domDoc) {
34 return;
35 }
36
37 auto domList = AceType::DynamicCast<DOMList>(domDoc->GetDOMNodeById(nodeId));
38 if (!domList) {
39 return;
40 }
41 auto offset = domList->GetCurrentOffset();
42 offsetX = offset.GetX();
43 offsetY = offset.GetY();
44 };
45 instance->GetDelegate()->PostSyncTaskToPage(task);
46 JSValue offsetContext = JS_NewObject(ctx);
47 JS_SetPropertyStr(ctx, offsetContext, "x", JS_NewFloat64(ctx, offsetX));
48 JS_SetPropertyStr(ctx, offsetContext, "y", JS_NewFloat64(ctx, offsetY));
49 return offsetContext;
50 }
51
52 } // namespace OHOS::Ace::Framework
53