summaryrefslogtreecommitdiff
path: root/extensions/WikiEditor/modules/jquery.wikiEditor.templates.js
blob: b0462563e67421ac212c28b53ffb33f58dcda221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Templates Module for wikiEditor */
/*jshint onevar:false */
( function ( $ ) { $.wikiEditor.modules.templates = {

/**
 * Core Requirements
 */
req: [ 'iframe' ],
/**
 * Object Templates
 */
tpl: {
	marker: {
		type: 'template',
		anchor: 'wrap',
		skipDivision: 'realchange',
		afterWrap: function ( node ) {
			$( node ).addClass( 'wikiEditor-template' );
		},
		getAnchor: function ( ca1 ) {
			return $( ca1.parentNode ).is( '.wikiEditor-template' ) ? ca1.parentNode : null;
		}
	}
},
/**
 * Event handlers
 */
evt: {
	/**
	 * @param context
	 * @param event
	 */
	mark: function ( context ) {
		// The markers returned by this function are skipped on realchange, so don't regenerate them in that case
		if ( context.modules.highlight.currentScope === 'realchange' ) {
			return;
		}
		// Get references to the markers and tokens from the current context
		var markers = context.modules.highlight.markers;
		var tokens = context.modules.highlight.tokenArray;
		// Use depth-tracking to extract top-level templates from tokens
		var depth = 0, bias, start;
		for ( var i in tokens ) {
			depth += ( bias = tokens[i].label === 'TEMPLATE_BEGIN' ? 1 : ( tokens[i].label === 'TEMPLATE_END' ? -1 : 0 ) );
			if ( bias > 0 && depth === 1 ) {
				// Top-level opening - use offset as start
				start = tokens[i].offset;
			} else if ( bias < 0 && depth === 0 ) {
				// Top-level closing - use offset as end
				markers[markers.length] = $.extend(
					{ context: context, start: start, end: tokens[i].offset },
					$.wikiEditor.modules.templates.tpl.marker
				);
			}
			if ( depth < 0 ) {
				depth = 0;
			}
		}
	}
},
exp: [
	{ regex: /{{/, label: 'TEMPLATE_BEGIN' },
	{ regex: /}}/, label: 'TEMPLATE_END', markAfter: true }
],
/**
 * Internally used functions
 */
fn: {
	/**
	 * @param context
	 * @param config
	 */
	create: function () {
		// Do some stuff here...
	}
}

}; } ) ( jQuery );