|
| 1 | +var DocMapInfo = function(docMap, getCurrent) { |
| 2 | + this.docMap = docMap; |
| 3 | + this.childrenMap = makeChildrenMap(docMap); |
| 4 | + this.getCurrent = getCurrent; |
| 5 | +}; |
| 6 | +DocMapInfo.prototype.isCurrent = function(docObject){ |
| 7 | + return docObject.name === this.getCurrent().name; |
| 8 | +}; |
| 9 | +DocMapInfo.prototype.hasCurrent = function(docObject){ |
| 10 | + var parents = this.getParents(this.getCurrent()); |
| 11 | + parents.push(this.getCurrent()); |
| 12 | + var itemMap = {}; |
| 13 | + parents.forEach(function(docObject){ |
| 14 | + itemMap[docObject.name] = true; |
| 15 | + }); |
| 16 | + |
| 17 | + return itemMap[docObject.name]; |
| 18 | +}; |
| 19 | +DocMapInfo.prototype.hasOrIsCurrent = function(docObject){ |
| 20 | + return this.isCurrent(docObject) || this.hasCurrent(docObject); |
| 21 | +}; |
| 22 | +DocMapInfo.prototype.getParents = function(docObject, cb){ |
| 23 | + var names = {}; |
| 24 | + |
| 25 | + // walk up parents until you don't have a parent |
| 26 | + var parent = this.docMap[docObject.parent], |
| 27 | + parents = []; |
| 28 | + if(!parent) { |
| 29 | + return []; |
| 30 | + } |
| 31 | + // don't allow things that are their own parent |
| 32 | + if(parent.parent === docObject.name){ |
| 33 | + return parents; |
| 34 | + } |
| 35 | + |
| 36 | + while(parent){ |
| 37 | + if(cb) { |
| 38 | + cb(parent); |
| 39 | + } |
| 40 | + parents.unshift(parent); |
| 41 | + if(names[parent.name]){ |
| 42 | + return parents; |
| 43 | + } |
| 44 | + names[parent.name] = true; |
| 45 | + parent = this.docMap[parent.parent]; |
| 46 | + } |
| 47 | + return parents; |
| 48 | +}; |
| 49 | +DocMapInfo.prototype.getTitle = function(docObject) { |
| 50 | + return docObject.title || docObject.name; |
| 51 | +}; |
| 52 | + |
| 53 | +function getShortTitle(name, parent){ |
| 54 | + if(parent && (parent.type === "module" || parent.type === "group")) { |
| 55 | + var modeletName = path.dirname(parent.name), |
| 56 | + moduleName = path.basename(parent.name); |
| 57 | + |
| 58 | + if(name.indexOf( parent.name+"/" ) === 0 ) { |
| 59 | + name = name.replace(parent.name+"/", "./"); |
| 60 | + } |
| 61 | + // can-util/dom/events/attributes/attributes's parent is can-util/dom/events/events |
| 62 | + else if( moduleName && modeletName.endsWith(moduleName) && name.indexOf( modeletName+"/" ) === 0 ) { |
| 63 | + name = name.replace(modeletName+"/", "./"); |
| 64 | + } |
| 65 | + else { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + var basename = path.basename(name); |
| 70 | + if(name.endsWith("/"+basename+"/"+basename)) { |
| 71 | + return path.dirname(name)+"/"; |
| 72 | + } else { |
| 73 | + return name; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +DocMapInfo.prototype.getShortTitle = function(docObject) { |
| 78 | + |
| 79 | + if(docObject.type === "module") { |
| 80 | + var parents = this.getParents(docObject).reverse(); |
| 81 | + |
| 82 | + var parentModule = parents.find(function(docObject){ |
| 83 | + return docObject.type === "module"; |
| 84 | + }); |
| 85 | + var parentGroup = parents[0] && parents[0].type === "group" && parents[0]; |
| 86 | + |
| 87 | + var name = docObject.name, |
| 88 | + shortTitle; |
| 89 | + |
| 90 | + if(parentGroup) { |
| 91 | + shortTitle = getShortTitle(name, parentGroup); |
| 92 | + if(shortTitle) { |
| 93 | + return shortTitle; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if(parentModule) { |
| 98 | + shortTitle = getShortTitle(name, parentModule); |
| 99 | + if(shortTitle) { |
| 100 | + return shortTitle; |
| 101 | + } |
| 102 | + } |
| 103 | + return name; |
| 104 | + } else { |
| 105 | + return this.getTitle(docObject); |
| 106 | + } |
| 107 | + |
| 108 | +}; |
| 109 | +DocMapInfo.prototype.isGroup = function(docObject) { |
| 110 | + return ["group","static","prototype"].indexOf(docObject.type) !== -1; |
| 111 | +}; |
| 112 | +DocMapInfo.prototype.getCurrentTree = function(){ |
| 113 | + // [{docObject, children<>},{docObject}] |
| 114 | + // |
| 115 | + var getChildren = this.getChildren.bind(this), |
| 116 | + getNestedDocObject = this.getNestedDocObject.bind(this); |
| 117 | + |
| 118 | + var cur = this.getCurrent(); |
| 119 | + |
| 120 | + var curChildren = this.getNestedChildren(cur); |
| 121 | + |
| 122 | + this.getParents(cur, function(docObject){ |
| 123 | + curChildren = getChildren(docObject).map(function(docObject){ |
| 124 | + if(docObject.name === cur.name) { |
| 125 | + return {docObject: docObject, children: curChildren}; |
| 126 | + } else { |
| 127 | + return getNestedDocObject(docObject); |
| 128 | + } |
| 129 | + }); |
| 130 | + cur = docObject; |
| 131 | + }); |
| 132 | + |
| 133 | + if(!curChildren) { |
| 134 | + return {children: []}; |
| 135 | + } else { |
| 136 | + return {children: curChildren}; |
| 137 | + } |
| 138 | +}; |
| 139 | +DocMapInfo.prototype.getChildren = function(docObject){ |
| 140 | + var children = this.childrenMap[docObject.name]; |
| 141 | + return (children || []).sort(compareDocObjects); |
| 142 | +}; |
| 143 | +DocMapInfo.prototype.getNestedDocObject = function(docObject){ |
| 144 | + if(this.isGroup(docObject)) { |
| 145 | + return { |
| 146 | + docObject: docObject, |
| 147 | + children: this.getNestedChildren(docObject) |
| 148 | + }; |
| 149 | + } else { |
| 150 | + return {docObject: docObject}; |
| 151 | + } |
| 152 | +}; |
| 153 | +DocMapInfo.prototype.getNestedChildren = function(docObject){ |
| 154 | + return this.getChildren(docObject).map(this.getNestedDocObject.bind(this)); |
| 155 | +}; |
| 156 | + |
| 157 | +var levelMap = ["collection","modules"]; |
| 158 | + |
| 159 | +function makeChildrenMap(docMap){ |
| 160 | + var childrenMap = {}; |
| 161 | + for(var name in docMap) { |
| 162 | + var docObject = docMap[name]; |
| 163 | + var parent = docObject.parent; |
| 164 | + if(parent) { |
| 165 | + if(!childrenMap[parent]) { |
| 166 | + childrenMap[parent] = []; |
| 167 | + } |
| 168 | + childrenMap[parent].push(docObject); |
| 169 | + } |
| 170 | + } |
| 171 | + return childrenMap; |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +var compareDocObjects = function(child1, child2){ |
| 176 | + |
| 177 | + // put groups at the end |
| 178 | + if(/group|prototype|static/i.test(child1.type)){ |
| 179 | + if(!/group|prototype|static/i.test(child2.type)){ |
| 180 | + return 1; |
| 181 | + } else { |
| 182 | + if(child1.type === "prototype"){ |
| 183 | + return -1; |
| 184 | + } |
| 185 | + if(child2.type === "prototype"){ |
| 186 | + return 1; |
| 187 | + } |
| 188 | + if(child1.type === "static"){ |
| 189 | + return -1; |
| 190 | + } |
| 191 | + if(child2.type === "static"){ |
| 192 | + return 1; |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + } |
| 197 | + if(/prototype|static/i.test(child2.type)){ |
| 198 | + return -1; |
| 199 | + } |
| 200 | + |
| 201 | + if(typeof child1.order == "number"){ |
| 202 | + if(typeof child2.order == "number"){ |
| 203 | + // same order given? |
| 204 | + if(child1.order == child2.order){ |
| 205 | + // sort by name |
| 206 | + if(child1.name < child2.name){ |
| 207 | + return -1; |
| 208 | + } |
| 209 | + return 1; |
| 210 | + } else { |
| 211 | + return child1.order - child2.order; |
| 212 | + } |
| 213 | + |
| 214 | + } else { |
| 215 | + return -1; |
| 216 | + } |
| 217 | + } else { |
| 218 | + if(typeof child2.order == "number"){ |
| 219 | + return 1; |
| 220 | + } else { |
| 221 | + // alphabetical |
| 222 | + if(child1.name < child2.name){ |
| 223 | + return -1; |
| 224 | + } |
| 225 | + return 1; |
| 226 | + } |
| 227 | + } |
| 228 | +}; |
| 229 | + |
| 230 | +module.exports = DocMapInfo; |
0 commit comments