|
37 | 37 | return file.path.slice(-5) !== '.meta'; |
38 | 38 | } |
39 | 39 |
|
| 40 | + function metaFile(file) { |
| 41 | + return file.path.slice(-5) === '.meta'; |
| 42 | + } |
| 43 | + |
40 | 44 | function handleErrorFor(file) { |
41 | 45 | return function markFileWithError(error) { |
42 | 46 | file.error = error; |
|
144 | 148 | separated[entry.type || 'file'].push(entry); |
145 | 149 | }); |
146 | 150 |
|
147 | | - var files = separated.file.filter(notMetaFile).map(function (file) { |
| 151 | + var createRamlFile = function (file) { |
148 | 152 | return new RamlFile(file.path, file.contents, { dirty: false, persisted: true, root: file.root} ); |
149 | | - }); |
| 153 | + }; |
| 154 | + |
| 155 | + var files = separated.file.filter(notMetaFile).map(createRamlFile); |
| 156 | + |
| 157 | + var metaFiles = separated.file.filter(metaFile).map(createRamlFile); |
150 | 158 |
|
151 | 159 | var directories = separated.folder.map(function (directory) { |
152 | 160 | return new RamlDirectory(directory.path, directory.meta, directory.children); |
153 | 161 | }); |
154 | 162 |
|
155 | 163 | this.children = directories.concat(files).sort(sortingFunction); |
| 164 | + this.metaChildren = directories.concat(metaFiles).sort(sortingFunction); |
156 | 165 | } |
157 | 166 |
|
158 | 167 | RamlDirectory.prototype.getDirectories = function getDirectories() { |
|
163 | 172 | return this.children.filter(function(t) { return !t.isDirectory; }); |
164 | 173 | }; |
165 | 174 |
|
| 175 | + RamlDirectory.prototype.getMetaFiles = function getMetaFiles() { |
| 176 | + return this.metaChildren.filter(function(t) { return !t.isDirectory; }); |
| 177 | + }; |
| 178 | + |
166 | 179 | RamlDirectory.prototype.forEachChildDo = function forEachChildDo(action) { |
167 | 180 | // BFS |
168 | 181 | var queue = this.children.slice(); |
|
179 | 192 | } |
180 | 193 | }; |
181 | 194 |
|
| 195 | + RamlDirectory.prototype.forEachMetaChildDo = function forEachMetaChildDo(action) { |
| 196 | + // BFS |
| 197 | + var queue = this.metaChildren.slice(); |
| 198 | + var current; |
| 199 | + |
| 200 | + while (queue.length > 0) { |
| 201 | + current = queue.shift(); |
| 202 | + |
| 203 | + if (current.isDirectory) { |
| 204 | + queue = queue.concat(current.metaChildren); |
| 205 | + } |
| 206 | + |
| 207 | + action.call(current, current); |
| 208 | + } |
| 209 | + }; |
| 210 | + |
182 | 211 | RamlDirectory.prototype.sortChildren = function sortChildren() { |
183 | 212 | this.children.sort(sortingFunction); |
184 | 213 | }; |
|
249 | 278 | var promises = []; |
250 | 279 | directory.getDirectories().forEach(function(dir) { promises.push(service.removeDirectory(dir)); }); |
251 | 280 | directory.getFiles().forEach(function(file) { promises.push(service.removeFile(file)); }); |
| 281 | + directory.getMetaFiles().forEach(function(file) { promises.push(service.removeFile(file)); }); |
252 | 282 |
|
253 | 283 | // remove this directory object from parent's children list |
254 | 284 | var parent = service.getParent(directory); |
|
353 | 383 | .then(modifyFile, handleErrorFor(file)) |
354 | 384 | .then(function () { |
355 | 385 | // remove the file object from the parent's children list |
356 | | - var index = parent.children.indexOf(file); |
| 386 | + if (notMetaFile(file)) { |
| 387 | + var index = parent.children.indexOf(file); |
357 | 388 |
|
358 | | - if (index !== -1) { |
359 | | - parent.children.splice(index, 1); |
| 389 | + if (index !== -1) { |
| 390 | + parent.children.splice(index, 1); |
| 391 | + } |
| 392 | + } else { |
| 393 | + var metaIndex = parent.metaChildren.indexOf(file); |
| 394 | + |
| 395 | + if (metaIndex !== -1) { |
| 396 | + parent.metaChildren.splice(metaIndex, 1); |
| 397 | + } |
360 | 398 | } |
361 | 399 |
|
362 | 400 | $rootScope.$broadcast('event:raml-editor-file-removed', file); |
|
458 | 496 | target.forEachChildDo(function(c) { |
459 | 497 | c.path = c.path.replace(target.path, newPath); |
460 | 498 | }); |
| 499 | + |
| 500 | + // renames the path of each meta child under the current directory |
| 501 | + target.forEachMetaChildDo(function(c) { |
| 502 | + c.path = c.path.replace(target.path, newPath); |
| 503 | + }); |
461 | 504 | } else { |
| 505 | + service.moveMeta(target, destination); |
462 | 506 | promise = target.persisted ? fileSystem.rename(target.path, newPath) : $q.when(target); |
463 | 507 | } |
464 | 508 |
|
|
475 | 519 | var metaFile = new RamlFile(file.path + '.meta', JSON.stringify(meta)); |
476 | 520 | return service.saveFile(metaFile) |
477 | 521 | .then(function () { |
| 522 | + var parent = service.getParent(metaFile); |
| 523 | + parent.metaChildren.push(metaFile); |
478 | 524 | return meta; |
479 | 525 | }) |
480 | 526 | ; |
|
493 | 539 | ); |
494 | 540 | }; |
495 | 541 |
|
| 542 | + service.moveMeta = function moveMeta(file, destination) { |
| 543 | + var metaName = file.name + '.meta'; |
| 544 | + var newMetaPath = service.join(destination.path, metaName); |
| 545 | + var metaPathName = file.path + '.meta'; |
| 546 | + var oldParent = service.getParent(file); |
| 547 | + |
| 548 | + var metaFile = oldParent.metaChildren.find(function(meta) { |
| 549 | + return meta.path === metaPathName; |
| 550 | + }); |
| 551 | + |
| 552 | + if (metaFile) { |
| 553 | + return fileSystem.rename(metaFile.path, newMetaPath).then( |
| 554 | + function success() { |
| 555 | + //Remove old parent meta data |
| 556 | + var index = oldParent.metaChildren.indexOf(metaFile); |
| 557 | + if (index !== -1) { |
| 558 | + oldParent.metaChildren.splice(index, 1); |
| 559 | + } |
| 560 | + metaFile.path = newMetaPath; |
| 561 | + destination.metaChildren.push(metaFile); |
| 562 | + return metaFile; |
| 563 | + }, |
| 564 | + function failure() { |
| 565 | + return metaFile; |
| 566 | + } |
| 567 | + ); |
| 568 | + } |
| 569 | + }; |
| 570 | + |
496 | 571 | service.join = function () { |
497 | 572 | return Array.prototype.reduce.call(arguments, function (path, segment) { |
498 | 573 | if (segment == null) { |
|
0 commit comments