-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
1375 lines (1247 loc) · 37.5 KB
/
Gruntfile.js
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
// Generated on 2013-11-17 using generator-jekyllrb 0.4.1
'use strict';
// Require libraries for the CDL task.
var _ = require('underscore');
var he = require('he');
// Directory reference:
// css: css
// compass: _scss
// javascript: js
// images: img
// fonts: fonts
module.exports = function (grunt) {
// Show elapsed time after tasks run.
require('time-grunt')(grunt);
// Load all Grunt tasks.
require('load-grunt-tasks')(grunt);
grunt.initConfig({
// Configurable paths.
yeoman: {
app: 'app',
dist: 'dist',
brain: 'brain',
template: 'template'
},
watch: {
compass: {
files: ['<%= yeoman.app %>/_scss/**/*.{scss,sass,css}'],
tasks: ['compass:server', 'autoprefixer:server']
},
autoprefixer: {
files: ['<%= yeoman.app %>/css/**/*.css'],
tasks: ['copy:stageCss', 'autoprefixer:server']
},
jekyll: {
files: [
'<%= yeoman.app %>/**/*.index.html',
'_config.yml',
'_config.build.yml',
'!<%= yeoman.app %>/_bower_components',
'<%= yeoman.template %>/**/*'
],
tasks: [
'cdlPrepare',
'convert:json2yaml',
'generate',
'copy:cdl',
'clean:brain',
'jekyll:server'
]
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'.jekyll/**/*.html',
'.tmp/css/**/*.css',
'{.tmp,<%= yeoman.app %>}/<%= js %>/**/*.js',
'<%= yeoman.app %>/img/**/*.{gif,jpg,jpeg,png,svg,webp}'
]
}
},
connect: {
options: {
port: 8000,
livereload: 35735,
// change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
livereload: {
options: {
open: true,
base: [
'.tmp',
'.jekyll',
'<%= yeoman.app %>'
]
}
},
dist: {
options: {
open: true,
base: [
'<%= yeoman.dist %>'
]
}
},
test: {
options: {
base: [
'.tmp',
'.jekyll',
'test',
'<%= yeoman.app %>'
]
}
}
},
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
// Running Jekyll also cleans the target directory. Exclude any
// non-standard `keep_files` here (e.g., the generated files
// directory from Jekyll Picture Tag).
'!<%= yeoman.dist %>/.git*',
'<%= yeoman.app %>/pages',
'<%= yeoman.app %>/data'
]
}]
},
server: [
'.tmp',
'.tmp',
'.jekyll',
'<%= yeoman.app %>/pages',
'<%= yeoman.app %>/data'
],
brain: [
'<%= yeoman.app %>/data/.tmp'
]
},
compass: {
options: {
// If you're using global Sass gems, require them here.
// require: ['singularity', 'jacket'],
bundleExec: true,
sassDir: '<%= yeoman.app %>/_scss',
cssDir: '.tmp/css',
imagesDir: '<%= yeoman.app %>/img',
javascriptsDir: '<%= yeoman.app %>/js',
fontsDir: '<%= yeoman.app %>/fonts',
importPath: '<%= yeoman.app %>/_bower_components',
httpFontsPath: '/fonts',
relativeAssets: false,
httpImagesPath: '/img',
httpGeneratedImagesPath: '/img/generated',
outputStyle: 'expanded',
raw: 'extensions_dir = "<%= yeoman.app %>/_bower_components"\n'
},
dist: {
options: {
generatedImagesDir: '<%= yeoman.dist %>/img/generated'
}
},
server: {
options: {
debugInfo: true,
generatedImagesDir: '.tmp/img/generated'
}
}
},
autoprefixer: {
options: {
browsers: ['last 2 versions']
},
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.dist %>/css',
src: '**/*.css',
dest: '<%= yeoman.dist %>/css'
}]
},
server: {
files: [{
expand: true,
cwd: '.tmp/css',
src: '**/*.css',
dest: '.tmp/css'
}]
}
},
jekyll: {
options: {
bundleExec: true,
config: '_config.yml,_config.build.yml',
src: '<%= yeoman.app %>'
},
dist: {
options: {
dest: '<%= yeoman.dist %>'
}
},
server: {
options: {
config: '_config.yml',
dest: '.jekyll'
}
},
check: {
options: {
doctor: true
}
}
},
// UseminPrepare will only scan a single page for usemin blocks. If you
// use usemin blocks that aren't in index.html, create a usemin manifest
// page (hackery!) and point this task there.
useminPrepare: {
options: {
dest: '<%= yeoman.dist %>'
},
html: '<%= yeoman.dist %>/index.html'
},
usemin: {
options: {
basedir: '<%= yeoman.dist %>',
dirs: ['<%= yeoman.dist %>/**/*']
},
html: ['<%= yeoman.dist %>/**/*.html'],
css: ['<%= yeoman.dist %>/css/**/*.css']
},
htmlmin: {
dist: {
options: {
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true
},
files: [{
expand: true,
cwd: '<%= yeoman.dist %>',
src: '**/*.html',
dest: '<%= yeoman.dist %>'
}]
}
},
// Usemin adds files to concat
concat: {},
// Usemin adds files to uglify
uglify: {},
// Usemin adds files to cssmin
cssmin: {
dist: {
options: {
check: 'gzip'
}
}
},
imagemin: {
dist: {
options: {
progressive: true
},
files: [{
expand: true,
cwd: '<%= yeoman.dist %>',
src: '**/*.{jpg,jpeg,png}',
dest: '<%= yeoman.dist %>'
}]
}
},
svgmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.dist %>',
src: '**/*.svg',
dest: '<%= yeoman.dist %>'
}]
}
},
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
src: [
// Jekyll processes and moves HTML and text files
// Usemin moves CSS and javascript inside of Usemin blocks
// Copy moves asset files and directories
'images/**/*',
'js/**/*',
'fonts/**/*',
// Like Jekyll, exclude files & folders prefixed with an underscore
'!**/_*{,/**}',
// Explicitly add any files your site needs for distribution here
//'_bower_components/jquery/jquery.js',
//'favicon.ico',
//'apple-touch*.png'
'data/**/*',
'pages/**/*.{jpg,JPG,PNG,png,gif,jpeg,webp,tiff,mp3,wav,avi,mp4}'
],
dest: '<%= yeoman.dist %>'
},
// Copy the file that define domains/subdomains redirected to the gh-pages site.
{
expand: true,
cwd: '<%= yeoman.app %>',
src: 'CNAME',
dest: '<%= yeoman.dist %>'
}]
},
// Copy CSS into .tmp directory for Autoprefixer processing
stageCss: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>/css',
src: '**/*.css',
dest: '.tmp/css'
}]
},
cdl: {
files: [{
expand: true,
dot: true,
cwd: 'brain/files',
src: '**/*.{jpg,JPG,png,PNG,gif,jpeg,webp,tiff,mp3,wav,avi,mp4}',
dest: '<%= yeoman.app %>/pages'
}]
}
},
rev: {
options: {
length: 4
},
dist: {
files: {
src: [
'<%= yeoman.dist %>/js/**/*.js',
'<%= yeoman.dist %>/css/**/*.css',
'<%= yeoman.dist %>/img/**/*.{gif,jpg,jpeg,png,svg,webp}',
'<%= yeoman.dist %>/fonts/**/*.{eot*,otf,svg,ttf,woff}'
]
}
}
},
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: [
'Gruntfile.js',
'<%= yeoman.app %>/js/**/*.js',
'test/spec/**/*.js',
'!<%= yeoman.app %>/js/vendor/**/*'
]
},
csscss: {
options: {
bundleExec: true,
minMatch: 2,
ignoreSassMixins: false,
compass: true,
colorize: true,
shorthand: false,
verbose: true
},
check: {
src: ['<%= yeoman.app %>/css/**/*.css',
'<%= yeoman.app %>/_scss/**/*.scss']
}
},
csslint: {
options: {
csslintrc: '.csslintrc'
},
check: {
src: [
'<%= yeoman.app %>}/css/**/*.css',
'<%= yeoman.app %>}/_scss/**/*.scss'
]
}
},
concurrent: {
server: [
'compass:server',
'copy:stageCss',
'jekyll:server'
],
dist: [
'compass:dist',
'copy:dist'
]
},
convert: {
xml2json: {
options: {
explicitArray: false
},
files: [
{
expand: true,
cwd: '<%= yeoman.brain %>/',
src: ['**/*.xml'],
dest: '<%= yeoman.app %>/data/',
ext: '.json'
}
]
},
json2yaml: {
files: [
{
expand: true,
cwd: '<%= yeoman.app %>/data/.tmp/',
src: ['**/*.json'],
dest: '<%= yeoman.app %>/data/.tmp/',
ext: '.yml'
}
]
}
},
'gh-pages': {
options: {
base: 'dist'
},
src: ['**']
},
replace: {
dist: {
src: [
'<%= yeoman.dist %>/**/index.html',
'<%= yeoman.dist %>/css/*.css'
],
overwrite: true,
replacements: [{
from: '<link rel="stylesheet" href="css/',
to: '<link rel="stylesheet" href="//gizra.github.io/CDL/css/'
},
{
from: '<script src="js/',
to: '<script src="//gizra.github.io/CDL/js/'
},
{
from: '<script src="/js/',
to: '<script src="//gizra.github.io/CDL/js/'
},
{
from: 'url(/fonts/',
to: 'url(http://gizra.github.io/CDL/fonts/'
},
{
from: 'url(/images/',
to: 'url(http://gizra.github.io/CDL/images/'
}]
}
},
'CDL': {
src: '<%= yeoman.app %>/data/brain.json',
dest: '<%= yeoman.app %>/data/tree.json',
tmp: '<%= yeoman.app %>/data/.tmp/'
},
'generate': {
src: '<%= yeoman.app %>/data/tree.json',
dest: '<%= yeoman.app %>/pages/',
template: 'pageFull'
}
});
grunt.registerTask('CDL', function() {
var src = {},
dest = {};
// Data Information.
var nodesIndexed = [],
nodes = {},
nodesLinks = [],
nodesEntries = [],
nodesAttachments = [],
firstNode,
tree = {},
brain;
src.path = grunt.config.get('CDL.src');
dest.path = grunt.config.get('CDL.dest');
/**
* Prepare collection of nodes indexed, that will use to parse and generate the tree.
*/
function prepareData() {
/**
* Create an object to filter the data from brain.json file and store in a work
* variables.
*
* The filtering of the nodes it does by identify the "forgotten nodes" and separete this
* separate from the rest "valid nodes". Example of a Thought tag element:
*
* <Thought>
* <guid>723DF418-A329-4636-45E0-B0357D423F7B</guid>
* <name>:1: The Wanderings of the Children of Israel in the desert</name>
* <label></label>
* <creationDateTime>2014-01-07 12:00:05.264 @+0200</creationDateTime>
* <realModificationDateTime>2014-01-09 12:49:19.623 @+0200</realModificationDateTime>
* <displayModificationDateTime>2014-01-09 12:48:51.711 @+0200</displayModificationDateTime>
* <forgottenDateTime>2014-01-09 12:49:19.623 @+0200</forgottenDateTime>
* <activationDateTime>2014-01-09 12:49:14.493 @+0200</activationDateTime>
* <linksModificationDateTime>2013-11-19 13:28:32.242 @+0200</linksModificationDateTime>
* <isType>0</isType>
* <color>0</color>
* <accessControlType>0</accessControlType>
* </Thought>
*
*
* @constructor
* @param brainData
* Json object with the contain brain data, exporter from the brain xml file.
*
* brainData {
* Attachments: {*},
* AttributesDatas: '',
* Attributes: {*},
* Entries: {*},
* Links: {*},
* Source: {*},
* Thoughts: {*}
* }
*
*/
function Data(brainData) {
var self = this;
this.data = brainData;
this.nodes = {};
this.links = {};
this.entries = {};
this.attachments = {};
// Filter forgotten nodes. (Definition in the beginning of the function)
this.filterNodes = function() {
self.nodes.valid = _.filter(self.data.Thoughts.Thought, function(node){
return typeof node.forgottenDateTime === 'undefined';
});
self.nodes.forgotten = _.filter(self.data.Thoughts.Thought, function(node){
return typeof node.forgottenDateTime !== 'undefined';
});
};
// Filter links of forgotten nodes.
this.filterLinks = function(result) {
var properties = ['idB', 'idA'];
_.each(self.nodes.forgotten, function(node) {
properties.forEach(function(property) {
result = _.filter(result, function(link) {
return link[property] !== node.guid;
});
});
});
return result;
};
// Filter entries of forgotten nodes.
this.filterEntries = function(result) {
_.each(self.nodes.forgotten, function(node) {
result = _.filter(result, function(entry) {
return entry.EntryObjects.EntryObject.objectID !== node.guid;
});
});
return result;
};
// Filter Attachments of forgotten nodes.
this.filterAttachments = function(result) {
_.each(self.nodes.forgotten, function(node) {
result = _.filter(result, function(attachment) {
return attachment.objectID !== node.guid;
});
});
return result;
};
return {
/**
* Return the guid of the root node.
*
* @returns {*}
*/
getFirstNode: function() {
return self.data.Source.homeThoughtGuid;
},
/**
* Return and array of node objects "valid", without the forgotten nodes.
*
* @returns [{*}]
* Array of node objects.
*/
getValidNodes: function(){
// Filter nodes store into properties.
self.filterNodes();
return self.nodes.valid;
},
/**
* Return and array of a links objects.
*
* @returns {*}
*/
getLinks: function(){
self.links = self.filterLinks(self.data.Links.Link);
return self.links;
},
/**
* Return and array of a entries objects.
*
* @returns {*}
*/
getEntries: function() {
self.entries = self.filterEntries(self.data.Entries.Entry);
return self.entries;
},
/**
* Return and array of attachments objects.
*
* @returns {*}
*/
getAttachments: function() {
self.attachments = self.filterAttachments(self.data.Attachments.Attachment);
return self.attachments;
}
};
}
// Create object to do pre-filtering with the data.
brain = new Data(src.data.BrainData);
firstNode = brain.getFirstNode();
// Prepare raw data in thoughts, links, entries and attachments and "guid" of the root node.
nodes = brain.getValidNodes();
nodesLinks = brain.getLinks();
nodesEntries = brain.getEntries();
nodesAttachments = brain.getAttachments();
// Remove properties not necessary data from Node and links.
_.each(nodes, function(thought, index) {
nodes[index] = _.pick(thought, 'guid', 'name');
});
_.each(nodesLinks, function(link, index) {
nodesLinks[index] = _.pick(link, 'guid', 'idA', 'idB', 'dir', 'linkTypeID', 'isBackward');
});
_.each(nodesEntries, function(entry, index) {
nodesEntries[index] = _.pick(entry, 'guid', 'EntryObjects', 'body', 'format');
});
_.each(nodesAttachments, function(item, index) {
nodesAttachments[index] = _.omit(item, 'creationDateTime', 'modificationDateTime');
});
// Index the nodes of thought by guid.
nodesIndexed = _.indexBy(nodes, 'guid');
// Index entries.
_.each(nodesEntries, function(entry) {
entry.objectID = entry.EntryObjects.EntryObject.objectID;
});
nodesEntries = _.indexBy(nodesEntries, 'objectID');
// Index attachments.
nodesAttachments = _.groupBy(nodesAttachments, function(item) {
return item.objectID;
});
// Prepare the root of the tree.
nodes = _.without(nodes, nodesIndexed[firstNode]);
nodes.unshift(nodesIndexed[firstNode]);
// Refresh nodesIndexed.
nodesIndexed = _.indexBy(nodes, 'guid');
}
/**
* Get first node of the array order by node.chronologicaltId.
*
* @param nodes
* @returns {*}
*/
function getFirst(nodes) {
return _.min(nodes, function(node) {
return parseInt(node.chronologicalId, 10);
});
}
/**
* From an array of nodes node.type: 'chronological', Reorder these in order chronological. Where the children nodes are
* the next event in order, this repeat for each node.
*
* param nodes
* @returns {*}
*/
function reorderChronological(nodes) {
var node;
// Get the first node and an array without the node.
node = getFirst(nodes);
nodes = _.without(nodes, node);
if (nodes.length > 0) {
node.children = _.union(node.children, [reorderChronological(nodes)]);
}
return node;
}
/**
* Set the content data and properties according the type of node.
*
* @param node
*/
function setNodeContent(node) {
var regexChronological = /:\d+:/,
regexBastard = /:_:/,
regexId = /:/,
regexContent = /(<body>)((\n.*)*)(<\/body>)/,
regexCleaner,
rawName;
// Init regex for content.
regexCleaner = [
{
regex: / ?class=".*?"+/,
phrase: ''
},
{
regex: / ?style=".*?"+/,
phrase: ''
},
{
regex: / ?align=".*?"+/,
phrase: ''
},
{
regex: /^\n+/,
phrase: ''
},
{
regex: /$\n+/,
phrase: ''
},
{
regex: /<font .*?>/,
phrase: ''
},
{
regex: /<\/font>/,
phrase: ''
}
];
/**
* Clean the content extracted with an array of regular expressions and phrases.
*
* @param data
* @param regex
* @param phrase
*
* @returns {string}
*/
function cleanContent(data, regex, phrase) {
data = data.replace(regex, phrase);
if (data.match(regex)) {
data = cleanContent(data, regex, phrase);
}
return data;
}
// Add "node.name" to escape HTML entities in YAML.
rawName = he.decode(node.name);
node.name = rawName;
// Add entry content to the node.
node.data = '';
if (nodesEntries[node.guid] && nodesEntries[node.guid].body) {
// Extract the content from body.
if (regexContent.test(nodesEntries[node.guid].body)) {
node.data = he.decode(nodesEntries[node.guid].body.match(regexContent)[2]);
}
else {
node.data = he.decode(nodesEntries[node.guid].body);
}
// Clean content.
regexCleaner.forEach(function(element) {
node.data = cleanContent(node.data, element.regex, element.phrase);
});
}
// Add attachments.
node.attachments = [];
if (nodesAttachments[node.guid]) {
node.attachments = nodesAttachments[node.guid];
}
// Categorize child.
if (node.name.match(regexChronological)) {
// Chronological childs.
node.type = 'chronological';
node.chronologicalId = rawName.split(regexId)[1];
node.chronologicalName = rawName.split(regexChronological)[1].trim();
}
else if (node.name.match(regexBastard)) {
// Bastard childs.
node.type = 'bastard';
node.bastardName = rawName.split(regexBastard)[1].trim();
}
else {
node.type = 'default';
}
}
// @todo: Function repeted in grunt task CDLPrepare, create one function in a multitask grunt task for
// the tasks CDL, generate and CDLPrepare.
/**
* Helper to select the correct name of the node according its type.
*
* @param node
* @returns {*}
*/
function getName(node) {
return (node.type === 'chronological') ? node.chronologicalName : (node.type === 'bastard') ? node.bastardName : node.name;
}
/**
* From an array of nodes links we get the information of each sibling node.
*
* @param childs
* @returns {*}
*/
function setSiblingsInfo(childs) {
var key,
siblings = [];
_.each(childs, function(child) {
if (child.dir === '1') {
key = child.idB;
}
else if (child.dir === '2') {
key = child.idA;
}
siblings.push(_.pick(nodesIndexed[key], 'guid', 'name'));
});
return siblings;
}
/**
* Reorder the childs properties according dir: (Brain direction), type: chronological, bastard and default.
* Return a collection of childs categorized and organized.
*
* @param childs
* @param parent
* @returns {*}
*/
function parseChilds(childs, parent, depth) {
var childsOrdered = [],
chronologicalChilds;
// Parse child.
_.each(childs, function(child, index, childs) {
// Set node information of child node.
child.node = {};
child.node.children = [];
if (child.dir === '1') {
child.node = nodesIndexed[child.idB];
}
else if (child.dir === '2') {
child.node = nodesIndexed[child.idA];
}
// Parse the content and set the classification of the node.
setNodeContent(child.node);
// Add depth.
if (typeof depth === 'undefined') {
child.node._depth = 1;
}
else {
child.node._depth = depth+1;
}
// Add parent information.
if (parent.guid !== firstNode && child.node._depth > 2) {
child.node.parent = _.pick(parent, 'guid');
child.node.parent.name = getName(parent);
}
// Set parent guid.
child.node.hasChronologicalChildren = false;
// Look up for more generations of childrens.
child.node.children = getChilds(child.node, child.node._depth);
// If a chronological node have grandchildSet the granchild.parent like the father parent.
if (child.node.type === 'chronological' && child.node.children.length > 0) {
_.each(child.node.children, function(grandchild){
grandchild.parent = child.node.parent;
});
}
// Check if have chronological children if the node is not chronological, and set it.
if (child.node.type !== 'chronological' && _.where(child.node.children, {type: 'chronological'}).length) {
child.node.hasChronologicalChildren = true;
}
// Get the siblings nodes.
child.node.brothers = [];
if (child.node.type !== 'chronological' && !child.node.hasChronologicalChildren && child.node._depth > 1) {
child.node.brothers = _.filter(setSiblingsInfo(childs), function(brother) {
// Clean the name property.
var label = undefined;
brother._name = brother.name;
brother.name = he.decode(brother.name);
brother.result = brother.name.match(/^:\d+: (.*)|^:_: (.*)/);
if (brother.result) {
brother._result = [];
brother.result.forEach(function(item) {
if (typeof item !== 'undefined') {
brother._result.push(item);
}
});
brother._result = new Array(brother._result);
label = brother._result.pop();
}
brother.name = (label) ? label.pop() : brother.name;
return brother.guid !== child.node.guid && !brother._name.match(/:\d+:/);
});
}
childsOrdered.push(child.node);
});
// Select chronological nodes from children.
chronologicalChilds = _.where(childsOrdered, {type: 'chronological'});
if (chronologicalChilds.length) {
// Reorder chronological childs.
childsOrdered = _.union(_.difference(childsOrdered, chronologicalChilds), reorderChronological(chronologicalChilds));
}
// Join childs classified.
return childsOrdered;
}
/**
* Get childs of the node.
*
* @param node
* @param depth
* @returns {*}
*/
function getChilds(node, depth) {
var childsOrdered = [],
childs = [];
grunt.log.ok('Parsing node: ' + node.guid);
childs = _.union( _.where(nodesLinks, {idA: node.guid, dir: '1'}), _.where(nodesLinks, {idB: node.guid, dir: '2'}) );
if (childs.length) {
childsOrdered = parseChilds(childs, node, depth);
}
return childsOrdered;
}
/**
* Parse JSON to structure d3 layout.
* Create json d3 tree layout structure.
* https://github.com/mbostock/d3/wiki/Tree-Layout
*
* @returns {*}
*/
function generateTreeData() {
prepareData();
grunt.log.ok('Working object prepared and indexed.');
// Look up the node and position into the array.
tree = nodesIndexed[firstNode];
tree.children = [];
tree.children = getChilds(tree);
return tree;
}
// Prepare JSON.
if (grunt.file.exists(src.path)) {
src.data = grunt.file.readJSON(src.path);
grunt.log.ok('Loaded source file: ' + src.path);