+ },
+
+ addTheme: function()
+ {
+ var selection = window.getSelection();
+ var n = selection.rangeCount;
+
+ console.log("Range count:", n);
+
+ if(n == 0)
+ window.alert("Nie zaznaczono żadnego obszaru");
+
+ // for now allow only 1 range
+ if(n > 1)
+ window.alert("Zaznacz jeden obszar");
+
+
+ // from this point, we will assume that the ranges are disjoint
+ for(var i=0; i < n; i++)
+ {
+ var range = selection.getRangeAt(i);
+ console.log(i, range.startContainer, range.endContainer);
+ 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);
+
+ // insert theme-ref
+
+ var elem = $('<span x-editable="true" x-node="motyw" class="motyw">Nowy motyw</span>');
+ elem.attr('x-attr-qname-'+id, 'id');
+ elem.attr('x-attr-value-'+id, 'm'+id);
+ spoint.insertNode(elem[0]);
+
+ // insert theme-begin
+ elem = $('<span x-node="begin" class="begin"></span>');
+ elem.attr('x-attr-qname-'+id, 'id');
+ elem.attr('x-attr-value-'+id, 'b'+id);
+ spoint.insertNode(elem[0]);
+
+ elem = $('<span x-node="end" class="end"></span>');
+ elem.attr('x-attr-qname-'+id, 'id');
+ elem.attr('x-attr-value-'+id, 'e'+id);
+ epoint.insertNode(elem[0]);
+ }
+
+ //selection.removeAllRanges();
+ },
+
+ selectTheme: function(themeId)
+ {
+ var selection = document.getSelection();
+
+ // remove current selection
+ selection.removeAllRanges();
+
+ var range = document.createRange();
+ var s = $('#m'+themeId)[0];
+ var e = $('#e'+themeId)[0];
+ console.log('Selecting range:', themeId, range, s, e);
+
+ if(s && e) {
+ range.setStartAfter(s);
+ range.setEndBefore(e);
+ selection.addRange(range);
+ }