From 04595baf1ee1d06e24e9f77223aaf7e7bb3d96f1 Mon Sep 17 00:00:00 2001 From: Nathan Faubion Date: Sun, 10 Dec 2017 10:48:20 -0800 Subject: [PATCH 1/2] MutationObserver API --- src/DOM/Node/MutationObserver.js | 31 +++++++++++++ src/DOM/Node/MutationObserver.purs | 38 ++++++++++++++++ src/DOM/Node/MutationRecord.js | 55 +++++++++++++++++++++++ src/DOM/Node/MutationRecord.purs | 70 ++++++++++++++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 src/DOM/Node/MutationObserver.js create mode 100644 src/DOM/Node/MutationObserver.purs create mode 100644 src/DOM/Node/MutationRecord.js create mode 100644 src/DOM/Node/MutationRecord.purs diff --git a/src/DOM/Node/MutationObserver.js b/src/DOM/Node/MutationObserver.js new file mode 100644 index 0000000..1e44368 --- /dev/null +++ b/src/DOM/Node/MutationObserver.js @@ -0,0 +1,31 @@ +"use strict"; + +exports.mutationObserver = function (cb) { + return function () { + return new MutationObserver(function (mr, mo) { + return cb(mr)(mo)(); + }); + }; +}; + +exports.observe = function (node) { + return function (config) { + return function (mo) { + return function () { + return mo.observe(node, config); + }; + }; + }; +}; + +exports.disconnect = function (mo) { + return function () { + return mo.disconnect(); + }; +}; + +exports.takeRecords = function (mo) { + return function () { + return mo.takeRecords(); + }; +}; diff --git a/src/DOM/Node/MutationObserver.purs b/src/DOM/Node/MutationObserver.purs new file mode 100644 index 0000000..02720b9 --- /dev/null +++ b/src/DOM/Node/MutationObserver.purs @@ -0,0 +1,38 @@ +module DOM.Node.MutationObserver + ( MutationObserver + , MutationObserverInit + , mutationObserver + , observe + , disconnect + , takeRecords + ) where + +import Prelude + +import Control.Monad.Eff (Eff, kind Effect) +import DOM (DOM) +import DOM.Node.MutationRecord (MutationRecord) +import DOM.Node.Types (Node) + +foreign import data MutationObserver :: Type + +type MutationObserverInit = + { childList :: Boolean + , attributes :: Boolean + , characterData :: Boolean + , subtree :: Boolean + , attributeOldValue :: Boolean + , characterDataOldValue :: Boolean + , attributeFilter :: Array String + } + +foreign import mutationObserver + :: forall eff + . (MutationRecord -> MutationObserver -> Eff (dom :: DOM | eff) Unit) + -> Eff (dom :: DOM | eff) MutationObserver + +foreign import observe :: forall eff. Node -> MutationObserverInit -> MutationObserver -> Eff (dom :: DOM | eff) Unit + +foreign import disconnect :: forall eff. MutationObserver -> Eff (dom :: DOM | eff) Unit + +foreign import takeRecords :: forall eff. MutationObserver -> Eff (dom :: DOM | eff) (Array MutationRecord) diff --git a/src/DOM/Node/MutationRecord.js b/src/DOM/Node/MutationRecord.js new file mode 100644 index 0000000..8840546 --- /dev/null +++ b/src/DOM/Node/MutationRecord.js @@ -0,0 +1,55 @@ +"use strict"; + +exports.typeString = function (mr) { + return function () { + return mr.type; + }; +}; + +exports.target = function (mr) { + return function () { + return mr.target; + }; +}; + +exports.addedNodes = function (mr) { + return function () { + return mr.addedNodes; + }; +}; + +exports.removedNodes = function (mr) { + return function () { + return mr.removedNodes; + }; +}; + +exports._nextSibling = function (mr) { + return function () { + return mr.nextSibling; + }; +}; + +exports._previousSibling = function (mr) { + return function () { + return mr.previousSibling; + }; +}; + +exports._attributeName = function (mr) { + return function () { + return mr.attributeName; + }; +}; + +exports._attributeNamespace = function (mr) { + return function () { + return mr.attributeNamespace; + }; +}; + +exports._oldValue = function (mr) { + return function () { + return mr.oldValue; + }; +}; diff --git a/src/DOM/Node/MutationRecord.purs b/src/DOM/Node/MutationRecord.purs new file mode 100644 index 0000000..4fdc838 --- /dev/null +++ b/src/DOM/Node/MutationRecord.purs @@ -0,0 +1,70 @@ +module DOM.Node.MutationRecord + ( MutationRecord + , MutationRecordType(..) + , typeString + , type_ + , target + , addedNodes + , removedNodes + , nextSibling + , previousSibling + , attributeName + , attributeNamespace + , oldValue + ) where + +import Prelude + +import Control.Monad.Eff (Eff) +import DOM (DOM) +import DOM.Node.Types (Node, NodeList) +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toMaybe) + +foreign import data MutationRecord :: Type + +data MutationRecordType + = MutationRecordAttributes + | MutationRecordCharacterData + | MutationRecordChildList + +foreign import typeString :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) String + +type_ :: forall eff. Partial => MutationRecord -> Eff (dom :: DOM | eff) MutationRecordType +type_ = map stringToType <<< typeString + where + stringToType = case _ of + "attributes" -> MutationRecordAttributes + "characterData" -> MutationRecordCharacterData + "childList" -> MutationRecordChildList + +foreign import target :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) Node + +foreign import addedNodes :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) NodeList + +foreign import removedNodes :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) NodeList + +foreign import _nextSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable Node) + +nextSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe Node) +nextSibling = map toMaybe <<< _nextSibling + +foreign import _previousSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable Node) + +previousSibling :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe Node) +previousSibling = map toMaybe <<< _previousSibling + +foreign import _attributeName :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String) + +attributeName :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String) +attributeName = map toMaybe <<< _attributeName + +foreign import _attributeNamespace :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String) + +attributeNamespace :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String) +attributeNamespace = map toMaybe <<< _attributeNamespace + +foreign import _oldValue :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Nullable String) + +oldValue :: forall eff. MutationRecord -> Eff (dom :: DOM | eff) (Maybe String) +oldValue = map toMaybe <<< _oldValue From cf55d3f7fc4165fa64b15ae7c762cc7a9dd33167 Mon Sep 17 00:00:00 2001 From: Nathan Faubion Date: Sun, 10 Dec 2017 10:58:57 -0800 Subject: [PATCH 2/2] Updated MutationObserverInit --- src/DOM/Node/MutationObserver.js | 2 +- src/DOM/Node/MutationObserver.purs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/DOM/Node/MutationObserver.js b/src/DOM/Node/MutationObserver.js index 1e44368..29523b5 100644 --- a/src/DOM/Node/MutationObserver.js +++ b/src/DOM/Node/MutationObserver.js @@ -8,7 +8,7 @@ exports.mutationObserver = function (cb) { }; }; -exports.observe = function (node) { +exports._observe = function (node) { return function (config) { return function (mo) { return function () { diff --git a/src/DOM/Node/MutationObserver.purs b/src/DOM/Node/MutationObserver.purs index 02720b9..7f0d4a5 100644 --- a/src/DOM/Node/MutationObserver.purs +++ b/src/DOM/Node/MutationObserver.purs @@ -1,6 +1,6 @@ module DOM.Node.MutationObserver ( MutationObserver - , MutationObserverInit + , MutationObserverInitFields , mutationObserver , observe , disconnect @@ -16,22 +16,31 @@ import DOM.Node.Types (Node) foreign import data MutationObserver :: Type -type MutationObserverInit = - { childList :: Boolean +type MutationObserverInitFields = + ( childList :: Boolean , attributes :: Boolean , characterData :: Boolean , subtree :: Boolean , attributeOldValue :: Boolean , characterDataOldValue :: Boolean , attributeFilter :: Array String - } + ) foreign import mutationObserver :: forall eff . (MutationRecord -> MutationObserver -> Eff (dom :: DOM | eff) Unit) -> Eff (dom :: DOM | eff) MutationObserver -foreign import observe :: forall eff. Node -> MutationObserverInit -> MutationObserver -> Eff (dom :: DOM | eff) Unit +foreign import _observe :: forall eff r. Node -> Record r -> MutationObserver -> Eff (dom :: DOM | eff) Unit + +observe + :: forall eff r rx + . Union r rx MutationObserverInitFields + => Node + -> Record r + -> MutationObserver + -> Eff (dom :: DOM | eff) Unit +observe = _observe foreign import disconnect :: forall eff. MutationObserver -> Eff (dom :: DOM | eff) Unit