|
37 | 37 | return file.path.slice(-5) !== '.meta';
|
38 | 38 | }
|
39 | 39 |
|
| 40 | + function metaFile(file) { |
| 41 | + return !notMetaFile(file); |
| 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 |
|
166 |
| - RamlDirectory.prototype.forEachChildDo = function forEachChildDo(action) { |
| 175 | + RamlDirectory.prototype.getMetaFiles = function getMetaFiles() { |
| 176 | + return this.metaChildren.filter(function(t) { return !t.isDirectory; }); |
| 177 | + }; |
| 178 | + |
| 179 | + RamlDirectory.prototype.forEachItemDo = function forEachItemDo(action, isMetaChildren) { |
167 | 180 | // BFS
|
168 |
| - var queue = this.children.slice(); |
| 181 | + var queue = isMetaChildren ? this.metaChildren.slice() : this.children.slice(); |
169 | 182 | var current;
|
170 | 183 |
|
171 | 184 | while (queue.length > 0) {
|
172 | 185 | current = queue.shift();
|
173 | 186 |
|
174 | 187 | if (current.isDirectory) {
|
175 |
| - queue = queue.concat(current.children); |
| 188 | + queue = queue.concat((isMetaChildren) ? current.metaChildren : current.children); |
176 | 189 | }
|
177 | 190 |
|
178 | 191 | action.call(current, current);
|
179 | 192 | }
|
180 | 193 | };
|
181 | 194 |
|
| 195 | + RamlDirectory.prototype.forEachChildDo = function forEachChildDo(action) { |
| 196 | + this.forEachItemDo(action, false); |
| 197 | + }; |
| 198 | + |
| 199 | + RamlDirectory.prototype.forEachMetaChildDo = function forEachChildDo(action) { |
| 200 | + this.forEachItemDo(action, true); |
| 201 | + }; |
| 202 | + |
182 | 203 | RamlDirectory.prototype.sortChildren = function sortChildren() {
|
183 | 204 | this.children.sort(sortingFunction);
|
184 | 205 | };
|
|
248 | 269 | // and collect all promises into an array
|
249 | 270 | var promises = [];
|
250 | 271 | directory.getDirectories().forEach(function(dir) { promises.push(service.removeDirectory(dir)); });
|
251 |
| - directory.getFiles().forEach(function(file) { promises.push(service.removeFile(file)); }); |
| 272 | + directory.getFiles().concat(directory.getMetaFiles()) |
| 273 | + .forEach(function(file) { promises.push(service.removeFile(file)); }); |
252 | 274 |
|
253 | 275 | // remove this directory object from parent's children list
|
254 | 276 | var parent = service.getParent(directory);
|
|
353 | 375 | .then(modifyFile, handleErrorFor(file))
|
354 | 376 | .then(function () {
|
355 | 377 | // remove the file object from the parent's children list
|
356 |
| - var index = parent.children.indexOf(file); |
| 378 | + if (notMetaFile(file)) { |
| 379 | + var index = parent.children.indexOf(file); |
357 | 380 |
|
358 |
| - if (index !== -1) { |
359 |
| - parent.children.splice(index, 1); |
| 381 | + if (index !== -1) { |
| 382 | + parent.children.splice(index, 1); |
| 383 | + } |
| 384 | + } else { |
| 385 | + var metaIndex = parent.metaChildren.indexOf(file); |
| 386 | + |
| 387 | + if (metaIndex !== -1) { |
| 388 | + parent.metaChildren.splice(metaIndex, 1); |
| 389 | + } |
360 | 390 | }
|
361 | 391 |
|
362 | 392 | $rootScope.$broadcast('event:raml-editor-file-removed', file);
|
|
458 | 488 | target.forEachChildDo(function(c) {
|
459 | 489 | c.path = c.path.replace(target.path, newPath);
|
460 | 490 | });
|
| 491 | + |
| 492 | + // renames the path of each meta child under the current directory |
| 493 | + target.forEachMetaChildDo(function(c) { |
| 494 | + c.path = c.path.replace(target.path, newPath); |
| 495 | + }); |
461 | 496 | } else {
|
| 497 | + service.moveMeta(target, destination); |
462 | 498 | promise = target.persisted ? fileSystem.rename(target.path, newPath) : $q.when(target);
|
463 | 499 | }
|
464 | 500 |
|
|
475 | 511 | var metaFile = new RamlFile(file.path + '.meta', JSON.stringify(meta));
|
476 | 512 | return service.saveFile(metaFile)
|
477 | 513 | .then(function () {
|
| 514 | + var parent = service.getParent(metaFile); |
| 515 | + parent.metaChildren.push(metaFile); |
478 | 516 | return meta;
|
479 | 517 | })
|
480 | 518 | ;
|
|
493 | 531 | );
|
494 | 532 | };
|
495 | 533 |
|
| 534 | + service.moveMeta = function moveMeta(file, destination) { |
| 535 | + var metaName = file.name + '.meta'; |
| 536 | + var newMetaPath = service.join(destination.path, metaName); |
| 537 | + var metaPathName = file.path + '.meta'; |
| 538 | + var oldParent = service.getParent(file); |
| 539 | + |
| 540 | + var metaFile = oldParent.metaChildren.find(function(meta) { |
| 541 | + return meta.path === metaPathName; |
| 542 | + }); |
| 543 | + |
| 544 | + if (metaFile) { |
| 545 | + return fileSystem.rename(metaFile.path, newMetaPath).then( |
| 546 | + function success() { |
| 547 | + //Remove old parent meta data |
| 548 | + var index = oldParent.metaChildren.indexOf(metaFile); |
| 549 | + if (index !== -1) { |
| 550 | + oldParent.metaChildren.splice(index, 1); |
| 551 | + } |
| 552 | + metaFile.path = newMetaPath; |
| 553 | + destination.metaChildren.push(metaFile); |
| 554 | + return metaFile; |
| 555 | + }, |
| 556 | + function failure() { |
| 557 | + return metaFile; |
| 558 | + } |
| 559 | + ); |
| 560 | + } |
| 561 | + }; |
| 562 | + |
496 | 563 | service.join = function () {
|
497 | 564 | return Array.prototype.reduce.call(arguments, function (path, segment) {
|
498 | 565 | if (segment == null) {
|
|
0 commit comments