Repository navigation
Expand file tree
/
Copy pathpwiki.js
More file actions
executable file
·1633 lines (1373 loc) · 41 KB
/
Copy pathpwiki.js
File metadata and controls
executable file
·1633 lines (1373 loc) · 41 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
*
*
* XXX this seems a bit overcomplicated...
*
**********************************************************************/
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
(function(require){ var module={} // make module AMD/node compatible...
/*********************************************************************/
var object = require('lib/object')
var actions = require('lib/actions')
var features = require('lib/features')
var macro = require('macro')
/*********************************************************************/
// Split path into a list and handle special path elements...
//
// The path is split via '/' or '\', no difference is made between the
// two styles, e.g. 'a/b' is the same as 'a\b'.
//
// Consecutive '/' or '\' are treated as one, e.g. 'a///b' and 'a/b'
// are the same.
//
// Special path items supported:
// "." - Current position indicator
// this is simply removed from positions other than 0
// a/./b/c -> a/b/c
// ./b/c -> ./b/c
// ".." - Consumes path item above (pop) / up one level
// a/../b/c -> b/c
// ../b/c -> ../b/c
// ">>" - Consumes path item below (shift)
// a/>>/b/c -> a/c
// a/b/c/>> -> a/b/c (XXX ???)
//
// NOTE: the path is handled out of context, this leading '.' and '..'
// are left as-is.
// NOTE: '>>' has no effect when at last position (XXX ???)
var path2list =
module.path2list = function(path){
return (path instanceof Array ?
path
: path.split(/[\\\/]+/g))
// handle '..' (lookahead) and trim path elements...
// NOTE: this will not touch the leading '.' or '..'
.map(function(p, i, l){
// remove '..' and '.' out at positions > 0...
return (i > 0
&& (p.trim() == '..' || p.trim() == '.')
// remove items followed by '..'...
|| (l[i+1] || '').trim() == '..'
// remove items preceded by '>>'...
|| (l[i-1] || '').trim() == '>>') ?
null
: p.trim() })
// cleanup and clear '>>'...
.filter(function(p){
return p != null
&& p != ''
&& p != '>>' })}
// Normalize path...
//
// This is the same as path2list(..) but also joins the path with '/'
var normalizePath =
module.normalizePath = function(path){
return path2list(path).join('/') }
// XXX add pattern to match all subpaths above...
// Ex:
// 'a/b/c/^' shold mathc 'a/b/c', 'a/b', 'a'
var path2re =
module.path2re = function(path){
return RegExp('^'
+normalizePath(path)
/*/ quote regexp chars...
.replace(/([\.\\\/\(\)\[\]\$\+\-\{\}\@\^\&\?\<\>])/g, '\\$1')
/*/// XXX experimental...
.replace(/([\.\\\/\(\)\[\]\$\+\-\{\}\@\^\&\?\>])/g, '\\$1')
// '<' -> handle subpath matching...
// XXX this needs * up the stack...
.replace(/.*</, function(match){
return `(${ match
.replace(/(\\[\\\/])?<$/, '')
.split(/\\[\\\/]/)
.reduce(function(res, e){
return res.concat([
(res.length > 0 ?
res[res.length-1] +'\/'
: '')
+ e]) }, [])
.join('|') })` })
//*/
// convert '*' and '**' to regexp...
.replace(/\*\*/g, '.*')
.replace(/^\*|([^.])\*/g, '$1[^\\/]*')
+'$')}
/*********************************************************************/
// pWiki featureset...
var pWikiFeatures =
module.pWikiFeatures = new features.FeatureSet()
/*
// base pWiki object...
var pWiki =
module.pWiki = object.Constructor('pWiki', actions.MetaActions)
// base instance constructor...
pWikiFeatures.__actions__ =
function(){ return actions.Actions(pWiki()) }
//*/
/*********************************************************************/
var BaseData =
module.BaseData = {
// Macro acces to standard page attributes (paths)...
'System/title': function(){
return { text: this.get('..').title() } },
'System/path': function(){
return { text: this.base() } },
'System/dir': function(){
return { text: this.get('..').base() } },
'System/location': function(){
return { text: this.base() } },
'System/resolved': function(){
return { text: this.get('..').acquire() } },
// page data...
//
// NOTE: special case: ./raw is treated a differently when getting .text
// i.e:
// .get('./raw').text
// is the same as:
// .get('.').raw
'System/raw': function(){
return { text: this.get('..').raw() } },
'System/html': function(){
return { text: this.get('..').html() } },
// list all path elements on a level, including empty path sections...
// XXX update these to the new format -- must return an object...
// XXX move this to Wiki.children + rename...
// XXX
'System/list': function(){
return 'NoImplemented'
var p = this.dir
return Object.keys(this.__wiki_data)
.map(function(k){
if(k.indexOf(p) == 0){
return path2lst(k.slice(p.length)).shift() }
return null })
.filter(function(e){ return e != null })
.sort()
.map(function(e){ return '['+ e +']' })
.join('<br>') },
// list links to this page...
// XXX this is done, though we cant use this until we solve .html(..)
// macro recursion issues...
// XXX cache the result + need a strategy to drop parts of cache when
// irrelevant -- when path/text changes...
// XXX might be a good idea to move this to the store, at least the
// management, part...
'System/links': function(){
return 'NoImplemented'
var that = this
var p = this.path()
var res = []
this.wiki.match('**')
.forEach(function(p){
var pa = that.acquire(p)
that.get(p)
// XXX this will render the page which might not be
// the best idea in some cases...
.links()
.forEach(function(l){
var la = that.acquire(l)
if(l == p || la == p || la == pa){
res.push([l, p]) } }) })
// cache the result...
// XXX
this.attr('rev-links', res)
return res
//.map(function(e){ return '['+ e[0] +'] <i>from page: ['+ e[1] +']</i>' })
.map(function(e){ return '['+ e[1] +'] <i>-> ['+ e[0] +']</i>' })
.sort()
.join('<br>') },
// Page modifiers/actions...
// XXX these needs redirecting...
// ...not sure if using history here is the right way to go...
'System/_sort': function(){
this.get('..').sort() },
'System/sort': function(){
// XXX does not work for some reason...
//this.get('../_sort')
this.get('..').sort()
history
&& history.back() },
'System/_reverse': function(){
this.get('..').reverse() },
'System/reverse': function(){
// XXX does not work for some reason...
//this.get('../_reverse')
this.get('..').reverse()
history
&& history.back() },
'System/_delete': function(){
this.get('..').clear() },
'System/delete': function(){
// XXX does not work for some reason...
//this.get('../_delete')
this.get('..').clear()
history
&& history.back() },
'System/back': function(){
history.go(-2) },
// XXX not sure how to deal with this...
//'System/foreward': function(){
// history.go(1) },
// XXX need to support simple functions...
// ...return a list to simulate a list of pages...
'System/test': function(){
return ['list', 'of', 'links'] },
}
/*********************************************************************/
// XXX should this be art of the main API or a separate entity???
// XXX should we combine page and wiki api???
// - pWikiData is wiki api
// - pWiki is page api
var pWikiData =
module.pWikiData = {
__data: null,
// XXX
search: function(query, sort){
},
// Get a list of matching paths...
//
// XXX sort path API...
// ...should we be able to spec sort in path???
// XXX should we account for order here???
match: function(path, sort, count, from){
var data = this.__data || {}
from = from || 0
// XXX normalize this to account for '*'
//var order = (data[path] || {}).order || []
if(path == null){
return [] }
// strict path...
if(path.indexOf('*') < 0){
return path in data ?
[ path ]
: [] }
sort = sort
|| (data[path] || {}).sort
|| ['order']
sort = sort instanceof Array ?
sort
: [sort]
var order = (data[path] || {}).order || []
var pattern = path2re(path)
return Object.keys(data)
// XXX is this correct???
.concat(Object.keys(data.__proto__)
// do not repeat overloaded stuff...
.filter(function(e){
return !data.hasOwnProperty(e) }))
.filter(function(p){
return pattern.test(p) })
// page...
.slice(from,
count ?
from + count
: undefined)
// prepare to sort...
.map(function(p, i){
return sort
.map(function(method){
// explicit order...
if(method instanceof Array){
i = method.indexOf(p)
i = i < 0 ? method.indexOf('*') : i
i = i < 0 ? method.length : i
return i }
// drop the reversal marker...
method = method[0] == '-' ?
method.slice(1)
: method
// stored order...
if(method == 'order'){
i = order.indexOf(p)
i = i < 0 ?
order.indexOf('*')
: i
i = i < 0 ?
order.length
: i
return i }
return method == 'path' ?
p.toLowerCase()
: method == 'Path' ?
p
: method == 'title' ?
path2list(p).pop().toLowerCase()
: method == 'Title' ?
path2list(p).pop()
// special case...
: method == 'checked' ?
(data[p][method] ?
1
: 0)
// attr...
: data[p][method] })
.concat([i, p]) })
// sort...
.sort(function(a, b){
for(var i=0; i < sort.length+1; i++){
var reverse = (sort[i] || '')[0] == '-' ? -1 : 1
if(a[i] == b[i]){
continue }
return (a[i] > b[i] ? 1 : -1) * reverse }
return 0 })
// cleanup...
.map(function(e){
return e.pop() }) },
// Get/set data at path...
//
// XXX should this overwrite or expand???
// XXX should from be pattern compatible???
data: function(path, value){
// get the data...
if(value == null){
if(this.__data == null){
return null }
var data = this.__data[path]
return data == null ?
null
: data instanceof Function ?
data
: JSON.parse(JSON.stringify(data))
// set the data...
} else {
this.__data = this.__data || {}
this.__data[path] = JSON.parse(JSON.stringify(value))
return this } },
// Move data from path to path...
//
// XXX should from be pattern compatible???
move: function(from, to){
if(this.__data == null){
return }
var d = this.__data[from]
this.clear(from)
this.__data[to] = d
return this },
// Clear a path...
//
clear: function(path){
if(this.__data == null){
return this }
this.remove(this.match(path))
return this },
// explicitly remove path...
//
// NOTE: this is similar to .clear(..) but will not expand patterns,
// thus only one page is is removed per path.
remove: function(path){
path = arguments.length > 1 ?
[].slice.call(arguments)
: path instanceof Array ?
path
: [path]
var data = this.__data
path.forEach(function(p){
delete data[p] })
return this },
// XXX
json: function(data){
if(arguments.length == 0){
return JSON.parse(JSON.stringify(this.__data))
} else {
this.__data = data } },
}
/*********************************************************************/
// Base pWiki page API...
//
// Page data format:
// {
// 'order': [ <title>, .. ] | undefined,
// 'order-unsorted-first': <bool>,
//
// 'text': <string>,
//
// // XXX not yet used...
// 'links': [ .. ],
// }
//
var pWikiBase =
module.pWikiBase = actions.Actions({
config: {
'home-page': 'WikiHome',
'default-page': 'EmptyPage',
'no-match-page': 'NoMatch',
'system-path': 'System',
'acquesition-order': [
'Templates',
],
'post-acquesition-order': [],
'order-unsorted-first': false,
// sorting...
'default-sort-methods': [
'path',
],
},
// pWikiData...
wiki: null,
// XXX should this be local/dump???
json: ['',
function(){ }],
// Location and path API...
refresh: ['',
function(force){
// get/set location and base fields...
var location = this.__location = this.__location || {}
var path = location.path = location.path
|| this.config['home-path']
|| 'WikiHome'
var at = location.at || 0
// get location cache...
var match = location.match
// refresh the cache...
if(match == null || force){
this.order(force) } }],
location: ['Page/Get or set location',
function(value){
if(value === null){
return }
var location = this.__location || this.refresh().location()
// get location...
if(arguments.length == 0){
return location }
// set location index...
if(typeof(value) == typeof(123)){
location.at = value
// set location path...
} else if(typeof(value) == typeof('str')){
this.__location = {
path: this.resolve(value),
at: 0,
}
// object...
} else {
this.__location = value
// NOTE: we are returning here without a refresh to avoid
// recursion...
// NOTE: a refresh will get called when the location value
// is accessed for the first time...
// XXX should we clear .match here???
return }
this.refresh(true) }],
exists: ['Page/Check if path explicitly exists.',
function(path){
var at = path ?
0
: this.at()
path = path || this.path()
return this.wiki.match(this.get(path).location().path)[at] !== undefined }],
// Resolve path statically...
//
// This will:
// - expand variables
// - resolve relative paths ('.', '..', and '>>')
//
// Supported variables:
// $NOW - resolves to current date (same as Date.now())
//
// $PATH - resolves to page path (same as .path())
// $BASE - resolves to page base path (same as .base())
// $TITLE - resolves to page title (same as .title())
//
// $INDEX - resolves to page index (same as .at())
//
// NOTE: all variables are resolved relative to the page from which
// .resolve(..) was called, e.g. the following two are equivalent:
// <page>.resolve('$PATH')
// <page>.path()
// NOTE: this will not resolve path patterns ('*' and '**')
resolve: ['Path/Resolve relative path and expand path variables',
function(path){
path = path || this.path()
// path variables...
// XXX make this more modular...
path = path
// NOTE: these are equivalent to '..' and '.' but not
// identical -- the variables are useful for things
// like moving a page to:
// "Trash/$PATH"
// ...to move the above page out of trash move it to:
// ">>/$PATH"
.replace(/\$PATH|\$\{PATH\}/g, this.path())
.replace(/\$BASE|\$\{BASE\}/g, this.base())
.replace(/\$TITLE|\$\{TITLE\}/g, this.title())
.replace(/\$INDEX|\$\{INDEX\}/g, this.at())
.replace(/\$NOW|\$\{NOW\}/g, Date.now())
path = normalizePath(path)
// relative paths -- "." and ".."
if(path.indexOf('.') >= 0){
path =
// '.' or './*'
path == '.' || /^\.\//.test(path) ?
normalizePath(path.replace(/^\./, this.path()))
// '..' or '../*'
: path == '..' || /^\.\.\//.test(path) ?
normalizePath(path.replace(/^\.\./, this.base()))
: path }
return path }],
// XXX should this get a page???
acquire: ['Path/Acquire the page path that the given path resolves to',
function(path, no_default){
var that = this
// handle paths and relative paths...
var p = this.get(path || this.path())
var title = p.title()
path = path2list(p.base())
var acquire_from = this.config['acquesition-order'] || []
var post_acquire_from = this.config['post-acquesition-order'] || []
var _get = function(path, title, lst){
lst = (lst == null || lst.length == 0) ? [''] : lst
for(var i=0; i < lst.length; i++){
var p = normalizePath(path.concat([lst[i], title]))
if(that.exists(p)){
return that.wiki.data(p) && p } } }
while(true){
// get title from path...
var p = _get(path, title)
// get title from special paths in path...
|| _get(path, title, acquire_from)
if(p != null){
return p }
if(path.length == 0){
break }
path.pop() }
// default paths...
var p = _get(path, title, post_acquire_from)
// system path...
|| this.config['system-path']
&& _get([this.config['system-path']], title)
// NOTE: this may be null...
return p
|| ((!no_default && title != this.config['default-page']) ?
this.acquire('./'+this.config['default-page'])
: null) }],
// XXX pattern does not match anything needs to be handled correctly...
// XXX do we need to normalize 'at'???
path: ['Page/Get or set path',
function(value){
// get explcit path from location (acounting for 'at')...
if(arguments.length == 0){
var location = this.location()
return location.match[location.at]
|| this.config['no-match-page']
|| ''
// move page to path...
} else if(value != null) {
this.wiki.move(this.path(), this.resolve(value))
this.location(value) } }],
title: ['Page/Get or set title',
function(value){
if(arguments.length == 0){
return path2list(this.path()).pop() || ''
} else if(value != null){
this.path(this.base() +'/'+ value) } }],
base: ['Page/Get or set directory',
function(base){
if(arguments.length == 0){
return path2list(this.path()).slice(0, -1).join('/')
} else if(base != null){
this.path(base +'/'+ this.title()) } }],
// Object API...
// NOTE: a clone references the same data and .config, no copying
// is done.
clone: ['Page/Get page clone (new reference)',
function(){
var o = Object.create(this)
.location(JSON.parse(JSON.stringify(this.location())))
o.__parent_context = this
return o }],
end: ['Page/Get parent context of clone',
function(){
return this.__parent_context || this }],
// XXX should this return false on empty path???
copy: ['Page/Copy page to path',
function(path){
return path != null
&& this
.get(path)
// NOTE: this is here mainly to maintain the context stack...
.clone()
.data(this.data()) }],
get: ['Page/Get page by path',
function(path){
return this
.clone()
.location(path) }],
// Order and iteration API...
get length(){
// special case -- non-pattern path...
if(this.location().path.indexOf('*') < 0){
return 1 }
this.refresh()
return this.location().match.length },
at: ['Page/Get index or page at given index',
function(n){
// get current index...
if(n == null){
return this.location().at || 0 }
// get page at index...
var l = this.length
// self...
if(n == this.at()){
return this
// out of bounds...
} else if(n >= l || n < -l){
return null }
var res = this.clone()
n = n < 0 ? l - n : n
// XXX do we min/max n???
n = Math.max(n, 0)
n = Math.min(l-1, n)
res.location(n)
return res }],
prev: ['Page/Get previous page',
function(){
var i = this.at() - 1
// NOTE: need to guard against overflows...
return i >= 0 ?
this.at(i)
: null }],
next: ['Page/Get next page',
function(){
return this.at(this.at() + 1) }],
map: ['Page/',
function(func){
var res = []
for(var i=0; i < this.length; i++){
var page = this.at(i)
res.push(func.call(page, page, i)) }
return res }],
// NOTE: a filter can take a function or a string path pattern...
// NOTE: only absolute path patterns are supported...
filter: ['Page/',
function(func){
// we got a sting pattern...
if(typeof(func) == typeof('str')){
var pattern = path2re(func)
func = function(page){
return pattern.test(page.path()) } }
var res = []
for(var i=0; i < this.length; i++){
var page = this.at(i)
func.call(page, page, i)
&& res.push(page) }
return res }],
each: ['Page/',
function(func){
this.map(func) }],
// XXX reduce???
// Get/set sibling order...
//
// Get order...
// .order()
// -> order
//
// Force get order...
// .order(true)
// .order('force')
// -> order
// NOTE: this will overwrite cache.
//
// Get saved order...
// .order('saved')
// -> order
//
// Save list of paths as order explicitly...
// .order([<title>, .. ])
// -> page
//
// Save order persistently...
// .order('save')
// -> page
//
// Remove set order, local if available else persistent...
// .order('clear')
// -> page
//
// Remove all ordering...
// .order('clear-all')
// -> page
//
//
// List of paths passed to .order(..) can contain a '*' to indicate
// the pages not specified by the list.
// By default all unspecified pages will get appended to the resulting
// list, same as appending a '*' to the tail of the list passed to
// .order(..)
//
//
// NOTE: saving order to data is supported ONLY for paths that contain
// one and only one pattern and in the last path segment...
// NOTE: clearing persistent ordering will remove a page (parent) from
// data if it contains nothing but the order...
// NOTE: this will also maintain page position within order (.at())
//
// NOTE: the actual sorting/ordering is done in .wiki.match(..)
//
// XXX should we also cache the saved sort and order???
// XXX (LEAK?) not sure if the current location where order is stored
// is the right way to go -- would be really hard to clean out...
// ...might be a good idea to clear pattern paths that match no
// pages from data...
order: ['Page/Get or set sibling pages order',
function(order){
var location = this.location()
var path = location.path || ''
var page = (location.match || [])[location.at || 0]
// get order...
if(order == null
|| order == 'force'
|| order === true){
// no patterns in path -> no ordering...
if(path.indexOf('*') < 0){
if(!location.match){
location.match = [ path ]
this.location(location) }
return [ path ] }
// get cached order if not forced...
if(location.match != null && order == null){
return location.match.slice() }
// XXX should we check if this returns a function???
var parent = this.wiki.data(path) || {}
var sort = (location.sort || parent.sort || ['order']).slice()
var i = sort.indexOf('order')
location.order
&& i >= 0
&& sort.splice(i, 1, location.order)
var order = this.wiki.match(path, sort)
// filter out paths containing '*'
.filter(function(p){
return p.indexOf('*') < 0 })
// save cache...
location.match = order
location.at = page ?
order.indexOf(page)
: 0
this.location(location)
return order.slice()
// get saved order...
} else if(order == 'saved'){
return location.order
// XXX should we check if this returns a function???
|| (this.wiki.data(path) || {}).order
|| []
// clear order...
// XXX should this:
// - clear all always
// - explicitly clear only local or persistent
// - progressively clear local then persistent (current)
} else if(order == 'clear' || order == 'clear-all'){
var local = !!location.order
// local order...
delete location.order
// clear persistent order...
if(!local || order == 'clear-all'){
// XXX should we check if this returns a function???
var parent = this.wiki.data(path)
// persistent order...
if(parent && parent.order){
delete parent.order
// remove if empty...
if(Object.keys(parent).length == 0){
this.wiki.remove(path)
// save...
} else {
this.wiki.data(path, parent) } } }
// save order...
} else if(order == 'save'){
// XXX should we check if this returns a function???
var parent = this.wiki.data(path) || {}
var order =
parent.order =
location.order
|| this.order()
this.wiki.data(path, parent)
delete location.order
// set order...
} else {
location.order = order }
// save cache...
this.location(location)
this.order(true) }],
// Sort siblings...
//
// Sort pages via default method
// .sort()
// -> page
//
// Sort pages via method
// .sort(method)
// -> page
//
// Sort pages via method1, then method2, ...
// .sort(method1, method2, ...)
// .sort([method1, method2, ...])
// -> page
// NOTE: the next method is used iff the previous concludes the
// values equal...
//
// To reverse a specific method, prepend it's name with "-", e.g.
// "title" will do the default ascending sort while "-title" will do
// a descending sort.
//
// Supported methods:
// path - compare paths (case-insensitive)
// Path - compare paths (case-sensitive)
// title - compare titles (case-insensitive)
// Title - compare titles (case-sensitive)
// checked - checked state
// order - the set manual order (see .order(..))
// <attribute> - compare data attributes
//
//
// NOTE: the sort is local to the returned object.
// NOTE: the sorted object may loose sync form the actual wiki as the
// list of siblings is cached.
// ...the resulting object is not to be stored for long.
// NOTE: the actual sorting is done by the store...
//
// XXX add 'save' and 'saved' actions...
sort: ['Page/',
function(methods){
var that = this
var res = this.clone()
var location = this.location()
methods = methods instanceof Array ?
methods
: [].slice.call(arguments)
location.sort = methods.length == 0 ?
(this.config['default-sort-methods']
|| ['path'])
: methods
res.location(location)
res.order(true)
return res }],
// XXX should this be persistent???
// ...e.g. survive .order('force') or .order('clear')
reverse: ['Page/',
function(){
var location = this.location()
// reverse the match...
location.match
&& location.match.reverse()