diff options
author | Valerio Virgillito | 2012-02-15 17:04:39 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-15 17:04:39 -0800 |
commit | fe71cb9aa1700199c0089166ad54fc56eb760644 (patch) | |
tree | b0c7c24b1f45a079e1c09312ca716e3d35aecad4 /js/io/system/fileio.js | |
parent | 747616980cad14f0b65fbcc7f497ed9680a39d29 (diff) | |
parent | d366c0bd1af6471511217ed574083e15059519b5 (diff) | |
download | ninja-fe71cb9aa1700199c0089166ad54fc56eb760644.tar.gz |
Merge branch 'refs/heads/master' into components
Diffstat (limited to 'js/io/system/fileio.js')
-rwxr-xr-x[-rw-r--r--] | js/io/system/fileio.js | 391 |
1 files changed, 196 insertions, 195 deletions
diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js index 1d76a91b..f78ad173 100644..100755 --- a/js/io/system/fileio.js +++ b/js/io/system/fileio.js | |||
@@ -4,223 +4,224 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | //Required modules | 7 | /* ///////////////////////////////////////////////////////////////////// |
8 | var Serializer = require("montage/core/serializer").Serializer; | 8 | //////////////////////////////////////////////////////////////////////// |
9 | NOTES: | ||
10 | |||
11 | For newFile, only the 'uri' is required, if contents is empty, such | ||
12 | empty file will be created. 'contents' should be a string to be saved | ||
13 | as the file. 'contentType' is the mime type of the file. | ||
14 | |||
15 | Core API reference in NINJA: this.application.ninja.coreIoApi | ||
16 | |||
17 | //////////////////////////////////////////////////////////////////////// | ||
18 | ///////////////////////////////////////////////////////////////////// */ | ||
19 | // | ||
20 | var Montage = require("montage/core/core").Montage, | ||
21 | Component = require("montage/ui/component").Component; | ||
22 | //////////////////////////////////////////////////////////////////////// | ||
9 | //Exporting as File I/O | 23 | //Exporting as File I/O |
10 | exports.FileIo = (require("montage/core/core").Montage).create(Object.prototype, { | 24 | exports.FileIo = Montage.create(Component, { |
11 | /* | ||
12 | create: { | ||
13 | enumerable: true, | ||
14 | value: function (type) { | ||
15 | // | ||
16 | } | ||
17 | }, | ||
18 | */ | ||
19 | //////////////////////////////////////////////////////////////////// | 25 | //////////////////////////////////////////////////////////////////// |
20 | // | 26 | //Creating new file |
21 | open: { | 27 | newFile: { |
22 | enumerable: true, | 28 | enumerable: true, |
23 | value: function(doc, type, uri, server) { | 29 | value: function(file) { |
24 | // | 30 | //Checking for API to be available |
25 | var file = {}, head, body, h, b; | 31 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
26 | file.uri = uri; | 32 | //API not available, no IO action taken |
27 | file.server = server; | 33 | return null; |
28 | // | 34 | } |
29 | if (doc.content) { | 35 | //Peforming check for file to exist |
30 | if (type === 'html' || type === 'htm') { | 36 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create; |
31 | // | 37 | //Upon successful check, handling results |
32 | h = doc.content.split('</head>'); | 38 | if (check.success) { |
33 | h = h[0].split('<head>'); | 39 | //Handling status of check |
34 | head = h[1]; | 40 | switch (check.status) { |
35 | // | 41 | case 204: |
36 | b = doc.content.split('</body>'); | 42 | //Storing status to be returned (for UI handling) |
37 | b = b[0].split('<body>'); | 43 | status = check.status; |
38 | body = b[1]; | 44 | break; |
39 | // | 45 | case 404: |
40 | file.type = 'html'; | 46 | //File does not exists, ready to be created |
41 | file.head = head; | 47 | create = this.application.ninja.coreIoApi.createFile(file); |
42 | file.body = body; | 48 | status = create.status; |
43 | } else { | 49 | break; |
44 | //TODO: Add other file type routines | 50 | default: |
45 | file.type = type; | 51 | //Unknown Error |
46 | file.content = doc.content; | 52 | status = 500; |
47 | } | 53 | break; |
48 | } else { | 54 | } |
49 | //TODO: File is empty | 55 | } else { |
50 | if (type === 'html' || type === 'htm') { | 56 | //Unknown Error |
51 | head = ''; | 57 | status = 500; |
52 | body = ''; | 58 | } |
53 | // | 59 | //Returning resulting code |
54 | file.type = 'html'; | 60 | return status; |
55 | file.head = head; | 61 | // 204: File exists (not created) | 400: File exists | 404: File does not exists |
56 | file.body = body; | 62 | // 201: File succesfully created | 500: Unknown |
57 | } else { | 63 | } |
58 | //TODO: Add other file type routines | 64 | }, |
59 | file.type = type; | 65 | //////////////////////////////////////////////////////////////////// |
60 | file.content = doc.content; | 66 | //Reading contents from file |
61 | } | 67 | readFile: { |
62 | } | 68 | enumerable: true, |
63 | //TODO: Load contents into App | 69 | value: function(file) { |
64 | //documentManagerModule.DocumentManager.openDocument(file); | 70 | //Checking for API to be available |
71 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | ||
72 | //API not available, no IO action taken | ||
73 | return null; | ||
74 | } | ||
75 | //Peforming check for file to exist | ||
76 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; | ||
77 | //Upon successful check, handling results | ||
78 | if (check.success) { | ||
79 | //Handling status of check | ||
80 | switch (check.status) { | ||
81 | case 204: | ||
82 | //File exists | ||
83 | result = {}; | ||
84 | result.content = this.application.ninja.coreIoApi.readFile(file).content; | ||
85 | result.details = this.infoFile(file); | ||
86 | status = check.status; | ||
87 | break; | ||
88 | case 404: | ||
89 | //File does not exists | ||
90 | status = check.status; | ||
91 | break; | ||
92 | default: | ||
93 | //Unknown Error | ||
94 | status = 500; | ||
95 | break; | ||
96 | } | ||
97 | } else { | ||
98 | //Unknown Error | ||
99 | status = 500; | ||
100 | } | ||
101 | //Returning status and result (null if none) | ||
102 | return {status: status, file: result}; | ||
103 | //Status Codes | ||
104 | // 204: File exists | 404: File does not exists | 500: Unknown | ||
105 | } | ||
106 | }, | ||
107 | //////////////////////////////////////////////////////////////////// | ||
108 | //Saving file (existing file or creates and saves if none exists) | ||
109 | saveFile: { | ||
110 | enumerable: true, | ||
111 | value: function(file) { | ||
112 | //Checking for API to be available | ||
113 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | ||
114 | //API not available, no IO action taken | ||
115 | return null; | ||
116 | } | ||
117 | //Peforming check for file to exist | ||
118 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result; | ||
119 | //Upon successful check, handling results | ||
120 | if (check.success) { | ||
121 | //Handling status of check | ||
122 | switch (check.status) { | ||
123 | case 204: | ||
124 | //File exists | ||
125 | result = this.application.ninja.coreIoApi.updateFile(file); | ||
126 | status = 204; | ||
127 | break; | ||
128 | case 404: | ||
129 | //File does not exists, ready to be created | ||
130 | result = this.application.ninja.coreIoApi.createFile(file); | ||
131 | status = 404; | ||
132 | break; | ||
133 | default: | ||
134 | //Unknown Error | ||
135 | status = 500; | ||
136 | break; | ||
137 | } | ||
138 | } else { | ||
139 | //Unknown Error | ||
140 | status = 500; | ||
141 | } | ||
142 | //Returning status and result (null if none) | ||
143 | return {status: status, result: result}; | ||
144 | //Status Codes | ||
145 | // 204: File exists | 404: File does not exists | 500: Unknown | ||
65 | } | 146 | } |
66 | }, | 147 | }, |
67 | //////////////////////////////////////////////////////////////////// | 148 | //////////////////////////////////////////////////////////////////// |
68 | // | 149 | // |
69 | save: { | 150 | deleteFile: { |
70 | enumerable: true, | 151 | enumerable: true, |
71 | value: function(type, id, components) { | 152 | value: function() { |
72 | 153 | //Checking for API to be available | |
73 | /* | 154 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
74 | 155 | //API not available, no IO action taken | |
75 | GETS HTML IN LOADED DOCUMENT | 156 | return null; |
76 | document.getElementById('userDocument').contentDocument.documentElement.outerHTML | ||
77 | |||
78 | GETS HTML IN <HEAD> AND <BODY> OR ANYTHING INSIDE <HTML> | ||
79 | document.getElementById('userDocument').contentDocument.documentElement.innerHTML | ||
80 | |||
81 | THE ABOVE METHOD SEEMS TO BE BETTER JUST IN CASE PEOPLE REMOVE THE BODY TAG SINCE NOT REQUIRED IN HTML5 | ||
82 | |||
83 | GETS HTML IN <BODY> ONLY | ||
84 | document.getElementById('userDocument').contentDocument.body.innerHTML | ||
85 | |||
86 | HACK TO GET THE STYLES OF THE ELEMENTS ADDED WHILE DRAWING | ||
87 | document.getElementById('userDocument').contentDocument.styleSheets[document.getElementById('userDocument').contentDocument.styleSheets.length-1] | ||
88 | |||
89 | CSS SEEMS TO BE RESERVED WHEN APPENDED, MEANING 0 IN THE ARRAY IS ACTUALLY THE LAST DEFINED STYLE IN THE CSS | ||
90 | |||
91 | //GETS CSS RULES APPLIED TO ALL OBJECTS CREATED BY THE APP | ||
92 | document.getElementById('userDocument').contentDocument.styleSheets[document.getElementById('userDocument').contentDocument.styleSheets.length-1].cssRules | ||
93 | |||
94 | document.getElementById('userDocument').contentDocument.getElementById('userHead').innerHTML | ||
95 | document.getElementById('userDocument').contentDocument.getElementById('UserContent').innerHTML | ||
96 | this.getCssFromRules(document.getElementById('userDocument').contentDocument.styleSheets[document.getElementById('userDocument').contentDocument.styleSheets.length-1].cssRules) | ||
97 | |||
98 | */ | ||
99 | |||
100 | // | ||