Skip to content

Instantly share code, notes, and snippets.

@jeroencornelissen
Created February 15, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroencornelissen/9768f410b721461f470001c133a291e9 to your computer and use it in GitHub Desktop.
Save jeroencornelissen/9768f410b721461f470001c133a291e9 to your computer and use it in GitHub Desktop.
const Routing = DefineMap.extend('MyDocRouting', {
item: {
Type: Item,
set(newValue) {
this.itemId = newValue && newValue.id ? newValue.id : undefined;
return newValue;
},
serialize: false
},
itemId: {
type: 'string'
},
listHash: {
Value: DefineMap,
serialize: false
},
deeplink(allItems) {
const itemId = this.itemId;
this.listHash.assignDeep(getHashOfFoldersList(allItems)); // creates a flat object { key: item }
if (this.listHash[itemId]) {
this.item = this.listHash[itemId];
this.dispatch('itemId', [itemId]);
} else {
mydocState.itemsLoading = true;
Folder.getPath(itemId).then(this._getFolderPath.bind(this), error => {
console.error(error);
});
}
},
_getFolderPath(path) {
const itemId = this.itemId;
const foldersToRequest = path.filter(
pathId => (this.listHash[pathId] && this.listHash[pathId].children ? false : true)
);
const foldersToOpen = path.filter(
pathId => (this.listHash[pathId] && this.listHash[pathId].children ? true : false)
);
foldersToOpen.forEach(pathId => {
if (this.listHash[pathId] && this.listHash[pathId].isOpened === false) {
this.listHash[pathId].isOpened = true;
}
});
const promises = [];
foldersToRequest.forEach(pathId => {
promises.push(DirectoryListing.getItems(mydocState.defaultPages.files, pathId));
});
Promise.all(promises).then(
values => {
try {
let deeplinkFound = false;
values.forEach((data, idx) => {
const pathId = foldersToRequest[idx];
this.listHash.assignDeep(getHashOfFoldersList(data));
if (this.listHash[pathId]) {
const folder = this.listHash[pathId];
folder.children = data;
folder.childrenSetHelper = true;
folder.isOpened = true;
if (idx === foldersToRequest.length - 1) {
const deeplinkItem = folder.children.filter(item => item.id === itemId);
if (deeplinkItem.length === 1) {
deeplinkFound = true;
this.item = deeplinkItem[0];
this.dispatch('itemId', [itemId]);
}
}
}
});
if (!deeplinkFound) {
this.item = this.listHash[mydocState.defaultPages.files];
}
} catch (err) {
throw err;
}
},
error => {
console.error(error);
}
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment