2020-09-07 23:35:41 +02:00
import linkService from "../services/link.js" ;
import server from "../services/server.js" ;
2021-04-16 23:01:56 +02:00
import froca from "../services/froca.js" ;
2020-09-07 23:35:41 +02:00
import TabAwareWidget from "./tab_aware_widget.js" ;
import options from "../services/options.js" ;
const TPL = `
< div class = "similar-notes-widget hide-in-zen-mode" >
< style >
. similar - notes - wrapper {
2020-09-17 14:34:10 +02:00
max - height : 75 px ;
2020-09-07 23:35:41 +02:00
overflow : auto ;
}
. similar - notes - wrapper a {
display : inline - block ;
border : 1 px dotted var ( -- main - border - color ) ;
border - radius : 20 px ;
background - color : var ( -- accented - background - color ) ;
padding : 0 10 px 0 10 px ;
margin : 0 3 px 0 3 px ;
max - width : 10 em ;
text - overflow : ellipsis ;
white - space : nowrap ;
overflow : hidden ;
}
< / s t y l e >
2020-09-25 23:52:24 +02:00
< div class = "area-expander" >
2020-09-07 23:35:41 +02:00
< hr class = "w-100" >
2020-09-25 23:52:24 +02:00
< div class = "area-expander-text"
2020-09-07 23:35:41 +02:00
title = "This list contains notes which might be similar to the current note based on textual similarity of note title, its labels and relations." > < / d i v >
< hr class = "w-100" >
< / d i v >
< div class = "similar-notes-wrapper" > < / d i v >
< / d i v >
` ;
export default class SimilarNotesWidget extends TabAwareWidget {
2020-09-13 22:23:03 +02:00
isEnabled ( ) {
2020-12-25 13:06:58 +01:00
return super . isEnabled ( )
&& this . note . type !== 'search'
&& ! this . note . hasLabel ( 'similarNotesWidgetDisabled' ) ;
2020-09-13 22:23:03 +02:00
}
2020-09-07 23:35:41 +02:00
doRender ( ) {
this . $widget = $ ( TPL ) ;
this . overflowing ( ) ;
this . $similarNotesWrapper = this . $widget . find ( ".similar-notes-wrapper" ) ;
2020-09-25 23:52:24 +02:00
this . $expanderText = this . $widget . find ( ".area-expander-text" ) ;
2020-09-07 23:35:41 +02:00
2020-09-25 23:52:24 +02:00
this . $expander = this . $widget . find ( '.area-expander' ) ;
2020-09-07 23:35:41 +02:00
this . $expander . on ( 'click' , async ( ) => {
const collapse = this . $similarNotesWrapper . is ( ":visible" ) ;
await options . save ( 'similarNotesExpanded' , ! collapse ) ;
this . triggerEvent ( ` similarNotesCollapsedStateChanged ` , { collapse } ) ;
} ) ;
return this . $widget ;
}
noteSwitched ( ) {
const noteId = this . noteId ;
this . toggleInt ( false ) ;
this . $similarNotesWrapper . hide ( ) ; // we'll open it in refresh() if needed
// avoid executing this expensive operation multiple times when just going through notes (with keyboard especially)
// until the users settles on a note
setTimeout ( ( ) => {
if ( this . noteId === noteId ) {
this . refresh ( ) ;
}
} , 1000 ) ;
}
2020-09-10 21:01:46 +02:00
async refresh ( ) {
2020-09-13 22:47:23 +02:00
if ( ! this . isEnabled ( ) ) {
return ;
}
2020-09-07 23:35:41 +02:00
// remember which title was when we found the similar notes
2020-09-10 21:01:46 +02:00
this . title = this . note . title ;
2020-09-07 23:35:41 +02:00
const similarNotes = await server . get ( 'similar-notes/' + this . noteId ) ;
2020-10-19 22:10:25 +02:00
if ( ! similarNotes ) {
this . toggleInt ( false ) ;
return ;
}
2020-09-07 23:35:41 +02:00
this . toggleInt ( similarNotes . length > 0 ) ;
if ( similarNotes . length === 0 ) {
return ;
}
if ( options . is ( 'similarNotesExpanded' ) ) {
this . $similarNotesWrapper . show ( ) ;
}
2020-09-25 23:52:24 +02:00
this . $expanderText . text ( ` ${ similarNotes . length } similar note ${ similarNotes . length === 1 ? '' : "s" } ` ) ;
2020-09-07 23:35:41 +02:00
const noteIds = similarNotes . flatMap ( note => note . notePath ) ;
2021-04-16 22:57:37 +02:00
await froca . getNotes ( noteIds , true ) ; // preload all at once
2020-09-07 23:35:41 +02:00
const $list = $ ( '<div>' ) ;
for ( const similarNote of similarNotes ) {
2021-04-16 22:57:37 +02:00
const note = await froca . getNote ( similarNote . noteId , true ) ;
2020-09-07 23:35:41 +02:00
if ( ! note ) {
continue ;
}
const $item = ( await linkService . createNoteLink ( similarNote . notePath . join ( "/" ) ) )
2020-09-17 09:33:43 +02:00
. css ( "font-size" , 24 * ( 1 - 1 / ( 1 + similarNote . score ) ) ) ;
2020-09-07 23:35:41 +02:00
$list . append ( $item ) ;
}
this . $similarNotesWrapper . empty ( ) . append ( $list ) ;
}
entitiesReloadedEvent ( { loadResults } ) {
if ( this . note && this . title !== this . note . title ) {
this . rendered = false ;
this . refresh ( ) ;
}
}
/ * *
* This event is used to synchronize collapsed state of all the tab - cached widgets since they are all rendered
* separately but should behave uniformly for the user .
* /
similarNotesCollapsedStateChangedEvent ( { collapse } ) {
if ( collapse ) {
this . $similarNotesWrapper . slideUp ( 200 ) ;
} else {
this . $similarNotesWrapper . slideDown ( 200 ) ;
}
}
}