Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | 14x 752x 70x 386x 625x 548x 548x 506x 506x 42x 31x 11x 208x 208x 202x 6x 756x 756x 756x 1766x 1766x 1766x 14x 14x 1752x 508x 1244x 742x 235x 507x 270x 270x 117x 117x 117x 117x 270x 219x 219x 11x 11x 11x 11x 208x 138x 208x 208x 208x 6x 202x 202x 917x 6x 911x 3x 1x 2x 1x 1x 189x 117x 58x 2x 56x 87x 87x 254x 1x 253x 253x 69x 184x 84x 9x 7x 5x 1x 4x 4x 1x 3x 6x 548x 548x 139x 548x 548x 548x 11x 537x 537x 1928x 1920x 122x 120x 133x 665x 765x 590x 588x 12x 576x 120x 439x 437x 300x 137x 35x 102x 102x 1x 1x 1x 1x 101x 101x 434x 432x 294x 138x 38x 100x 100x 1x 1x 1x 1x 99x 99x 3391x 14x 3377x | /*
* Handbook data wrapper object
*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2019
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// @flow
import handbook from '../data/handbok/handbok.json';
import type { HandbookPath, HandbookDataStructure, HandbookDataEntry } from './types/data';
import type { searchSnippetComponent } from './helper.lunrSnippet';
/**
* Base class for Handbook* objects. Provides shared methods for looking up
* objects.
*/
class HandbookBase
{
_toplevelParent: ?HandbookIndex;
/**
* Internal warning emitter for Handbook* objects
*
* XXX: Should be able to differentiate between various subclasses
*/
_warn(message: string)
{
// eslint-disable-next-line no-console
console.log('HandbookEntry: '+message);
}
/**
* Retrieves the root datastructure for this Handbook class. You can override
* this in a subclass to override the datastructure that is being used.
*/
_rootDatastructure (): HandbookDataStructure
{
return handbook;
}
/**
* Retrives the HandbookIndex variant for this Handbook variant. You can override
* this in a subclass to override which object gets created.
*/
_newHandbookIndex (path: HandbookPath): HandbookIndex
{
return new HandbookIndex(path);
}
/**
* Retrives the HandbookEntry variant for this Handbook variant. You can override
* this in a subclass to override which object gets created.
*/
_newHandbookEntry (path: HandbookPath): HandbookEntry
{
return new HandbookEntry(path);
}
/**
* Retrieves the root path for this Handbook variant
*/
_rootPath(): string
{
return '/handbok/';
}
/**
* Looks up a single entry from the handbook based upon a path.
* Will always either return the entry or null, it will not return
* an index entry (use _lookupIndex for that)
*/
_lookupEntry(path: HandbookPath): HandbookDataEntry | null
{
let index: HandbookDataEntry | HandbookDataStructure | null = this.__lookup(path);
if(index !== null && index.content !== undefined && index.order === undefined)
{
index = ((index: $UnsafeRecast): HandbookDataEntry);
return index;
}
if(index !== null && index.article !== undefined)
{
return index.article;
}
return null;
}
/**
* Looks up a single index entry from the handbook based upon a path.
* Will always either return the entry or null, it will not return
* an actual entry (use _lookupEntry for that)
*/
_lookupIndex(path: HandbookPath): HandbookDataStructure | null
{
const index = this.__lookup(path);
if(index !== null && index.content === undefined && index.order !== undefined && index.tree !== undefined)
{
return index;
}
return null;
}
/**
* The method that powers the other _lookup* methods. Will iterate through
* the datastructure to find 'path' and return it. It is up to the caller
* to determine if the path is a HandbookDataEntry or a HandbookDataStructure.
*
* This method should not be called by any subclasses directly.
*/
__lookup(path: HandbookPath): HandbookDataEntry | HandbookDataStructure | null
{
// Make a copy of the path array, so that we can do destructive
// modifications to it
const lookupPath : HandbookPath = [].concat(path);
let currentTree : HandbookDataStructure = this._rootDatastructure();
let result: ?HandbookDataEntry | ?HandbookDataStructure;
while(lookupPath.length)
{
const current = lookupPath.shift();
const entry = currentTree.tree[current];
if(entry === undefined)
{
this._warn(this.constructor.name+': Unable to lookup path: '+lookupPath.join('/')+' - '+current+' does not exist');
return null;
}
if(entry.tree === undefined && entry.content !== undefined)
{
// We use UnsafeRecast because flow has problems knowing that
// if tree is undefined and content defined, that means we have a
// HandbookDataEntry, not another HandbookDataStructure
result = ((entry: $UnsafeRecast): HandbookDataEntry);
}
else
{
// We use UnsafeRecast because flow has problems knowing that
// if tree is defined and content undefined, that means we have a
// HandbookDataEntry, not another HandbookDataStructure
currentTree = ((entry: $UnsafeRecast): HandbookDataStructure);
}
}
if(result === undefined)
{
return currentTree;
}
return result;
}
/**
* Locates the toplevel parent of this entry. Toplevel parent is the
* highest entry in the tree *except* the root element.
*
* Returns `this` if it is the toplevel parent.
*/
get toplevelParent(): HandbookIndex
{
// We cache the toplevel parent if it's not ourself
let parent: ?HandbookIndex = this._toplevelParent;
if (parent === null || parent === undefined)
{
Iif (this instanceof HandbookIndex && this.path.length == 1)
{
parent = this;
}
else
{
const path = this.path[0];
parent = new HandbookIndex(path);
this._toplevelParent = parent;
}
}
return parent;
}
/**
* Stubbed path
*/
get path(): HandbookPath
{
throw("Subclass of HandbookBase MUST override path!");
}
}
/**
* Represents a single "index" entry in the handbook. That is, an entry that
* contains other entries.
*/
class HandbookIndex extends HandbookBase
{
exists: boolean;
_entry: HandbookDataStructure;
_firstChild: HandbookEntry | HandbookIndex;
path: HandbookPath;
constructor(path?: HandbookPath | string) // eslint-disable-line require-jsdoc
{
super();
if(path === undefined)
{
this.exists = true;
this._entry = this._rootDatastructure();
this.path = [];
return;
}
if(typeof(path) === "string")
{
path = path.split('/');
}
const lookup = this._lookupIndex(path);
this.path = path;
if(lookup === null)
{
this.exists = false;
}
else
{
this._entry = lookup;
this.exists = true;
}
}
/**
* Retrieves the HandbookDataStructure for this entry
*/
get value(): HandbookDataStructure
{
if (!this.exists)
{
throw('Attempt to retrieve .value from a non-existing HandbookIndex: '+this.path.join('/'));
}
return this._entry;
}
/**
* Retrieves the article for this entry, if it exists
*/
get article (): HandbookEntry | null
{
if (!this.exists)
{
throw('Attempt to retrieve .article from a non-existing HandbookIndex: '+this.path.join('/'));
}
if(this._entry.article !== undefined)
{
return this._newHandbookEntry(this.path);
}
return null;
}
/**
* Retrieves the title for this entry
*/
get title(): string
{
return this.value.title;
}
/**
* Retrieves the displayIndexLink for this entry
*/
get displayIndexLink(): boolean
{
return this.value.displayIndexLink === true;
}
/**
* Retrieves the URL for this entry
*/
get url(): string
{
if (!this.exists)
{
throw('Can not retrieve .url for non-existing object');
}
return this._rootPath()+this.path.join('/');
}
/**
* Retrieves the list of entries contained within this one
*/
get tree(): Array<HandbookEntry | HandbookIndex>
{
const tree = [];
for(const entry of this.value.order)
{
if(this.value.tree[entry] === undefined)
{
throw('tree(): undefined entry listed in order');
}
const path = [].concat(this.path,entry);
if(this.value.tree[entry].tree !== undefined)
{
tree.push(
this._newHandbookIndex(path)
);
}
else
{
tree.push(
this._newHandbookEntry(path)
);
}
}
return tree;
}
/**
* Retrieves the first child contained within this one
*
* This is essentially the same as `tree.shift()`, but is slightly optimized
* because it doesn't have to iterate through everything
*/
get firstChild(): HandbookEntry | HandbookIndex
{
if(this._firstChild === undefined)
{
const entry = this.value.order[0];
if(this.value.tree[entry] === undefined)
{
throw('firstChild(): undefined entry listed in order');
}
const path = [].concat(this.path,entry);
if(this.value.tree[entry].tree !== undefined)
{
this._firstChild = this._newHandbookIndex(path);
}
else
{
this._firstChild = this._newHandbookEntry(path);
}
}
return this._firstChild;
}
}
/**
* Represents a single entry in the handbook. Use this instead of referencing
* the handbook data structure directly, whenever possible. It makes looking up
* prev/next entries etc. much easier.
*/
class HandbookEntry extends HandbookBase
{
_entry: HandbookDataEntry;
_next: HandbookEntry | null;
_previous: HandbookEntry | null;
_path: HandbookPath;
searchSnippet: Array<searchSnippetComponent>;
exists: boolean;
constructor(path: HandbookPath | string) // eslint-disable-line require-jsdoc
{
super();
if(typeof(path) === "string")
{
path = path.split('/');
}
this._path = path;
const lookup = this._lookupEntry(path);
if(lookup === null)
{
this.exists = false;
}
else
{
this._entry = lookup;
this.exists = true;
}
}
/**
* Retrieves the HandbookDataEntry for this entry
*/
get value(): HandbookDataEntry
{
this._verifySelf('value');
return this._entry;
}
/**
* Retrieves the toc for this entry
*/
get toc(): Array<string>
{
return this.value.toc;
}
/**
* Retrieves the "hide TOC" value for this entry (usually false)
*/
get hideTOC(): boolean
{
return !!this.value.hideTOC;
}
/**
* Retrieves the content for this entry
*/
get content(): string
{
return this.value.content;
}
/**
* Retrieves the title for this entry
*/
get title(): string
{
return this.value.title;
}
/**
* Retrieves the path for this entry
*/
get path(): HandbookPath
{
return this.value.path;
}
/**
* Retrieves the fully-qualified URL for this entry
*/
get url(): string
{
this._verifySelf('url');
if(this._entry.customURL !== undefined)
{
return this._entry.customURL;
}
return this._rootPath()+this.path.join('/');
}
/**
* Determines if this entry has been whitelisted for public access
*/
get public(): boolean
{
return this.value.public;
}
/**
* Retrieves the next HandbookDataEntry or null
*/
get next(): HandbookEntry | null
{
this._verifySelf('next');
if(this._next !== undefined)
{
return this._next;
}
if(this._entry.next === undefined || !this.exists)
{
return null;
}
const lookup = this._newHandbookEntry( this._entry.next );
if(!lookup.exists)
{
let path: string = '';
Eif(this._entry.next !== undefined)
{
path = this._entry.next.join('/');
}
throw('Failed to look up '+path);
}
this._next = lookup;
return this._next;
}
/**
* Retrieves the previous HandbookDataEntry or null
*/
get previous(): HandbookEntry | null
{
this._verifySelf('previous');
if(this._previous !== undefined)
{
return this._previous;
}
if(this._entry.previous === undefined || !this.exists)
{
return null;
}
const lookup = this._newHandbookEntry( this._entry.previous );
if(!lookup.exists)
{
let path: string = '';
Eif(this._entry.previous !== undefined)
{
path = this._entry.previous.join('/');
}
throw('Failed to look up '+path);
}
this._previous = lookup;
return this._previous;
}
/**
* Private function that is used to verify that this object is valid (ie.
* that it `.exists`). If it isn't valid, then we throw()
*/
_verifySelf(caller: string): null
{
if (!this.exists)
{
throw('Attempt to retrieve .'+caller+' from a non-existing '+this.constructor.name+': '+this._path.join('/'));
}
return null;
}
}
export default HandbookEntry;
export { HandbookEntry, HandbookIndex };
|