[Libreoffice-commits] online.git: 2 commits - loleaflet/dist loleaflet/src

Pranav Kant pranavk at collabora.co.uk
Tue Jun 13 11:52:49 UTC 2017


 loleaflet/dist/menubar.css               |    5 
 loleaflet/src/control/Control.Menubar.js |  254 +++++++++++++++++--------------
 loleaflet/src/map/handler/Map.WOPI.js    |   46 +++--
 3 files changed, 171 insertions(+), 134 deletions(-)

New commits:
commit a986238f4e3583ab67856180f5fb60333508a031
Author: Pranav Kant <pranavk at collabora.co.uk>
Date:   Tue Jun 13 15:26:44 2017 +0530

    loleaflet: Wrap find & replace inside a function
    
    For better readability in our action if-else tree.
    
    Change-Id: Id0e39ed25362da5feb70002b887b5963a49900b1

diff --git a/loleaflet/src/control/Control.Menubar.js b/loleaflet/src/control/Control.Menubar.js
index 9fe98e3f..3d2eb51f 100644
--- a/loleaflet/src/control/Control.Menubar.js
+++ b/loleaflet/src/control/Control.Menubar.js
@@ -438,22 +438,9 @@ L.Control.Menubar = L.Control.extend({
 		});
 	},
 
-	_executeAction: function(item) {
-		var id = $(item).data('id');
-		if (id === 'save') {
-			map.save(true, true);
-		} else if (id === 'print') {
-			map.print();
-		} else if (id.startsWith('downloadas-')) {
-			var format = id.substring('downloadas-'.length);
-			// remove the extension if any
-			var fileName = title.substr(0, title.lastIndexOf('.')) || title;
-			// check if it is empty
-			fileName = fileName === '' ? 'document' : fileName;
-			map.downloadAs(fileName + '.' + format, format);
-		} else if (id === 'findandreplace') {
-			var findReplaceContent =
-			'\
+	_onClickFindAndReplace: function() {
+		var findReplaceContent =
+		    '\
 			<table class="findreplacetable">\
 				<tr>\
 					<td>\
@@ -473,104 +460,121 @@ L.Control.Menubar = L.Control.extend({
 				</tr>\
 			</table>\
 			';
-			var mouseMoveFunc;
-			vex.dialog.open({
-				showCloseButton: true,
-				escapeButtonCloses: true,
-				className: 'vex-theme-plain findReplaceVex',
-				message: _('Find & Replace'),
-				input: findReplaceContent,
-				buttons: [
-					$.extend({}, vex.dialog.buttons.replace, {
-						text: _('Replace'),
-						click: function($vexContent, e) {
-							$vexContent.data().vex.option = 'replace';
-						}}),
-					$.extend({}, vex.dialog.buttons.replaceAll, {
-						text: _('Replace All'),
-						click: function($vexContent, e) {
-							$vexContent.data().vex.option = 'replaceAll';
-						}}),
-					$.extend({}, vex.dialog.buttons.findPrev, {
-						text: _('Previous'),
-						className: 'btnArrow',
-						click: function($vexContent, e) {
-							$vexContent.data().vex.option = 'previous';
-						}}),
-					$.extend({}, vex.dialog.buttons.findNext, {
-						text: _('Next'),
-						className: 'btnArrow',
-						click: function($vexContent, e) {
-							$vexContent.data().vex.option = 'next';
-						}})
-				],
-				afterOpen: function(e) {
-					$('.vex-overlay').remove();
-					$('.vex').css('position', 'static');
-					var selected = null;
-					var xPos = 0, yPos = 0;
-					var xElem = 0, yElem = 0;
-					var maxH = window.innerHeight, maxW = window.innerWidth;
-
-					$('#findthis').on('input', function() {
-						if (this.value.length != 0) {
-							map.search(this.value, false, '', 0, true);
-						}
-					});
-					$('.vex-content').on('mousedown', function(e) {
-						selected = this;
-						selected.style.cursor = 'move';
-						xElem = xPos - selected.offsetLeft;
-						yElem = yPos - selected.offsetTop;
-					});
-					$('.vex-content').on('mouseup', function(e) {
-						selected.style.cursor = 'default';
-						selected = null;
-					});
-					var mouseMoveFunc = function(e) {
-						xPos = e.pageX;
-						yPos = e.pageY;
-						if (selected !== null) {
-							var isOutVert = (yPos - yElem >= 0 && (yPos - yElem + selected.offsetHeight) <= maxH);
-							var isOutHor = (xPos - xElem >= 0 && (xPos - xElem + selected.offsetWidth) <= maxW);
-							if (isOutHor) {
-								selected.style.left = (xPos - xElem) + 'px';
-							}
-							if (isOutVert) {
-								selected.style.top = (yPos - yElem) + 'px';
-							}
-						}
-					};
-					$(document).on('mousemove', mouseMoveFunc);
-				},
-				afterClose: function(e) {
-					$(document).off('mousemove', mouseMoveFunc);
-				},
-				onSubmit: function(event) {
-					var $vexContent = $(this).parent();
-					event.preventDefault();
-					event.stopPropagation();
-
-					var opt = $vexContent.data().vex.option;
-					var findText = this.findthis.value;
-					var replaceText = this.replacewith.value;
-
-					if (findText.length != 0) {
-						if (opt === 'next') {
-							map.search(findText);
-						}
-						else if (opt === 'previous') {
-							map.search(findText, true);
-						}
-						else if (opt === 'replace') {
-							map.search(findText, false, replaceText, 2);
+		var mouseMoveFunc;
+		vex.dialog.open({
+			showCloseButton: true,
+			escapeButtonCloses: true,
+			className: 'vex-theme-plain findReplaceVex',
+			message: _('Find & Replace'),
+			input: findReplaceContent,
+			buttons: [
+				$.extend({}, vex.dialog.buttons.replace, {
+					text: _('Replace'),
+					click: function($vexContent, e) {
+						$vexContent.data().vex.option = 'replace';
+					}}),
+				$.extend({}, vex.dialog.buttons.replaceAll, {
+					text: _('Replace All'),
+					click: function($vexContent, e) {
+						$vexContent.data().vex.option = 'replaceAll';
+					}}),
+				$.extend({}, vex.dialog.buttons.findPrev, {
+					text: _('Previous'),
+					className: 'btnArrow',
+					click: function($vexContent, e) {
+						$vexContent.data().vex.option = 'previous';
+					}}),
+				$.extend({}, vex.dialog.buttons.findNext, {
+					text: _('Next'),
+					className: 'btnArrow',
+					click: function($vexContent, e) {
+						$vexContent.data().vex.option = 'next';
+					}})
+			],
+			afterOpen: function(e) {
+				$('.vex-overlay').remove();
+				$('.vex').css('position', 'static');
+				var selected = null;
+				var xPos = 0, yPos = 0;
+				var xElem = 0, yElem = 0;
+				var maxH = window.innerHeight, maxW = window.innerWidth;
+
+				$('#findthis').on('input', function() {
+					if (this.value.length != 0) {
+						map.search(this.value, false, '', 0, true);
+					}
+				});
+				$('.vex-content').on('mousedown', function(e) {
+					selected = this;
+					selected.style.cursor = 'move';
+					xElem = xPos - selected.offsetLeft;
+					yElem = yPos - selected.offsetTop;
+				});
+				$('.vex-content').on('mouseup', function(e) {
+					selected.style.cursor = 'default';
+					selected = null;
+				});
+				var mouseMoveFunc = function(e) {
+					xPos = e.pageX;
+					yPos = e.pageY;
+					if (selected !== null) {
+						var isOutVert = (yPos - yElem >= 0 && (yPos - yElem + selected.offsetHeight) <= maxH);
+						var isOutHor = (xPos - xElem >= 0 && (xPos - xElem + selected.offsetWidth) <= maxW);
+						if (isOutHor) {
+							selected.style.left = (xPos - xElem) + 'px';
 						}
-						else if (opt === 'replaceAll') {
-							map.search(findText, false, replaceText, 3);
+						if (isOutVert) {
+							selected.style.top = (yPos - yElem) + 'px';
 						}
 					}
+				};
+				$(document).on('mousemove', mouseMoveFunc);
+			},
+			afterClose: function(e) {
+				$(document).off('mousemove', mouseMoveFunc);
+			},
+			onSubmit: function(event) {
+				var $vexContent = $(this).parent();
+				event.preventDefault();
+				event.stopPropagation();
+
+				var opt = $vexContent.data().vex.option;
+				var findText = this.findthis.value;
+				var replaceText = this.replacewith.value;
+
+				if (findText.length != 0) {
+					if (opt === 'next') {
+						map.search(findText);
+					}
+					else if (opt === 'previous') {
+						map.search(findText, true);
+					}
+					else if (opt === 'replace') {
+						map.search(findText, false, replaceText, 2);
+					}
+					else if (opt === 'replaceAll') {
+						map.search(findText, false, replaceText, 3);
+					}
 				}
-			}, this);
+			}
+		}, this);
+	},
+
+	_executeAction: function(item) {
+		var id = $(item).data('id');
+		if (id === 'save') {
+			map.save(true, true);
+		} else if (id === 'print') {
+			map.print();
+		} else if (id.startsWith('downloadas-')) {
+			var format = id.substring('downloadas-'.length);
+			// remove the extension if any
+			var fileName = title.substr(0, title.lastIndexOf('.')) || title;
+			// check if it is empty
+			fileName = fileName === '' ? 'document' : fileName;
+			map.downloadAs(fileName + '.' + format, format);
+		} else if (id === 'findandreplace') {
+			this._onClickFindAndReplace();
 		} else if (id === 'insertcomment') {
 			map.insertComment();
 		} else if (id === 'insertgraphic') {
commit caa4aa12c7beeb112dbd58222bd5928326e7e101
Author: Pranav Kant <pranavk at collabora.co.uk>
Date:   Tue Jun 13 15:23:21 2017 +0530

    loleaflet: Add button to menu bar if toolbar doesn't exist
    
    ... in case of Insert_Button PostMessage API when the document is loaded
    in readonly mode.
    
    Change-Id: I65a316d0b3cf22751a6926bc7aa430a249be5244

diff --git a/loleaflet/dist/menubar.css b/loleaflet/dist/menubar.css
index eb115d77..9d57445b 100644
--- a/loleaflet/dist/menubar.css
+++ b/loleaflet/dist/menubar.css
@@ -223,7 +223,10 @@
      */
     #menu-closedocument.readonly {
 	display: block;
-	top: 3px; /* This is not a menu, plain action, but we want it to appear like a menu */
+    }
+
+    .readonly {
+	top: 3px;
     }
 }
 
diff --git a/loleaflet/src/control/Control.Menubar.js b/loleaflet/src/control/Control.Menubar.js
index e236c218..9fe98e3f 100644
--- a/loleaflet/src/control/Control.Menubar.js
+++ b/loleaflet/src/control/Control.Menubar.js
@@ -2,7 +2,7 @@
 * Control.Menubar
 */
 
-/* global $ _ map title vex revHistoryEnabled closebutton */
+/* global $ _ map title vex revHistoryEnabled closebutton L */
 L.Control.Menubar = L.Control.extend({
 	// TODO: Some mechanism to stop the need to copy duplicate menus (eg. Help)
 	options: {
@@ -299,6 +299,25 @@ L.Control.Menubar = L.Control.extend({
 		this._menubarCont = L.DomUtil.get('main-menu');
 
 		map.on('doclayerinit', this._onDocLayerInit, this);
+		map.on('addmenu', this._addMenu, this);
+	},
+
+	_addMenu: function(e) {
+		var alreadyExists = L.DomUtil.get('menu-' + e.id);
+		if (alreadyExists)
+			return;
+
+		var liItem = L.DomUtil.create('li', '');
+		liItem.id = 'menu-' + e.id;
+		if (map._permission === 'readonly') {
+			L.DomUtil.addClass(liItem, 'readonly');
+		}
+		var aItem = L.DomUtil.create('a', '', liItem);
+		$(aItem).text(e.label);
+		$(aItem).data('id', e.id);
+		$(aItem).data('type', 'action');
+		$(aItem).data('postmessage', 'true');
+		this._menubarCont.insertBefore(liItem, this._menubarCont.firstChild);
 	},
 
 	_onDocLayerInit: function() {
@@ -419,7 +438,8 @@ L.Control.Menubar = L.Control.extend({
 		});
 	},
 
-	_executeAction: function(id) {
+	_executeAction: function(item) {
+		var id = $(item).data('id');
 		if (id === 'save') {
 			map.save(true, true);
 		} else if (id === 'print') {
@@ -432,7 +452,7 @@ L.Control.Menubar = L.Control.extend({
 			fileName = fileName === '' ? 'document' : fileName;
 			map.downloadAs(fileName + '.' + format, format);
 		} else if (id === 'findandreplace') {
-			findReplaceContent =
+			var findReplaceContent =
 			'\
 			<table class="findreplacetable">\
 				<tr>\
@@ -464,7 +484,7 @@ L.Control.Menubar = L.Control.extend({
 					$.extend({}, vex.dialog.buttons.replace, {
 						text: _('Replace'),
 						click: function($vexContent, e) {
-							$vexContent.data().vex.option = 'replace'
+							$vexContent.data().vex.option = 'replace';
 						}}),
 					$.extend({}, vex.dialog.buttons.replaceAll, {
 						text: _('Replace All'),
@@ -511,8 +531,8 @@ L.Control.Menubar = L.Control.extend({
 						xPos = e.pageX;
 						yPos = e.pageY;
 						if (selected !== null) {
-							isOutVert = (yPos - yElem >= 0 && (yPos - yElem + selected.offsetHeight) <= maxH);
-							isOutHor = (xPos - xElem >= 0 && (xPos - xElem + selected.offsetWidth) <= maxW);
+							var isOutVert = (yPos - yElem >= 0 && (yPos - yElem + selected.offsetHeight) <= maxH);
+							var isOutHor = (xPos - xElem >= 0 && (xPos - xElem + selected.offsetWidth) <= maxW);
 							if (isOutHor) {
 								selected.style.left = (xPos - xElem) + 'px';
 							}
@@ -527,13 +547,13 @@ L.Control.Menubar = L.Control.extend({
 					$(document).off('mousemove', mouseMoveFunc);
 				},
 				onSubmit: function(event) {
-					$vexContent = $(this).parent();
+					var $vexContent = $(this).parent();
 					event.preventDefault();
 					event.stopPropagation();
 
-					opt = $vexContent.data().vex.option;
-					findText = this.findthis.value;
-					replaceText = this.replacewith.value;
+					var opt = $vexContent.data().vex.option;
+					var findText = this.findthis.value;
+					var replaceText = this.replacewith.value;
 
 					if (findText.length != 0) {
 						if (opt === 'next') {
@@ -638,6 +658,11 @@ L.Control.Menubar = L.Control.extend({
 			map.sendUnoCommand('.uno:AttributePageSize {"AttributePageSize.Height":{"type":"long", "value": "21590"},"AttributePageSize.Width":{"type":"long", "value": "35560"}}');
 			map.sendUnoCommand('.uno:AttributePage {"AttributePage.Landscape":{"type":"boolean", "value": "true"}}');
 		}
+
+		// Inform the host if asked
+		if ($(item).data('postmessage') === 'true') {
+			map.fire('postMessage', {msgId: 'Clicked_Button', args: {Id: id} });
+		}
 	},
 
 	_onDeleteSlide: function(e) {
@@ -653,11 +678,10 @@ L.Control.Menubar = L.Control.extend({
 			var unoCommand = $(item).data('uno');
 			map.sendUnoCommand(unoCommand);
 		} else if (type === 'action') {
-			var id = $(item).data('id');
-			self._executeAction(id);
+			self._executeAction(item);
 		}
 
-		if (id !== 'insertcomment')
+		if ($(item).data('id') !== 'insertcomment')
 			map.focus();
 	},
 
diff --git a/loleaflet/src/map/handler/Map.WOPI.js b/loleaflet/src/map/handler/Map.WOPI.js
index 6ff295d2..44f4917b 100644
--- a/loleaflet/src/map/handler/Map.WOPI.js
+++ b/loleaflet/src/map/handler/Map.WOPI.js
@@ -103,29 +103,35 @@ L.Map.WOPI = L.Handler.extend({
 		if (msg.MessageId === 'Insert_Button') {
 			if (msg.Values) {
 				if (msg.Values.id && !w2ui['toolbar-up'].get(msg.Values.id)
-				   && msg.Values.imgurl) {
-					// add the css rule for the image
-					$('html > head > style').append('.w2ui-icon.' + msg.Values.id + '{background: url(' + msg.Values.imgurl + ')}');
-
-					// add the item to the toolbar
-					w2ui['toolbar-up'].insert('save', [
+				    && msg.Values.imgurl) {
+					if (this._map._permission === 'edit') {
+						// add the css rule for the image
+						$('html > head > style').append('.w2ui-icon.' + msg.Values.id + '{background: url(' + msg.Values.imgurl + ')}');
+
+						// add the item to the toolbar
+						w2ui['toolbar-up'].insert('save', [
+							{
+								type: 'button',
+								id: msg.Values.id,
+								img: msg.Values.id,
+								hint: _(msg.Values.hint), /* "Try" to localize ! */
+								postmessage: true /* Notify the host back when button is clicked */
+							}
+						]);
+						if (msg.Values.mobile)
 						{
-							type: 'button',
-							id: msg.Values.id,
-							img: msg.Values.id,
-							hint: _(msg.Values.hint), /* "Try" to localize ! */
-							postmessage: true /* Notify the host back when button is clicked */
+							// Add to our list of items to preserve when in mobile mode
+							// FIXME: Wrap the toolbar in a class so that we don't make use
+							// global variables and functions like this
+							var idx = toolbarUpMobileItems.indexOf('save');
+							toolbarUpMobileItems.splice(idx, 0, msg.Values.id);
 						}
-					]);
-					if (msg.Values.mobile)
-					{
-						// Add to our list of items to preserve when in mobile mode
-						// FIXME: Wrap the toolbar in a class so that we don't make use
-						// global variables and functions like this
-						var idx = toolbarUpMobileItems.indexOf('save');
-						toolbarUpMobileItems.splice(idx, 0, msg.Values.id);
+						resizeToolbar();
+					}
+					else if (this._map._permission === 'readonly') {
+						// Just add a menu entry for it
+						this._map.fire('addmenu', {id: msg.Values.id, label: msg.Values.hint});
 					}
-					resizeToolbar();
 				}
 			}
 		}


More information about the Libreoffice-commits mailing list