aboutsummaryrefslogtreecommitdiff
path: root/mod/ecml/views/default/embed/web_services/content.php
blob: 125da80a200e434f677567f8ec53a112a2ad3ef0 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php 
/**
 * URL -> ECML converter for supported 3rd party services.
 * 
 * Tries to automatically detect which site/ecml to use.
 * Will highlight the correct one.
 * If can find none, will show an error.
 * Lets users click to force an ECML keyword, but will display a warning.
 * 
 */

$internal_name = elgg_get_array_value('internal_name', $vars);

$keywords = ecml_get_keywords();
elgg_sort_3d_array_by_value($keywords, 'name');

$keyword_js_array = array();
$keywords_html = "<ul class='ecml_web_service_list clearfloat'>";
// include support for standard ECML so you can get previews and validation.
//$keyword_html = '<li class="ecml_web_service"><a class="ecml">Generic ECML</a></li>';

foreach ($keywords as $i => $v) {
	if (!isset($v['type']) || $v['type'] != 'web_service') {
		unset ($keywords[$i]);
		continue;
	}
	
	// make sure the classname doens't have invalid chars.
	$class = str_replace(array('.', ','), '_', $i);
	$keyword_js_array[] = $class;
	
	$keywords_html .= "<li class='ecml_web_service'><a class=\"$class link\">{$v['name']}</a></li>";
}

$keywords_html .= "</ul>";

$keywords_js = json_encode($keyword_js_array);

$input = elgg_view('input/text', array(
	'internalid' => 'web_services_resource'
));

$embed = elgg_view('input/button', array(
	'name' => 'buggy',
	'internalid' => 'embed_submit',
	'type' => 'button',
	'value' => elgg_echo('embed:embed'),
	'class' => 'submit_button disabled',
	'disabled' => TRUE
));

echo '<p>' . elgg_echo('ecml:embed:instructions') . '</p>';

echo $keywords_html;

echo "<div class='embed_content_section instructions hidden'><a class='ecml_embed_instructions link'>Instructions</a>";
echo "<div id='embed_ecml_keyword_help' class='hidden'></div></div>";

echo "<div id='embed_service_url'><label>URL<br />".$input."<div id='url_status' class=''></div></label>";
echo "<div class='ecml_generated_code hidden'>ECML: <span id='ecml_code'></span></div></div>";

echo "<div class='embed_content_section preview hidden'><a class='ecml_embed_preview link'>Preview</a>";
echo "<div id='ecml_preview' class='hidden'></div></div>";

echo "<div class='divider margin_top'></div>";
echo $embed;

?>

<script type="text/javascript">

$(document).ready(function() {
	$('a.ecml_embed_instructions.link').click(function() {
		elgg_slide_toggle($(this), '.embed_content_section.instructions', '#embed_ecml_keyword_help');
		$('.ecml_embed_instructions').toggleClass('open');
	});
	
	$('a.ecml_embed_preview.link').click(function() {
		elgg_slide_toggle($(this), '.embed_content_section.preview', '#ecml_preview');
		$('.ecml_embed_preview').toggleClass('open');
	});
});

$(function() {
	var keywords = <?php echo $keywords_js; ?>;
	var selected_service = '';
	var manual_selected_service = false;
	var embed_button = $('#embed_submit');
	var url_status = $('#url_status');
	var embed_resource_input = $('#web_services_resource');

	// counter for paused input to try to validate/generate a preview.
	var rest_timeout_id = null;
	var rest_min_time = 750;
	var embed_generate_ecml_url = '<?php echo $vars['url']; ?>pg/ecml_generate';
	var embed_ecml_keyword_help_url = '<?php echo $vars['url']; ?>pg/ecml/';
	var internal_name = '<?php echo addslashes($internal_name); ?>';
	
	var web_services_ecml_update = function() {
		if (rest_timeout_id) {
			clearTimeout(rest_timeout_id);
		}
		
		if (manual_selected_service) {
			// fire off preview attempt
			rest_timeout_id = setTimeout(generate_ecml, rest_min_time);
			return true;
		}
		
		var value = $(this).val();
		var value_length = value.length;
		 
		if (value_length > 0) {
			//embed_button.removeAttr('disabled').removeClass('disabled');//.addClass('embed_warning');
			url_status.removeClass('success').addClass('failure');
		} else {
			embed_button.attr('disabled', 'disabled').addClass('disabled');
			url_status.removeClass('success').removeClass('failure');
		}
		
		if (value_length < 5) {
			$('.ecml_web_service a').removeClass('selected');
			return true;
		}

		// if this is ECML check that it's a valid keyword
		if (value.substr(0, 1) == '[') {
			var keyword = value.split(' ')[0];
			keyword = keyword.replace('[', '');
			keyword = keyword.replace(']', '');
			if ($.inArray(keyword, keywords) >= 0) { 
				select_service(keyword);
			}
		} else {
			// check if any of the ECML keywords exist in the
			// string and select that service.
			$(keywords).each(function(index, keyword) {
				if (value.indexOf(keyword) >= 0) {
					select_service(keyword);
					return true;
				}
			});
		}

		// fire off a preview attempt
		if (selected_service) {
			rest_timeout_id = setTimeout(generate_ecml, rest_min_time);
		}
	};

	var select_service = function(service) {
		if ($.inArray(service, keywords) === false) {
			return false;
		}
		
		selected_service = service;
		$('.ecml_web_service a').removeClass('selected');
		$('.ecml_web_service a.' + service).addClass('selected');
		$('.embed_content_section.instructions').removeClass('hidden'); // reveal instructions link/panel
	}

	// pings back core to generate the ecml.
	// includes a status, ecml code, and the generated html. 
	var generate_ecml = function() {
		if (!selected_service) {
			return false;
		}
		
		var resource = embed_resource_input.val();
		var post_data = {'service': selected_service, 'resource': resource};

		$.post(embed_generate_ecml_url, post_data, function(data) {
			if (data.status == 'success') {
				// show previews and update embed code.
				$('#ecml_preview').html(data.html);
				$('#ecml_code').html(data.ecml);
				$('body').data('elgg_embed_ecml', data.ecml);
				$('.embed_content_section.preview').removeClass('hidden'); // reveal preview link/panel
				$('.ecml_generated_code').removeClass('hidden'); // reveal ecml generated code

				// set status for embed button
				embed_button.removeAttr('disabled').removeClass('disabled');
				url_status.removeClass('failure').addClass('success');
			} else {
				// data failure
				embed_button.attr('disabled', 'disabled').addClass('disabled');
				url_status.removeClass('success').removeClass('failure');
			}
		}, 'json');
	}

	// auto guess the service.
	embed_resource_input.keyup(web_services_ecml_update);

	// manually specify the service
	$('.ecml_web_service a').click(function() {
		select_service($(this).attr('class').split(' ')[0]);
		manual_selected_service = true;

		// show tip
		var help_url = embed_ecml_keyword_help_url + selected_service + '?ajax=true';
		$('#embed_ecml_keyword_help').load(help_url);
	});
	
	$('#embed_submit').click(function() {
		// insert the ECML
		// if the ECML input is empty, insert the resource.
		if (!(content = $('body').data('elgg_embed_ecml'))) {
			// @todo display an error?
			content = embed_resource_input.val();
		}

		elggEmbedInsertContent(content, internal_name);
		
		return false;
	});
});


</script>