editor: fixing, refactoring, improving styling in choice exercises
[fnpeditor.git] / src / editor / modules / data / document.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var _ = require('libs/underscore'),
8     Dialog = require('views/dialog/dialog'),
9     wlxml = require('wlxml/wlxml'),
10     logging = require('fnpjs/logging/logging');
11
12
13 var logger = logging.getLogger('document');
14
15 var Document = function() {
16     wlxml.WLXMLDocument.apply(this, Array.prototype.slice.call(arguments, 0));
17     this.properties = {};
18 };
19 Document.prototype = Object.create(wlxml.WLXMLDocument.prototype);
20
21 _.extend(Document.prototype, {
22     transaction: function(body, params) {
23         params = params || {};
24         var error = params.error;
25         params.error = function(e) {
26             logger.exception(e);
27
28             var dialog = Dialog.create({
29                 title: gettext('Error'),
30                 text: gettext('Something wrong happend when applying this change so it was undone.'),
31                 executeButtonText: gettext('Close')
32             });
33             dialog.show();
34             if(error) {
35                 error(e);
36             }
37             dialog.on('execute', function(e) {
38                 e.success();
39             });
40         }.bind(this);
41         return wlxml.WLXMLDocument.prototype.transaction.call(this, body, params);
42     },
43     getUrlForLink: function(link) {
44         var cfg = this.options.editorConfig;
45         if(link.substr(0, 7) === 'file://' && cfg && cfg.documentAttachmentUrl) {
46             link = cfg.documentAttachmentUrl(link.substr(7));
47         }
48         return link;
49     },
50     getLinkForUrl: function(url) {
51         /* globals window */
52         var baseUrl = function(url) {return url.split('/').slice(0,-1).join('/');};
53         if(baseUrl(url) === baseUrl(window.location.origin + this.getUrlForLink('file://test'))) {
54             return 'file://' + _.last(url.split('/'));
55         }
56         return url;
57     },
58     setProperty: function(propName, propValue) {
59         if(this.properties[propName] !== propValue) {
60             this.properties[propName] = propValue;
61             this.trigger('propertyChanged', propName, propValue);
62         }
63     }
64 });
65
66 var DumbDocument = function() {
67     Document.apply(this, Array.prototype.slice.call(arguments, 0));
68 };
69 DumbDocument.prototype = Object.create(Document.prototype);
70 _.extend(DumbDocument.prototype, {
71     loadXML: function(xml) {
72         this._xml = xml;
73         this.trigger('contentSet');
74     },
75     toXML: function() {
76         return this._xml;
77     }
78 });
79
80 return {Document: Document, DumbDocument: DumbDocument};
81
82 });