diff --git a/2-ui/99-ui-misc/01-mutation-observer/article.md b/2-ui/99-ui-misc/01-mutation-observer/article.md index 3cf6f5397..e690c2506 100644 --- a/2-ui/99-ui-misc/01-mutation-observer/article.md +++ b/2-ui/99-ui-misc/01-mutation-observer/article.md @@ -1,52 +1,53 @@ # Mutation observer -`MutationObserver` is a built-in object that observes a DOM element and fires a callback when it detects a change. + همان طور که می دانیم `MutationObserver` یک inner object است که یک عنصر DOM را مشاهده می کند و هنگامی که تغییری را تشخیص می دهد یک تماس برگشتی ایجاد می کند. + We'll first take a look at the syntax, and then explore a real-world use case, to see where such thing may be useful. -## Syntax +## سینتکس -`MutationObserver` is easy to use. +استفاده از `MutationObserver` بسیار ساده است. -First, we create an observer with a callback-function: +اول از همه، یک observer با استفاده از callback-function می سازیم: ```js let observer = new MutationObserver(callback); ``` -And then attach it to a DOM node: +و بعد observer را به DOM node، attach می کند. ```js observer.observe(node, config); ``` - -`config` is an object with boolean options "what kind of changes to react on": -- `childList` -- changes in the direct children of `node`, -- `subtree` -- in all descendants of `node`, -- `attributes` -- attributes of `node`, -- `attributeFilter` -- an array of attribute names, to observe only selected ones. + (کانفیگ)`config` یک شی با boolean options "به چه نوع تغییراتی باید واکنش نشان داد" است: +- `childList` -- `node` از direct children تغییراتی در +- `subtree` --`node` در همه فرزندان +- `attributes` -- attributes های `node`, +- `attributeFilter` --که فقط سلکت شده ها را ببینیم ،attribute name یک ارایه از - `characterData` -- whether to observe `node.data` (text content), +- `characterData` -- ایا `node.data` (text content) را مشاهده کنیم یا نه -Few other options: -- `attributeOldValue` -- if `true`, pass both the old and the new value of attribute to callback (see below), otherwise only the new one (needs `attributes` option), -- `characterDataOldValue` -- if `true`, pass both the old and the new value of `node.data` to callback (see below), otherwise only the new one (needs `characterData` option). +چند گزینه دیگر: +- `attributeOldValue` --بود هم مقدار قذیم و هم مقدار جدید attribute به callback پاس داده می شود `true` اگر +- `characterDataOldValue` --(needs `characterData` option) پاس می دهیم، در غیر این صورت فقط مقدار جدید را callback را یه `node.data` بود هم مقدار جدید و هم مقدار قدیم `true` اگر -Then after any changes, the `callback` is executed: changes are passed in the first argument as a list of [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) objects, and the observer itself as the second argument. +سپس پس از هر تغییری، `callback` اجرا میشود: تغییرات در آرگومان اول بهعنوان فهرستی از اشیاء [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) و خود مشاهدهگر بهعنوان استدلال دوم است. -[MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) objects have properties: -- `type` -- mutation type, one of + شی (object) های [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) properties هایی دارند: +- `type` -- mutation type, یکی از موارد زیر - `"attributes"`: attribute modified - `"characterData"`: data modified, used for text nodes, - `"childList"`: child elements added/removed, -- `target` -- where the change occurred: an element for `"attributes"`, or text node for `"characterData"`, or an element for a `"childList"` mutation, -- `addedNodes/removedNodes` -- nodes that were added/removed, -- `previousSibling/nextSibling` -- the previous and next sibling to added/removed nodes, -- `attributeName/attributeNamespace` -- the name/namespace (for XML) of the changed attribute, -- `oldValue` -- the previous value, only for attribute or text changes, if the corresponding option is set `attributeOldValue`/`characterDataOldValue`. +- `target` -- `"childList"` mutation برای element و یا `"characterData"` برای text node یا `"attributes"` جایی که تغییر رخ داده است: یک عنصر برای +- `addedNodes/removedNodes` -- شده اند added/removed هایی که (nodes)نود +- `previousSibling/nextSibling` --added/removed nodes خواهر یا برادر قبلی یا جدید، +- `attributeName/attributeNamespace` -- changed attribute (XML) برای namespace اسم یا +- `oldValue` -- .تنظیم شود `attributeOldValue`/`characterDataOldValue` مقدار قبلی، فقط برای تغییر ویژگی یا متن می باشد، اگر گزینه مربوطه -For example, here's a `
A demo-element with id="highlight-demo", run the code above to observe it.
یک عنصر آزمایشی با id="highlight-demo"، کد بالا را اجرا کنید تا آن را مشاهده کنید. p>
-The following code populates its `innerHTML`, that causes the `MutationObserver` to react and highlight its contents:
+کد زیر `innerHTML` خود را پر می کند، که باعث می شود`MutationObserver` واکنش نشان داده و محتوای آن را برجسته کند:
```js run
let demoElem = document.getElementById('highlight-demo');
@@ -228,19 +229,19 @@ demoElem.innerHTML = `A code snippet is below:
`;
```
-Now we have `MutationObserver` that can track all highlighting in observed elements or the whole `document`. We can add/remove code snippets in HTML without thinking about it.
+اکنون `MutationObserver` را داریم که می تواند تمام برجسته سازی ها در عناصر مشاهده شده یا کل `document` را ردیابی کند. ما میتوانیم تکههای کد را بدون فکر کردن در HTML اضافه یا حذف کنیم.
-## Additional methods
+## متد های اضافه
-There's a method to stop observing the node:
+روشی برای توقف مشاهده گره وجود دارد:
-- `observer.disconnect()` -- stops the observation.
+- `observer.disconnect()` - مشاهده را متوقف می کند.
-When we stop the observing, it might be possible that some changes were not yet processed by the observer. In such cases, we use
+وقتی مشاهده را متوقف می کنیم، ممکن است برخی از تغییرات هنوز توسط ناظر پردازش نشده باشد. در چنین مواردی استفاده می کنیم
-- `observer.takeRecords()` -- gets a list of unprocessed mutation records - those that happened, but the callback has not handled them.
+-`observer.takeRecords()` - لیستی از سوابق جهش پردازش نشده را دریافت می کند - مواردی که اتفاق افتاده اند، اما پاسخ تماس آنها را مدیریت نکرده است.
-These methods can be used together, like this:
+این روش ها را می توان با هم استفاده کرد، مانند:
```js
// get a list of unprocessed mutations
@@ -254,20 +255,20 @@ observer.disconnect();
```
-```smart header="Records returned by `observer.takeRecords()` are removed from the processing queue"
-The callback won't be called for records, returned by `observer.takeRecords()`.
+``` پس smart header="سوابق برگردانده شده توسط `observer.takeRecords()` از صف پردازش حذف می شوند"
+تماس برگشتی برای رکوردها که توسط 'observer.takeRecords()' برگردانده شده است، فراخوانی نمی شود.
```
-```smart header="Garbage collection interaction"
-Observers use weak references to nodes internally. That is, if a node is removed from the DOM, and becomes unreachable, then it can be garbage collected.
+```smart header="تعامل جمع آوری زباله"
+ناظران از ارجاعات ضعیف به گره ها در داخل استفاده می کنند. به این معنا که اگر یک گره از DOM حذف شود و غیرقابل دسترسی شود، میتوان زبالهها را جمعآوری کرد.
-The mere fact that a DOM node is observed doesn't prevent the garbage collection.
+صرف این واقعیت که یک گره DOM مشاهده می شود مانع از جمع آوری زباله نمی شود.
```
-## Summary
+## خلاصه
-`MutationObserver` can react to changes in DOM - attributes, text content and adding/removing elements.
+ پس`MutationObserver` می تواند به تغییرات در DOM - ویژگی ها، محتوای متن و افزودن/حذف عناصر واکنش نشان دهد.
-We can use it to track changes introduced by other parts of our code, as well as to integrate with third-party scripts.
+ما میتوانیم از آن برای ردیابی تغییرات ایجاد شده توسط بخشهای دیگر کدمان و همچنین برای ادغام با اسکریپتهای شخص ثالث استفاده کنیم.
-`MutationObserver` can track any changes. The config "what to observe" options are used for optimizations, not to spend resources on unneeded callback invocations.
+هم چنین `MutationObserver` می تواند هر تغییری را ردیابی کند. تنظیمات "چه چیزی را مشاهده کنیم" برای بهینه سازی استفاده می شود، نه برای صرف منابع در فراخوانی های غیر ضروری.