-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VYZN: Matrix Widget API: Hiding of one or more elements programmatica…
…lly (#704) * feat: hiding elements programmatically [widget api] * uncomment cypress tests --------- Co-authored-by: Ibrahim Saad <ibrahim.saad@xbim.net> Co-authored-by: Pablo Mayrgundter <pablo.mayrgundter@gmail.com>
- Loading branch information
1 parent
04a8a22
commit bcf0c24
Showing
10 changed files
with
319 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/WidgetApi/event-dispatchers/HiddenElementsEventDispatcher.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import ApiEventDispatcher from './ApiEventDispatcher' | ||
import Utils from '../Utils' | ||
import useStore from '../../store/useStore' | ||
import {unsortedArraysAreEqual} from '../../utils/arrays' | ||
|
||
/** | ||
* class HiddenElementsEventDispatcher | ||
*/ | ||
class HiddenElementsEventDispatcher extends ApiEventDispatcher { | ||
name = 'ai.bldrs-share.HiddenElements' | ||
utils = null | ||
apiConnection = null | ||
|
||
/** | ||
* constructor | ||
* | ||
* @param {object} apiConnection AbstractApiConnection | ||
* @param {object} searchIndex SearchIndex | ||
*/ | ||
constructor(apiConnection, searchIndex) { | ||
super() | ||
this.apiConnection = apiConnection | ||
this.utils = new Utils(searchIndex) | ||
} | ||
|
||
/** | ||
* initialize dispatcher. | ||
* | ||
*/ | ||
initDispatch() { | ||
let lastHiddenElementsGlobalIds = [] | ||
useStore.subscribe((state, previousState) => { | ||
const propertyStateChanged = (state.hiddenElements !== previousState.hiddenElements) | ||
if (!propertyStateChanged) { | ||
return | ||
} | ||
const hiddenElmentsExpressIds = [] | ||
Object.entries(state.hiddenElements).forEach((entry) => { | ||
const [key, value] = entry | ||
if (value === true) { | ||
hiddenElmentsExpressIds.push(key) | ||
} | ||
}) | ||
const currHiddenElementsGlobalIds = this.utils.getElementsGlobalIds(hiddenElmentsExpressIds) | ||
const noChanges = unsortedArraysAreEqual(currHiddenElementsGlobalIds, lastHiddenElementsGlobalIds) | ||
if (noChanges) { | ||
return | ||
} | ||
const eventData = {previous: lastHiddenElementsGlobalIds, current: currHiddenElementsGlobalIds} | ||
this.apiConnection.send(this.name, eventData) | ||
lastHiddenElementsGlobalIds = currHiddenElementsGlobalIds | ||
}) | ||
} | ||
} | ||
|
||
export default HiddenElementsEventDispatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import useStore from '../../store/useStore' | ||
import ApiEventHandler from './ApiEventHandler' | ||
/** | ||
* Hide Elements API event handler | ||
*/ | ||
class HideElementsEventHandler extends ApiEventHandler { | ||
apiConnection = null | ||
name = 'ai.bldrs-share.HideElements' | ||
|
||
|
||
/** | ||
* constructor | ||
* | ||
* @param {object} apiConnection AbstractApiConnection | ||
* @param {object} searchIndex SearchIndex | ||
*/ | ||
constructor(apiConnection, searchIndex) { | ||
super() | ||
this.apiConnection = apiConnection | ||
this.searchIndex = searchIndex | ||
} | ||
|
||
/** | ||
* The handler for this event | ||
* | ||
* @param {object} data the event associated data | ||
* @return {object} the response of the API call | ||
*/ | ||
handler(data) { | ||
if (!('globalIds' in data)) { | ||
return this.apiConnection.missingArgumentResponse('globalIds') | ||
} | ||
|
||
if (data.globalIds === null) { | ||
return this.apiConnection.invalidOperationResponse('globalIds can\'t be null') | ||
} | ||
|
||
const expressIds = [] | ||
|
||
if (data.globalIds.length) { | ||
for (const globalId of data.globalIds) { | ||
const expressId = this.searchIndex.getExpressIdByGlobalId(globalId) | ||
if (expressId) { | ||
expressIds.push(expressId) | ||
} | ||
} | ||
} | ||
|
||
useStore.getState().viewerStore.isolator.hideElementsById(expressIds.map((id) => Number(id))) | ||
|
||
return this.apiConnection.successfulResponse({}) | ||
} | ||
} | ||
|
||
export default HideElementsEventHandler |
Oops, something went wrong.