aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
authorNivesh Rajbhandari2012-04-16 16:06:24 -0700
committerNivesh Rajbhandari2012-04-16 16:06:24 -0700
commitc253192a08b499ea7be46fa5438d273e51f7ec5a (patch)
tree18a1f0e3679c0eb993a9dedb537035d3861f49ac /js/mediators/io-mediator.js
parente19376c54eedd1f1c457ba405b2f110be376a559 (diff)
parent4b900ea5cd6bb77eb30cec8c03b9ec9fa662c1e9 (diff)
downloadninja-c253192a08b499ea7be46fa5438d273e51f7ec5a.tar.gz
Merge branch 'refs/heads/ninja-internal' into WebGLFixes
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js1030
1 files changed, 566 insertions, 464 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index d81f4543..057a849d 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -6,552 +6,654 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
6 6
7//////////////////////////////////////////////////////////////////////// 7////////////////////////////////////////////////////////////////////////
8// 8//
9var Montage = require("montage/core/core").Montage, 9var Montage = require("montage/core/core").Montage,
10 Component = require("montage/ui/component").Component, 10 Component = require("montage/ui/component").Component,
11 FileIo = require("js/io/system/fileio").FileIo, 11 FileIo = require("js/io/system/fileio").FileIo,
12 ProjectIo = require("js/io/system/projectio").ProjectIo; 12 ProjectIo = require("js/io/system/projectio").ProjectIo,
13 TemplateCreator = require("node_modules/tools/template-creator").TemplateCreator;
13//////////////////////////////////////////////////////////////////////// 14////////////////////////////////////////////////////////////////////////
14// 15//
15exports.IoMediator = Montage.create(Component, { 16exports.IoMediator = Montage.create(Component, {
16 //////////////////////////////////////////////////////////////////// 17 ////////////////////////////////////////////////////////////////////
17 // 18 //
18 hasTemplate: { 19 hasTemplate: {
19 enumerable: false, 20 enumerable: false,
20 value: false 21 value: false
21 }, 22 },
22 //////////////////////////////////////////////////////////////////// 23 ////////////////////////////////////////////////////////////////////
23 // 24 //
24 deserializedFromTemplate: { 25 deserializedFromTemplate: {
25 enumerable: false, 26 enumerable: false,
26 value: function () { 27 value: function () {
27 // 28 //
28 } 29 }
29 }, 30 },
30 //////////////////////////////////////////////////////////////////// 31 ////////////////////////////////////////////////////////////////////
31 // 32 //
32 fio: { 33 fio: {
33 enumerable: false, 34 enumerable: false,
34 value: FileIo 35 value: FileIo
35 }, 36 },
36 //////////////////////////////////////////////////////////////////// 37 ////////////////////////////////////////////////////////////////////
37 // 38 //
38 pio: { 39 pio: {
39 enumerable: false, 40 enumerable: false,
40 value: ProjectIo 41 value: ProjectIo
41 }, 42 },
42 //////////////////////////////////////////////////////////////////// 43 ////////////////////////////////////////////////////////////////////
43 // 44 //
44 getAppTemplatesUrlRegEx: { 45 getAppTemplatesUrlRegEx: {
45 enumerable: false, 46 enumerable: false,
46 value: function () { 47 value: function () {
47 var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi'); 48 var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi');
48 return regex; 49 return regex;
49 } 50 }
50 }, 51 },
51 //////////////////////////////////////////////////////////////////// 52 ////////////////////////////////////////////////////////////////////
52 // 53 //
53 fileNew: { 54 fileNew: {
54 enumerable: false, 55 enumerable: false,
55 value: function (file, template, callback) { 56 value: function (file, template, callback) {
56 //Loading template from template URL 57 //Loading template from template URL
57 var xhr = new XMLHttpRequest(), result; 58 var xhr = new XMLHttpRequest(), result;
58 xhr.open("GET", template, false); 59 xhr.open("GET", template, false);
59 xhr.send(); 60 xhr.send();
60 if (xhr.readyState === 4) { 61 if (xhr.readyState === 4) {
61 //Making call to create file, checking for return code 62 //Making call to create file, checking for return code
62 switch (this.fio.newFile({uri: file, contents: xhr.response})) { 63 switch (this.fio.newFile({ uri: file, contents: xhr.response })) {
63 case 201: 64 case 201:
64 result = {status: 201, success: true, uri: file}; 65 result = { status: 201, success: true, uri: file };
65 break; 66 break;
66 case 204: 67 case 204:
67 result = {status: 204, success: false, uri: file}; 68 result = { status: 204, success: false, uri: file };
68 break; 69 break;
69 case 400: 70 case 400:
70 result = {status: 400, success: false, uri: file}; 71 result = { status: 400, success: false, uri: file };
71 break; 72 break;
72 default: 73 default:
73 result = {status: 500, success: false, uri: file}; 74 result = { status: 500, success: false, uri: file };
74 break; 75 break;
75 } 76 }
76 } else { 77 } else {
77 result = {status: 500, success: false, uri: file}; 78 result = { status: 500, success: false, uri: file };
78 } 79 }
79 //Sending result to callback if requested for handling 80 //Sending result to callback if requested for handling
80 if (callback) callback(result); 81 if (callback) callback(result);
81 //Codes 82 //Codes
82 // 204: File exists | 400: File exists 83 // 204: File exists | 400: File exists
83 // 201: File succesfully created | 500: Unknown (Probably cloud API not running) 84 // 201: File succesfully created | 500: Unknown (Probably cloud API not running)
84 } 85 }
85 }, 86 },
86 //////////////////////////////////////////////////////////////////// 87 ////////////////////////////////////////////////////////////////////
87 // 88 //
88 fileOpen: { 89 fileOpen: {
89 enumerable: false, 90 enumerable: false,
90 value: function (file, callback) { 91 value: function (file, callback) {
91 //Reading file (Ninja doesn't really open a file, all in browser memory) 92 //Reading file (Ninja doesn't really open a file, all in browser memory)
92 var read = this.fio.readFile({uri: file}), result; 93 var read = this.fio.readFile({ uri: file }), result;
93 //Checking for status 94 //Checking for status
94 switch(read.status) { 95 switch (read.status) {
95 case 204: 96 case 204:
96 //Creating and formatting result object for callbak 97 //Creating and formatting result object for callbak
97 result = read.file.details; 98 result = read.file.details;
98 result.root = read.file.details.uri.replace(read.file.details.name, ""); 99 result.root = read.file.details.uri.replace(read.file.details.name, "");
99 //Checking for type of content to returns 100 //Checking for type of content to returns
100 if (result.extension !== 'html' && result.extension !== 'htm') { 101 if (result.extension !== 'html' && result.extension !== 'htm') {
101 //Simple string 102 //Simple string
102 result.content = read.file.content; 103 result.content = read.file.content;
103 } else { 104 } else {
104 //Object to be used by Ninja Template 105 //Object to be used by Ninja Template
105 result.content = this.parseHtmlToNinjaTemplate(read.file.content); 106 result.content = this.parseHtmlToNinjaTemplate(read.file.content);
106 } 107 }
107 //Status of call 108 //Status of call
108 result.status = read.status; 109 result.status = read.status;
109 //Calling back with result 110 //Calling back with result
110 if (callback) callback(result); 111 if (callback) callback(result);
111 break; 112 break;
112 case 404: 113 case 404:
113 //File does not exists 114 //File does not exists
114 if (callback) callback({status: read.status}); 115 if (callback) callback({ status: read.status });
115 break; 116 break;
116 default: 117 default:
117 //Unknown 118 //Unknown
118 if (callback) callback({status: 500}); 119 if (callback) callback({ status: 500 });
119 break; 120 break;
120 } 121 }
121 /* 122 /*
122 //////////////////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////
123 //////////////////////////////////////////////////////////// 124 ////////////////////////////////////////////////////////////
124 //Return Object Description 125 //Return Object Description
125 Object.status (Always presents for handling) 126 Object.status (Always presents for handling)
126 204: File exists (Success) 127 204: File exists (Success)
127 404: File does not exists (Failure) 128 404: File does not exists (Failure)
128 500: Unknown (Probably cloud API not running) 129 500: Unknown (Probably cloud API not running)
129 130
130 (Below only present if succesfull 204) 131 (Below only present if succesfull 204)
131 132
132 Object.content 133 Object.content
133 Object.extension 134 Object.extension
134 Object.name 135 Object.name
135 Object.uri 136 Object.uri
136 Object.creationDate 137 Object.creationDate
137 Object.modifiedDate 138 Object.modifiedDate
138 Object.readOnly 139 Object.readOnly
139 Object.size 140 Object.size
140 //////////////////////////////////////////////////////////// 141 ////////////////////////////////////////////////////////////
141 //////////////////////////////////////////////////////////// 142 ////////////////////////////////////////////////////////////
142