Skip to content

Commit

Permalink
Merge pull request #47 from sarmadka/main
Browse files Browse the repository at this point in the history
Added Input.disabled property and cancelRequest function.
  • Loading branch information
sarmadka committed May 2, 2024
2 parents 30936a1 + 8596aed commit c08b193
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 8 deletions.
69 changes: 67 additions & 2 deletions Doc/global_funcs.ar.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,52 @@ function setElementAttribute (name: ptr[Char], prop: ptr[Char], value: ptr[Char]
`قيمة` (`value`) قيمة الميزة التي نريد تحديدها.


### هات_ميزة_عنصر (getElementAttribute)

<div dir=rtl>

```
عرف هات_ميزة_عنصر(اسم: مؤشر[مـحرف]، خاصية: مؤشر[مـحرف]): مـؤشر_محارف؛
```

</div>

```
function getElementAttribute (name: ptr[Char], prop: ptr[Char]): CharsPtr;
```

ترجع قيمة ميزة لعنصر من عناصر DOM.

المعطيات:

`اسم` (`name`) اسم العنصر.

`خاصية` (`prop`) اسم الخاصية المراد جلب قيمتها.


### أزل_ميزة_عنصر (removeElementAttribute)

<div dir=rtl>

```
عرف أزل_ميزة_عنصر(اسم: مؤشر[مـحرف]، خاصية: مؤشر[مـحرف])؛
```

</div>

```
function removeElementAttribute (name: ptr[Char], prop: ptr[Char]);
```

تزيل ميزة لعنصر من عناصر DOM.

المعطيات:

`اسم` (`name`) اسم العنصر.

`خاصية` (`prop`) اسم الخاصية المراد إزالتها.


### حدد_متن_طراز (setStyleRule)

<div dir=rtl>
Expand Down Expand Up @@ -374,7 +420,7 @@ function cancelTimeout (id: ArchInt);
عرف أرسل_نداء(
طريقة: مؤشر[مـحرف]، عنوان: مؤشر[مـحرف]، ترويسات: مؤشر[مـحرف]، متن: مؤشر[مـحرف]،
الصلاحية_بالميلي_ثانية: صـحيح، منادى: مغلفة(جـيسون)
): صـحيح_متكيف؛
عرف أرسل_نداء(
طريقة: مؤشر[مـحرف]، عنوان: مؤشر[مـحرف]، ترويسات: مؤشر[مـحرف]، متن: مؤشر[مـحرف]،
الصلاحية_بالميلي_ثانية: صـحيح
Expand All @@ -387,7 +433,7 @@ function cancelTimeout (id: ArchInt);
function sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int,
cb: closure (Json)
);
): ArchInt;
function sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int
): SrdRef[Promise[Json]];
Expand All @@ -406,6 +452,25 @@ function sendRequest (
}
```

الصيغة الأولى ترجع رمزًا يمكن استخدامه لاحقًا لإلغاء الطلب باستخدام دالة ألغ_نداء.


### ألغ_نداء (cancelRequest)

<div dir=rtl>

```
دالة ألغ_نداء (رمز_النداء: صـحيح_متكيف)؛
```

</div>

```
function cancelRequest (requestId: ArchInt);
```

تلغي نداءا ارسل باستخدام `أرسل_نداء`. الرمز المطلوب في المعطى هو الرمز المرجع من دالة `أرسل_نداء`.


### حمل_خط (loadFont)

Expand Down
44 changes: 43 additions & 1 deletion Doc/global_funcs.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ Parameters:
`value` the value of the attribute we want to set


### getElementAttribute

```
function getElementAttribute (name: ptr[Char], prop: ptr[Char]): CharsPtr;
```

Gets an attribute for a given DOM element.

Parameters:

`name` component's name.

`prop` the name of the attribute we want to retrieve.


### removeElementAttribute

```
function removeElementAttribute (name: ptr[Char], prop: ptr[Char]);
```

Removes an attribute from a given DOM element.

Parameters:

`name` component's name.

`prop` the name of the attribute we want to remove.


### setStyleRule

```
Expand Down Expand Up @@ -239,7 +269,7 @@ Parameters:
function sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int,
cb: closure (Json)
);
): ArchInt;
function sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int
): SrdRef[Promise[Json]];
Expand All @@ -259,6 +289,18 @@ The response data is formatted as JSON and has the following structure:
}
```

The callback form of this function returns an ID that can later be used to cancel the request using
cancelRequest.


### cancelRequest

```
function cancelRequest (requestId: ArchInt);
```

Cancels an ongoing request. The provided ID is the ID obtained from sendRequest.


### loadFont

Expand Down
2 changes: 2 additions & 0 deletions Doc/widgets.ar.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ def onKeyUp: DomEventSignal[TextInput, String];

* `قيمة_وصفية` (`placeholder`): `نـص` (`String`). قيمة تظهر قبل أن يقوم المستخدم بالإدخال لتوضيح ما يجب إدخاله.

* `معطل` (`disabled`): ثـنائي (`bool`). إن كانت قيمتها 1 ستُعطل الودجة بحيث لا يستطيع المستخدم تغيير قيمتها.

#### الأحداث

##### عند_الغيير (onChanged)
Expand Down
2 changes: 2 additions & 0 deletions Doc/widgets.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ A user input field.

* `inputType`: `String`. The type of input values.

* `disabled`: `Bool`. Whether the input box is disabled.

* `placeholder`: `String`. A value that appears before the user enters anything, to clarify what
to enter.

Expand Down
4 changes: 3 additions & 1 deletion WebPlatform/EventHandling/event_loop.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
def json: Json(jsonStr);
def index: ArchInt = findEventHandlerIndex(json("cbId"));
if index != -1 {
if json("recurring") {
if json("eventName")~cast[String] == "_abort_" {
eventHandlers.remove(index);
} else if json("recurring") {
eventHandlers(index).cb(json);
} else {
def cb: closure (Json) = eventHandlers(index).cb;
Expand Down
12 changes: 11 additions & 1 deletion WebPlatform/Widgets/Input.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
generateProperty[inputType, InputType, String];
generateProperty[placeholder, Placeholder, String];
generateProperty[ariaHidden, AriaHidden, bool];
generateProperty[disabled, Disabled, bool];

def _text: String;
handler this.setText (t: String) {
Expand All @@ -25,7 +26,7 @@
this.setText(val);
return this._text;
}
handler this.text: ref[String] {
handler this.text: String {
return this.getText();
}

Expand All @@ -45,13 +46,15 @@
this.onKeyUp.owner~no_deref = this;
this.inputType = String("text");
this.ariaHidden = false;
this.disabled = false;
}
handler this~init(inputType : String) {
this.onChanged.owner~no_deref = this;
this.onKeyPress.owner~no_deref = this;
this.onKeyUp.owner~no_deref = this;
this.inputType=inputType;
this.ariaHidden = false;
this.disabled = false;
}

//-----------------
Expand Down Expand Up @@ -125,6 +128,13 @@
setElementAttribute(this.id, "placeholder", this.placeholder);
if this.ariaHidden == true {
setElementAttribute(this.id, String("aria-hidden"), String("true"));
} else {
removeElementAttribute(this.id, String("aria-hidden"));
}
if this.disabled == true {
setElementAttribute(this.id, String("disabled"), String("true"));
} else {
removeElementAttribute(this.id, String("disabled"));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions WebPlatform/async_operations.alusus
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@merge module WebPlatform {
function sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int, cb: closure (Json)
) {
): ArchInt {
def index: ArchInt = addEventHandler(cb);
_sendRequest(method, uri, headers, body, timeoutInMs, eventHandlers(index).id);
return _sendRequest(method, uri, headers, body, timeoutInMs, eventHandlers(index).id);
}

function sendRequest (
Expand Down
4 changes: 3 additions & 1 deletion WebPlatform/browser_api.alusus
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@expname[removeStyleRule] function removeStyleRule (name: ptr[Char], selector: ptr[Char]);
@expname[setElementAttribute] function setElementAttribute (name: ptr[Char], prop: ptr[Char], value: ptr[Char]);
@expname[getElementAttribute] function getElementAttribute (name: ptr[Char], prop: ptr[Char]): ptr[array[Char]];
@expname[removeElementAttribute] function removeElementAttribute (name: ptr[Char], prop: ptr[Char]);
@expname[getElementDimensions] function getElementDimensions (name: ptr[Char], result: ref[Dimensions]);
@expname[getElementBoundingRect] function getElementBoundingRect (name: ptr[Char], result: ref[Rectangle]);
// Element Interaction
Expand All @@ -29,7 +30,8 @@
// Async Operations
@expname[sendRequest] function _sendRequest (
method: ptr[Char], uri: ptr[Char], headers: ptr[Char], body: ptr[Char], timeoutInMs: Int, cbId: ArchInt
);
): ArchInt;
@expname[cancelRequest] function cancelRequest (requestId: ArchInt);
@expname[startTimer] function _startTimer (duration: Word, cbId: ArchInt): Word;
@expname[stopTimer] function _stopTimer (id: Word);
@expname[setTimeout] function _setTimeout (duration: Word, cbId: ArchInt): Word;
Expand Down
23 changes: 23 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const STACK_SIZE = 8192;
const wasmApi = {};
const eventsQueue = [];
const resources = {};
const requestControllers = {};
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
audioContext.resume();

let resourceCounter = 0;
let requestControllerCounter = 0;
let program;
let wasmMemory = null;
let asyncifyDataPtr; // Where the unwind/rewind data structure will live.
Expand Down Expand Up @@ -86,6 +88,10 @@ wasmApi.getElementAttribute = (elementName, propName) => {
return toWasmString(result);
}

wasmApi.removeElementAttribute = (elementName, propName) => {
document.getElementById(toJsString(elementName)).removeAttribute(toJsString(propName));
}

wasmApi.getElementDimensions = (elementName, pResult) => {
const element = document.getElementById(toJsString(elementName));
const resultArray = new Int32Array(wasmMemory.buffer, pResult, 2);
Expand Down Expand Up @@ -242,10 +248,14 @@ wasmApi.usleep = (ms) => {
// Async Operations APIs

wasmApi.sendRequest = (method, uri, headers, body, timeoutInMs, cbId) => {
const controllerId = ++requestControllerCounter;
const controller = new AbortController();
controller.cbId = cbId;
requestControllers[controllerId] = controller;
const signal = controller.signal;
const timeoutId = setTimeout(() => {
controller.abort();
delete requestControllers[controllerId];
onEvent(cbId, false, 'sendRequest', {
status: 0,
body: "Connection timeout"
Expand All @@ -264,6 +274,7 @@ wasmApi.sendRequest = (method, uri, headers, body, timeoutInMs, cbId) => {
body: toJsString(body),
signal
}).then(response => {
delete requestControllers[controllerId];
return response.text().then(bodyText => {
clearTimeout(timeoutId);
onEvent(cbId, false, 'sendRequest', {
Expand All @@ -273,12 +284,23 @@ wasmApi.sendRequest = (method, uri, headers, body, timeoutInMs, cbId) => {
});
});
}).catch(err => {
delete requestControllers[controllerId];
clearTimeout(timeoutId);
onEvent(cbId, false, 'sendRequest', {
status: 0,
body: err.message
});
});
return controllerId;
}

wasmApi.cancelRequest = (requestId) => {
const controller = requestControllers[requestId];
if (controller) {
controller.abort();
onEvent(controller.cbId, false, '_abort_', {});
delete requestControllers[requestId];
}
}

wasmApi.startTimer = (duration, cbId) => {
Expand Down Expand Up @@ -938,6 +960,7 @@ const eventPropMap = {
visibilitychange: [],
requestWakeLock: ['result'],
exitWakeLock: ['result'],
_abort_: [],
};

function pickNeededEventData(eventName, event) {
Expand Down
4 changes: 4 additions & 0 deletions مـنصة_ويب.أسس
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

عرف أنشئ_عنصر: لقب createElement؛
عرف حدد_ميزة_عنصر: لقب setElementAttribute؛
عرف هات_ميزة_عنصر: لقب getElementAttribute؛
عرف أزل_ميزة_عنصر: لقب removeElementAttribute؛
عرف حدد_متن_طراز: لقب setStyleRule؛

عرف نفذ_حلقة_معالجة_الأحداث: لقب runEventLoop؛
Expand All @@ -27,6 +29,7 @@
عرف ابدأ_المؤقت: لقب setTimeout؛
عرف ألغ_المؤقت: لقب cancelTimeout؛
عرف أرسل_نداء: لقب sendRequest؛
عرف ألغ_نداء: لقب cancelRequest؛
عرف حمل_خط: لقب loadFont؛
عرف حمل_بريمج_جافاسكريبت: لقب loadJsScript؛
عرف استدع_دالة_جافاسكريبت: لقب callCustomJsFn؛
Expand Down Expand Up @@ -555,6 +558,7 @@
عرب_خصلة[النص، _النص، text، Text]؛
عرب_خصلة[صنف_الإدخال، _صنف_الإدخال، inputType، InputType]؛
عرب_خصلة[قيمة_وصفية، _قيمة_وصفية، placeholder، Placeholder]؛
عرب_خصلة[معطل، _معطل، disabled، Disabled]؛
عرف عند_الغيير: لقب onChanged؛
عرف عند_كبس_مفتاح: لقب onKeyPress؛
عرف عند_انتهاء_الكبسة: لقب onKeyUp؛
Expand Down

0 comments on commit c08b193

Please sign in to comment.