+ },
+
+ deleteElement: function($editable)
+ {
+ var relatedThemes = $("*[x-node='begin'], *[x-node='end']", $editable);
+
+ var themeMarks = relatedThemes.map(function() {
+ return $(".motyw[theme-class='"+$(this).attr('theme-class')+"']");
+ });
+
+ if($editable.is("*.motyw"))
+ {
+ console.log($editable);
+ var selector = "[theme-class='"+$editable.attr('theme-class')+"']";
+ relatedThemes = relatedThemes.add("*[x-node='begin']"+selector+", *[x-node='end']"+selector);
+ }
+
+ console.log(relatedThemes, themeMarks);
+
+ var del = confirm("Usunięcie elementu jest nieodwracalne.\n"
+ +" Czy na pewno chcesz usunąć ten element, wraz z zawartymi motywami ?\n");
+
+ if(del) {
+ relatedThemes.remove();
+ themeMarks.remove();
+ $editable.remove();
+ }
+ },
+
+ //
+ // Special stuff for themes
+ //
+
+ // Theme related stuff
+ verifyThemeInsertPoint: function(node) {
+
+ if(node.nodeType == 3) { // Text Node
+ node = node.parentNode;
+ }
+
+ if(node.nodeType != 1) return false;
+
+ console.log('Selection point:', node);
+
+ node = $(node);
+ var xtype = node.attr('x-node');
+
+ if(!xtype || (xtype.search(':') >= 0) ||
+ xtype == 'motyw' || xtype == 'begin' || xtype == 'end')
+ return false;
+
+ // this is hopefully redundant
+ //if(! node.is('*.utwor *') )
+ // return false;
+
+ // don't allow themes inside annotations
+ if( node.is('*[x-annotation-box] *') )
+ return false;
+
+ return true;
+ },
+
+
+ editTheme: function($element)
+ {
+ var themeTextSpan = $('.theme-text-list', $element);
+ this.themeEditor.setFromString( themeTextSpan.text() );
+
+ function _editThemeFinish(dialog) {
+ themeTextSpan.text( dialog.userData.themes.join(', ') );
+ };
+
+ this.themeEditor.show(_editThemeFinish);
+ },
+
+
+ addTheme: function()
+ {
+ var selection = window.getSelection();
+ var n = selection.rangeCount;
+
+ console.log("Range count:", n);
+ if(n == 0) {
+ window.alert("Nie zaznaczono żadnego obszaru");
+ return false;
+ }
+
+ // for now allow only 1 range
+ if(n > 1) {
+ window.alert("Zaznacz jeden obszar");
+ return false;
+ }
+
+ // remember the selected range
+ var range = selection.getRangeAt(0);
+ console.log(range.startContainer, range.endContainer);
+
+ // verify if the start/end points make even sense -
+ // they must be inside a x-node (otherwise they will be discarded)
+ // and the x-node must be a main text
+ if(! this.verifyThemeInsertPoint(range.startContainer) ) {
+ window.alert("Motyw nie może się zaczynać w tym miejscu.");
+ return false;
+ }
+
+ if(! this.verifyThemeInsertPoint(range.endContainer) ) {
+ window.alert("Motyw nie może się kończyć w tym miejscu.");
+ return false;
+ }
+
+ function _addThemeFinish(dialog)
+ {
+ var date = (new Date()).getTime();
+ var random = Math.floor(4000000000*Math.random());
+ var id = (''+date) + '-' + (''+random);
+
+ var spoint = document.createRange();
+ var epoint = document.createRange();
+
+ spoint.setStart(range.startContainer, range.startOffset);
+ epoint.setStart(range.endContainer, range.endOffset);
+
+ var mtag, btag, etag, errors;
+ var themesStr = dialog.userData.themes.join(', ');
+
+ // insert theme-ref
+ mtag = $('<span></span>');
+ spoint.insertNode(mtag[0]);
+ errors = this.model.updateWithWLML(mtag, '<motyw id="m'+id+'">'+themesStr+'</motyw>');
+
+ if(errors) {
+ messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
+ return false;
+ }
+
+ // insert theme-begin
+ btag = $('<span></span>');
+ spoint.insertNode(btag[0]);
+ errors = this.model.updateWithWLML(btag, '<begin id="b'+id+'" />');
+ if(errors) {
+ mtag.remove();
+ messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
+ return false;
+ }
+
+ etag = $('<span></span>');
+ epoint.insertNode(etag[0]);
+ result = this.model.updateWithWLML(etag, '<end id="e'+id+'" />');
+ if(errors) {
+ btag.remove();
+ mtag.remove();
+ messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
+ return false;
+ }
+
+ selection.removeAllRanges();
+ return true;
+ };
+
+ // show the modal
+ this.themeEditor.setFromString('');
+ this.themeEditor.show(_addThemeFinish.bind(this));
+ },
+
+ selectTheme: function(themeId)
+ {
+ var selection = window.getSelection();
+
+ // remove current selection
+ selection.removeAllRanges();
+
+ var range = document.createRange();
+ var s = $(".motyw[theme-class='"+themeId+"']")[0];
+ var e = $(".end[theme-class='"+themeId+"']")[0];
+ console.log('Selecting range:', themeId, range, s, e);
+
+ if(s && e) {
+ range.setStartAfter(s);
+ range.setEndBefore(e);
+ selection.addRange(range);
+ }
+ },
+
+ addAnnotation: function()
+ {
+ var selection = window.getSelection();
+ var n = selection.rangeCount;
+
+ console.log("Range count:", n);
+ if(n == 0) {
+ window.alert("Nie zaznaczono żadnego obszaru");
+ return false;
+ }
+
+ // for now allow only 1 range
+ if(n > 1) {
+ window.alert("Zaznacz jeden obszar");
+ return false;
+ }
+
+ // remember the selected range
+ var range = selection.getRangeAt(0);
+
+ if(! this.verifyThemeInsertPoint(range.endContainer) ) {
+ window.alert("Nie można wstawić w to miejsce przypisu.");
+ return false;
+ }
+
+ var text = range.toString();
+ var tag = $('<span></span>');
+ range.collapse(false);
+ range.insertNode(tag[0]);
+ var errors = this.model.updateWithWLML(tag, '<pr><slowo_obce>'+text+"</slowo_obce> </pr>");
+
+ if(errors) {
+ tag.remove();
+ messageCenter.addMessage('error', null, 'Błąd przy dodawaniu przypisu:' + errors);
+ return false;
+ }
+
+ return true;