preview-controls-js.hbs 11.4 KB
Newer Older
1
/* globals window,$,document */
2 3 4 5
if(window.self !== window.top && window.top.CQ){
	window.top.location.href = window.location.href;
}

Caleb Weeks's avatar
Caleb Weeks committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
	wrapping everything in this content loaded listener to
	account for the possibility that this JS is included before
	jQuery is included on the same page. In that cases we've got
	some conflicting issues.
*/
document.addEventListener('DOMContentLoaded', function() {

	window.BL = window.BL || {};
	window.BL.appsMount="{{appsMount}}";
	window.BL.proxyPrefix="{{blConfig.slingProxyPrefix}}";
	window.BL.isFoss=!!("{{isFoss}}");
	window.BL.author="{{author}}";


	window.BL.blacklightPreviewControls = function() {
			"use strict";

			//check for preview-controls.css. If not present, load it
			var hideBulb=false;
			if(window.self !== window.top && window.top.BL){
					hideBulb=true;
			}

30
			var showAnnotations = window.BL.utils.getCookie('bl-show-annotations');
Caleb Weeks's avatar
Caleb Weeks committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
			var $img=hideBulb?$("<span></span>"):$(
			"<div id='bl-preview-actions' class='bl-preview " + (showAnnotations ? "showing-annotations" : "") +  "'><div>" +
				"<a data-bl-tooltip='Edit Page' ><img id='bl-light-bulb' draggable='true' src='{{publicMount}}blacklight/edit/images/blacklight-bulb-white.svg'></a>" +
				"<ul>" +
					"<li class='show-annotations' data-bl-tooltip='Show Annotations'><a class='btn-floating blue'><i class='material-icons'>chat</i></a></li>" +
					"<li data-bl-tooltip='Hide Annotations'><a class='hide-annotations btn-floating blue' ><i class='material-icons'>chat_bubble_outline</i></a></li>" +
				"</ul>" +
			"</div></div>");
			$("body").append($img);

			$img.find('img').on("dragstart", function(evt){
					$img.css("display", "none");
					setTimeout(function(){$img.css("display","block")}, 30000);
			}).on("click", function(evt){
					switchToEditMode()
		})

			$img.find('.show-annotations a').click(function(e){
				localShowAnnotations();
				e.preventDefault();
			});
			$img.find('.hide-annotations').click(function(e){
				$img.removeClass('showing-annotations');
54
				window.BL.utils.deleteCookie('bl-show-annotations');
Caleb Weeks's avatar
Caleb Weeks committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
				window.BL.annotations.hideAnnotations()
				e.preventDefault();
			});

			$(".bl-selector-replace").click(function(){
					var desiredSelector = $(this).data("bl-selector");
					document.location.assign(replaceSelector(document.location.href, desiredSelector));
			})

			polyfillReplaceHash();
			setupEditOverlays($img);

			//////////////////////////////////////////////////////////////////////////////////////////////////////////
			function replaceSelector(url, desiredSelector){
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
				var mainParts = url.split(/(?=[?#])/g);
				var baseUrl = mainParts[0];

				var parts= baseUrl.split("/");
				var name = parts.pop();
				if(!name){
					name=parts.pop();
				}

				var bits = name.split(".");
				var base = bits.shift();
				var extension = bits.pop() || "html";
				parts.push(base + "." + desiredSelector + (desiredSelector?".":"") + extension);
				var fullPath = parts.join("/");

				//add back the hash and query if they exist.
85 86 87 88
				var addOns = mainParts.slice(1);
				if(addOns) {
					fullPath = fullPath + addOns.join("");
				}
89
				return fullPath;
Caleb Weeks's avatar
Caleb Weeks committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			}

			window.BL.replaceSelector = replaceSelector;

			///////////////////////////////////////////////////////////////////////////
			function polyfillReplaceHash(){
					if ('replaceState' in window.history) { // Yay, supported!
						window.replaceHash = function(newhash) {
								if ((''+newhash).charAt(0) !== '#') {newhash = '#' + newhash;}
								window.history.replaceState('', '', newhash);
						}
					} else {
						var hash = window.document.location.hash;
						window.replaceHash = function(newhash) {
								if (window.document.location.hash !== hash) {window.history.back();}
								window.document.location.hash = newhash;
						};
					}
			}

			//////////////////////////////////////////////////////////////////////////////////////////////////////////
			function switchToEditMode(){
					var isProxied = !("{{blConfig.slingProxyPrefix}}");
					if(isProxied){
114
							window.BL.utils.setCookie("wcmmode", "edit", 7);
Caleb Weeks's avatar
Caleb Weeks committed
115
							window.location.assign(document.location.pathname + document.location.hash);
116
							window.location.reload(true);
Caleb Weeks's avatar
Caleb Weeks committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
					}else{
							var editUrl=document.location.origin + "{{appsMount}}blacklight/edit/page" + document.location.pathname + document.location.hash;
							window.location.href=editUrl;
					}
			}
			window.BL.switchToEditMode = switchToEditMode;

			//////////////////////////////////////////////////////////////////////////////////////////////////////////
			function setupEditOverlays($img){

					var $document=$(document);
					var $allEditables=$("[data-bl-edit]");
					var $lastHighlighted;
					var lastKeyWasAltKey=false;
					var $overlay=$("<div id='bl-overlay' style='z-index:4400;cursor:context-menu;opacity:0.2;background-color:#39F;width:0;height:0;position:absolute;left:0;top:0'></div>")
					var $contextMenu = $("<ul id='bl-context-menu'><li id='bl-add-annotation'><i class='material-icons'>chat</i>Add Annotation</li></ul>");
					$document.find("body").append($overlay);
					$document.find("body").append($contextMenu);

					//////////////////////////////////////////////////////////////
					$overlay.click(function(e){
							var config=$overlay.data("bl-edit");
							window.replaceHash("#!" + config.slingPath.replace(/^\//,""));
							switchToEditMode();
Caleb Weeks's avatar
wip  
Caleb Weeks committed
141 142
					});

Caleb Weeks's avatar
Caleb Weeks committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
					$overlay.contextmenu(function (event) {
						// Avoid the real one
						event.preventDefault();

						// Show contextmenu in the right position (the mouse)
						$contextMenu.show().css({
							top: event.pageY + "px",
							left: event.pageX + "px"
						});
					});

					// If the document is clicked somewhere
					$(document).bind("mousedown", function (e) {
						// If the clicked element is not the menu
						if (!$(e.target).parents('#bl-context-menu').length > 0) {
							// Hide it
							$contextMenu.hide();
						}
					});

					$contextMenu.find('#bl-add-annotation').click(function(e){
						console.log('add annotation');
Caleb Weeks's avatar
wip  
Caleb Weeks committed
165
						$contextMenu.hide();
Caleb Weeks's avatar
Caleb Weeks committed
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
						//can't add annotations if annotations aren't loaded
						localShowAnnotations(function(){
							var path = document.location.pathname + '/jcr:content' + $overlay.data("bl-edit").slingPath;
							var annotationData =  {x: e.pageX, y: e.pageY, width: 150, height: 100, color: '#89fff9'};
							window.BL.annotations.create(path, annotationData, function(err, path){
								if(err){console.log('err')}else{
									var $newContainer = $('<div></div>', {css: {position:'absolute', top: 0, left: 0}});
									window.BL.annotations.renderAnnotation($newContainer, annotationData, path);
									$('body').append($newContainer);
								}
							});
						});
					});

					$allEditables.addClass("bl-edit");


					//////////////////////////////////////////////////////////////
					function hightlightIfOverEditable(e){
							if(e && e.pageX){
									var x=e.pageX-window.pageXOffset, y=e.pageY-window.pageYOffset;
									var nowOver = document.elementsFromPoint(x, y);
									var found, cur;
									for(var i=0; i < nowOver.length && !found; i++){
											cur=nowOver[i];
											if(cur.attributes && cur.attributes["data-bl-edit"]){
													found=$(cur);
											}
									}

									if(found && (!$lastHighlighted || !$lastHighlighted.is(found))){
											var $el=found;
											var offset=$el.offset();
											var styles={width:$el.outerWidth(), height:$el.outerHeight(), top:offset.top, left:offset.left, display: "block"};
											$overlay.css(styles);
											$overlay.data("bl-edit", $el.data("bl-edit"));
											$lastHighlighted=$el;
									}else{
											if(!found && $lastHighlighted){
													$overlay.css({height:0, width:0});
													$contextMenu.hide();
													$lastHighlighted=null;
											}
									}
							}
Caleb Weeks's avatar
wip  
Caleb Weeks committed
211
					}
Caleb Weeks's avatar
Caleb Weeks committed
212 213 214 215 216 217


					//////////////////////////////////////////////////////////////
					$document.keydown(function (e) {
							if(e.altKey) {
									window.BL.triggerEditMode();
218 219
							}
					});
Caleb Weeks's avatar
Caleb Weeks committed
220 221 222 223 224 225 226 227 228 229 230

					//////////////////////////////////////////////////////////////
					$document.keyup(function (e) {
							if(lastKeyWasAltKey) {
									$document.off("mousemove.bl-edit");
									$overlay.css({height:0, width:0});
									$contextMenu.hide();
									lastKeyWasAltKey=false;
									$lastHighlighted=null;
									$img.show()
							}
231
					});
Caleb Weeks's avatar
Caleb Weeks committed
232 233 234 235 236 237 238 239 240

					window.BL.triggerEditMode = function(){
							lastKeyWasAltKey=true;
							$document.on("mousemove.bl-edit", function(e){
									hightlightIfOverEditable(e);
							});
							$img.hide();
					}

241
			}
Caleb Weeks's avatar
wip  
Caleb Weeks committed
242

Caleb Weeks's avatar
Caleb Weeks committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
			///////////////////////////
			function localShowAnnotations(cb){
				if(window.BL.annotations){
					//the JS and annotations have already been loaded
					window.BL.annotations.showAnnotations();
					if(cb){cb();}
				}else{
					//need to load the annotations.js and get the annotations
					window.BL.loadLibrary('{{publicMount}}blacklight/edit/js/annotations.js', function(){
						window.BL.annotations.getAnnotations(function(annotations){
							window.BL.annotations.renderAnnotations(annotations);
							if(cb){cb();}
						});
					});
				}
				$img.addClass('showing-annotations');
259
				window.BL.utils.setCookie('bl-show-annotations', true);
Caleb Weeks's avatar
Caleb Weeks committed
260
			}
Caleb Weeks's avatar
wip  
Caleb Weeks committed
261

Caleb Weeks's avatar
Caleb Weeks committed
262 263
			//load the annotations if cookie value set
			if(showAnnotations){localShowAnnotations()}
264

Caleb Weeks's avatar
Caleb Weeks committed
265
			var $blActions = $('#bl-preview-actions');
Caleb Weeks's avatar
wip  
Caleb Weeks committed
266

Caleb Weeks's avatar
Caleb Weeks committed
267 268 269 270 271 272 273
			// Hover behaviour: make sure this doesn't work on .click-to-toggle FABs!
			$(document).on('mouseenter','#bl-light-bulb, #bl-preview-actions, #bl-preview-actions ul', function(e) {
				$blActions.addClass('active');
			});
			$(document).on('mouseleave', '#bl-preview-actions,  #bl-preview-actions ul', function(e) {
				$blActions.removeClass('active');
			});
274 275


Caleb Weeks's avatar
Caleb Weeks committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	}

	window.BL.librariesToLoad = [];
	window.BL.loadLibrary = function(addToLoad, cb){
		if(addToLoad || window.BL.librariesToLoad.length){
			var library = addToLoad || window.BL.librariesToLoad.shift() ;
			var headTag = document.getElementsByTagName("head")[0];

			if(library.endsWith('.js')){
				var jqTag = document.createElement('script');
				jqTag.type = 'text/javascript';
				jqTag.src = library;
				jqTag.onload = cb || function(){window.BL.loadLibrary()};
				headTag.appendChild(jqTag);
			}else if(library.endsWith('.css')){
				var link  = document.createElement('link');
				link.rel  = 'stylesheet';
				link.type = 'text/css';
				link.href = library;
				link.media = 'all';
				link.onload = onload = cb || function(){window.BL.loadLibrary()};
				headTag.appendChild(link);
			}else{
				throw 'Undefined library type received: ' + library;
			}
301
		}else{
Caleb Weeks's avatar
Caleb Weeks committed
302
			window.BL.blacklightPreviewControls();
303
		}
Caleb Weeks's avatar
wip  
Caleb Weeks committed
304
	}
305

Caleb Weeks's avatar
Caleb Weeks committed
306
	//////////////////////////////////////////////////////////////////////////////////////////////////////////
307

Caleb Weeks's avatar
Caleb Weeks committed
308 309 310
	if(!document.querySelector('link[href$="preview-controls.css"]')){
		window.BL.librariesToLoad.push('{{publicMount}}blacklight/edit/css/preview-controls.css');
	}
311 312


313 314


Caleb Weeks's avatar
Caleb Weeks committed
315 316
	if(typeof jQuery==='undefined') {
		window.BL.librariesToLoad.push('//code.jquery.com/jquery-1.12.0.min.js');
Corey Jepperson's avatar
Corey Jepperson committed
317
		window.BL.librariesToLoad.push('{{publicMount}}fshr/shared/js/jquery-xss-protect.js');
Caleb Weeks's avatar
Caleb Weeks committed
318 319 320 321 322
		window.BL.librariesToLoad.push('//code.jquery.com/ui/1.12.1/jquery-ui.min.js');
	}else if(!jQuery.ui){
		window.BL.librariesToLoad.push('//code.jquery.com/ui/1.12.1/jquery-ui.min.js');
	}

323 324
	window.BL.librariesToLoad.push('{{publicMount}}blacklight/edit/js/utils.js');

Caleb Weeks's avatar
Caleb Weeks committed
325 326 327
	if(!window.BL.isFoss){
		window.BL.librariesToLoad.push(window.BL.proxyPrefix + '/etc/clientlibs/granite/jquery/granite/csrf.js');
	}
328

Caleb Weeks's avatar
wip  
Caleb Weeks committed
329

Caleb Weeks's avatar
Caleb Weeks committed
330
	window.BL.loadLibrary();
Caleb Weeks's avatar
wip  
Caleb Weeks committed
331

Caleb Weeks's avatar
Caleb Weeks committed
332
}, false);