' + footnote + '';
+ // Move cursor to end of content:
+ var range = editor.createRange();
+ range.moveToElementEditEnd(range.root);
+ editor.getSelection().selectRanges([range]);
+ // Insert the container:
+ editor.insertHtml(container);
+ } else {
+ $footnotes.find('ol').append(footnote);
+ }
+ return;
+ },
+
+ generateFootnoteId: function() {
+ var id = Math.random().toString(36).substr(2, 5);
+ while (jQuery.inArray(id, this.footnote_ids) != -1) {
+ id = String(this.generateFootnoteId());
+ }
+ this.footnote_ids.push(id);
+ return id;
+ },
+
+ reorderMarkers: function() {
+ this.editor.fire('lockSnapshot');
+ editor = this.editor;
+ $contents = jQuery('#' + editor.id + '_contents iframe').contents().find('body');
+ var data = {
+ order: [],
+ occurrences: {}
+ };
+
+ // Check that there's a footnotes section. If it's been deleted the markers are useless:
+ if ($contents.find('#footnotes').length == 0) {
+ $contents.find('sup[data-footnote-id]').remove();
+ return;
+ }
+
+ // Find all the markers in the document:
+ var $markers = $contents.find('sup[data-footnote-id]');
+ // If there aren't any, remove the Footnotes container:
+ if ($markers.length == 0) {
+ $contents.find('#footnotes').remove();
+ return;
+ }
+
+ // Otherwise reorder the markers:
+ $markers.each(function(){
+ var footnote_id = jQuery(this).attr('data-footnote-id')
+ , marker_ref
+ , n = data.order.indexOf(footnote_id);
+ // If there isn't a matching footnote, remove the marker:
+ //console.log($contents.find('#footnote-' + (n + 1)).length);
+ //console.log('#footnote-' + (n + 1));
+ /*if ($contents.find('#footnote-' + (n + 1)).length == 0) {
+ //jQuery(this).remove();
+ return;
+ }*/
+
+ // If this is the markers first occurrence:
+ if (n == -1) {
+ // Store the id:
+ data.order.push(footnote_id);
+ n = data.order.length;
+ data.occurrences[footnote_id] = 1;
+ marker_ref = n + '-1';
+ } else {
+ // Otherwise increment the number of occurrences:
+ // (increment n due to zero-index array)
+ n++;
+ data.occurrences[footnote_id]++;
+ marker_ref = n + '-' + data.occurrences[footnote_id];
+ }
+ // Replace the marker contents:
+ var marker = '[' + n + ']';
+ jQuery(this).html(marker);
+ });
+
+ // Then rebuild the Footnotes content to match marker order:
+ var footnotes = '';
+ var footnote_text = '';
+ for (i in data.order) {
+ footnote_id = data.order[i];
+ footnote_text = $contents.find('#footnotes [data-footnote-id=' + footnote_id + '] cite').html();
+ footnotes += this.buildFootnote(footnote_id, footnote_text, data);
+ }
+ $contents.find('#footnotes ol').html(footnotes);
+
+ // Next we need to reinstate the 'editable' properties of the footnotes.
+ // (we have to do this individually due to Widgets 'fireOnce' for editable selectors)
+ var footnote_widget;
+ // So first we need to find the right Widget instance:
+ // (I hope there's a better way of doing this but I can't find one)
+ for (i in this.editor.widgets.instances) {
+ if (this.editor.widgets.instances[i].name == 'footnotes') {
+ footnote_widget = this.editor.widgets.instances[i];
+ break;
+ }
+ }
+ // Then we `initEditable` each footnote, giving it a unique selector:
+ for (i in data.order) {
+ n = parseInt(i) + 1;
+ footnote_widget.initEditable('footnote_' + n, {selector: '#footnote-' + n +' cite', allowedContent: 'a[href]; cite[*](*); b i span'});
+ }
+
+ this.editor.fire('unlockSnapshot');
+ return;
+ }
+});