PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/opencore/js/thirdparty/xinha96r1270/plugins/PSServer/PSServer.js

https://github.com/socialplanning/oc-js
JavaScript | 282 lines | 217 code | 44 blank | 21 comment | 30 complexity | 3869d7a81019dc8d98c69c1dc5d9f018 MD5 | raw file
  1. /**
  2. * PSServer PSServer.js file.
  3. * This plugin is a server based persistent storage backend.
  4. */
  5. (function() {
  6. var PSServer = window.PSServer = function PSServer(editor) {
  7. this.editor = editor;
  8. }
  9. PSServer._pluginInfo = {
  10. name : "PSServer",
  11. version : "2.0",
  12. developer : "Douglas Mayle",
  13. developer_url : "http://xinha.org",
  14. license : "MIT"
  15. };
  16. PSServer.prototype.onGenerateOnce = function () {
  17. // We use _loadConfig to asynchronously load the config and then register the
  18. // backend.
  19. this._loadConfig();
  20. };
  21. PSServer.prototype._loadConfig = function() {
  22. var self = this;
  23. if (!this._serverConfig) {
  24. Xinha._getback(Xinha.getPluginDir("PSServer") + "/config.inc.php",
  25. function(config) {
  26. self._serverConfig = eval('(' + config + ')');
  27. self._serverConfig.user_affinity = 20;
  28. self._serverConfig.displayName = 'Server';
  29. self._loadConfig();
  30. });
  31. return;
  32. }
  33. this._registerBackend();
  34. }
  35. PSServer.prototype._registerBackend = function(timeWaited) {
  36. var editor = this.editor;
  37. var self = this;
  38. if (!timeWaited) {
  39. timeWaited = 0;
  40. }
  41. // Retry over a period of ten seconds to register. We back off exponentially
  42. // to limit resouce usage in the case of misconfiguration.
  43. var registerTimeout = 10000;
  44. if (timeWaited > registerTimeout) {
  45. // This is most likely a configuration error. We're loaded and
  46. // PersistentStorage is not.
  47. return;
  48. }
  49. if (!editor.plugins['PersistentStorage'] ||
  50. !editor.plugins['PersistentStorage'].instance ||
  51. !editor.plugins['PersistentStorage'].instance.ready) {
  52. window.setTimeout(function() {self._registerBackend(timeWaited ? timeWaited*2 : 50);}, timeWaited ? timeWaited : 50);
  53. return;
  54. }
  55. editor.plugins['PersistentStorage'].instance.registerBackend('PSServer', this, this._serverConfig);
  56. }
  57. PSServer.prototype.loadData = function (asyncCallback) {
  58. var self = this;
  59. Xinha._getback(Xinha.getPluginDir("PSServer") + "/backend.php?directory&listing",
  60. function(json) {
  61. self.dirTree = eval('(' + json + ')');
  62. asyncCallback(self.dirTree);
  63. });
  64. }
  65. var treeRecurse = function treeRecurse(tree, callback, root) {
  66. if (typeof root == 'undefined') {
  67. root = '/';
  68. callback('/', '', tree);
  69. }
  70. for (var key in tree) {
  71. callback(root, key, tree[key]);
  72. if (tree[key].$type == 'folder') {
  73. treeRecurse(tree[key], callback, root + key + '/');
  74. }
  75. }
  76. };
  77. PSServer.prototype.getFilters = function(dirTree) {
  78. // Clear out the previous directory listing.
  79. var filters = [];
  80. treeRecurse(dirTree, function(path, key, value) {
  81. if (value.$type != 'folder') {
  82. return;
  83. }
  84. var filePath = key.length ? path + key + '/' : path;
  85. var filePathDisplay = key.length ? path + key + '/' : path;
  86. if (filePathDisplay.length > 1) {
  87. filePathDisplay = filePathDisplay.substring(0, filePathDisplay.length-1);
  88. }
  89. filters.push({
  90. value: filePath,
  91. display: filePathDisplay
  92. });
  93. });
  94. return filters;
  95. }
  96. PSServer.prototype.loadDocument = function(entry, asyncCallback) {
  97. Xinha._getback(entry.URL,
  98. function(documentSource) {
  99. asyncCallback(documentSource);
  100. });
  101. }
  102. PSServer.prototype.getMetadata = function(dirTree, pathFilter, typeFilter) {
  103. var editor = this.editor;
  104. var self = this;
  105. var metadata = [];
  106. var typeKeys = {};
  107. for (var index=0; index<typeFilter.length; ++index) {
  108. typeKeys[typeFilter[index]] = true;
  109. }
  110. treeRecurse(dirTree, function(path, key, value) {
  111. if (!value.$type || !key) {
  112. // This is a builtin property of objects, not one returned by the
  113. // backend.
  114. return;
  115. }
  116. if (path != pathFilter) {
  117. return;
  118. }
  119. if (!(value.$type in typeKeys)) {
  120. return;
  121. }
  122. if ((value.$type == 'folder') || (value.$type == 'html') ||
  123. (value.$type == 'text') || (value.$type == 'document')) {
  124. metadata.push({
  125. name: key,
  126. key: path + key,
  127. $type: value.$type
  128. });
  129. } else {
  130. metadata.push({
  131. URL: Xinha.getPluginDir("PSServer") + '/demo_images' + path + key,
  132. name: key,
  133. key: path + key,
  134. $type: value.$type
  135. });
  136. }
  137. });
  138. return metadata;
  139. }
  140. PSServer.prototype.loadDocument = function(entry, asyncCallback) {
  141. var self = this;
  142. Xinha._getback(Xinha.getPluginDir("PSServer") + '/demo_images' + entry.key,
  143. function(documentSource) {
  144. asyncCallback(documentSource);
  145. });
  146. }
  147. PSServer.prototype.buildImportUI = function(dialog, element) {
  148. // We receive an HTML element and are expected to build an HTML UI. We'll
  149. // model it off of this HTML fragment:
  150. // <form target="importFrame" action="../plugins/PSServer/backend.php?upload&amp;replace=false&amp;" id="importForm" method="post" enctype="multipart/form-data">
  151. // File: <input type="file" name="filedata" /><input type="submit" value="_(Import)" />
  152. // </form>
  153. // <iframe id="importFrame" name="importFrame" src="#" style="display:none;"></iframe>
  154. var iframeID = dialog.createId('importFrame');
  155. var form = document.createElement('form');
  156. form.setAttribute('enctype', 'multipart/form-data');
  157. form.setAttribute('method', 'post');
  158. form.setAttribute('action', Xinha.getPluginDir("PSServer") + "/backend.php?upload&replace=true&");
  159. var fileentry = document.createElement('input');
  160. fileentry.setAttribute('type', 'file');
  161. fileentry.setAttribute('name', 'filedata');
  162. var submitbutton = document.createElement('input');
  163. submitbutton.setAttribute('type', 'submit');
  164. submitbutton.setAttribute('value',Xinha._lc('Import', 'PSServer'));
  165. var filetext = document.createTextNode(Xinha._lc('File: ', 'PSServer'));
  166. filetext = form.appendChild(filetext);
  167. fileentry = form.appendChild(fileentry);
  168. submitbutton = form.appendChild(submitbutton);
  169. form = element.appendChild(form);
  170. form.setAttribute('target', iframeID);
  171. // The iframe must be added to the document after the form has been, or the targeting fails.
  172. var iframe = document.createElement('iframe');
  173. iframe.setAttribute('src', 'about:blank');
  174. iframe.style.display = 'none';
  175. iframe.id = iframe.name = iframeID;
  176. iframe.onload = function() {
  177. var docCheck = iframe.contentDocument || iframe.contentWindow;
  178. if (docCheck.location.href == 'about:blank') {
  179. return;
  180. }
  181. // What to do on import? Add an entry to the UI, I guess...
  182. alert('Add entry here');
  183. }
  184. iframe = element.appendChild(iframe);
  185. }
  186. PSServer.prototype.saveDocument = function(path, filename, documentSource, asyncCallback) {
  187. Xinha._postback(Xinha.getPluginDir("PSServer") + "/backend.php?upload&replace=true&filedata=" + escape(documentSource)+"&filename="+escape(path + filename),
  188. null,
  189. function(response) {
  190. asyncCallback(true);
  191. },
  192. function(response) {
  193. asyncCallback(false);
  194. });
  195. }
  196. PSServer.prototype.makeFolder = function(currentPath, folderName, asyncCallback) {
  197. Xinha._postback(Xinha.getPluginDir("PSServer") + "/backend.php?directory&create&dirname="+escape(currentPath + '/' + folderName),
  198. null,
  199. function(response) {
  200. asyncCallback(true);
  201. },
  202. function(response) {
  203. asyncCallback(false);
  204. });
  205. }
  206. PSServer.prototype.deleteEntry = function(entry, asyncCallback) {
  207. Xinha._postback(Xinha.getPluginDir("PSServer") + "/backend.php?file&delete&filename="+escape(entry.key),
  208. null,
  209. function(response) {
  210. asyncCallback(true);
  211. },
  212. function(response) {
  213. asyncCallback(false);
  214. });
  215. }
  216. PSServer.prototype.moveEntry = function(entry, container, asyncCallback) {
  217. Xinha._postback(Xinha.getPluginDir("PSServer") + "/backend.php?file&rename&filename="+escape(entry.key)+'&newname='+escape(container.key + '/' + entry.name),
  218. null,
  219. function(json) {
  220. asyncCallback(true);
  221. },
  222. function(json) {
  223. asyncCallback(false);
  224. });
  225. }
  226. PSServer.prototype.copyEntry = function(entry, asyncCallback) {
  227. Xinha._postback(Xinha.getPluginDir("PSServer") + "/backend.php?file&copy&filename="+escape(entry.key),
  228. null,
  229. function(json) {
  230. var newentry = eval('(' + json + ')');
  231. asyncCallback(true, newentry);
  232. },
  233. function(json) {
  234. var newentry = eval('(' + json + ')');
  235. asyncCallback(false, newentry);
  236. });
  237. }
  238. })();