/* * * (c) Copyright Ascensio System SIA 2010-2019 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) * version 3 as published by the Free Software Foundation. In accordance with * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect * that Ascensio System SIA expressly excludes the warranty of non-infringement * of any third-party rights. * * This program is distributed WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html * * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha * street, Riga, Latvia, EU, LV-1050. * * The interactive user interfaces in modified source and object code versions * of the Program must display Appropriate Legal Notices, as required under * Section 5 of the GNU AGPL version 3. * * Pursuant to Section 7(b) of the License you must retain the original Product * logo when distributing the program. Pursuant to Section 7(e) we decline to * grant you any rights under trademark law for use of our trademarks. * * All the Product's GUI elements, including illustrations and icon sets, as * well as technical writing content are licensed under the terms of the * Creative Commons Attribution-ShareAlike 4.0 International. See the License * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode * */ /** * ImageSettings.js * * Created by Julia Radzhabova on 2/05/14 * Copyright (c) 2018 Ascensio System SIA. All rights reserved. * */ define([ 'text!documenteditor/main/app/template/ImageSettings.template', 'jquery', 'underscore', 'backbone', 'common/main/lib/component/Button', 'common/main/lib/view/ImageFromUrlDialog', 'documenteditor/main/app/view/ImageSettingsAdvanced' ], function (menuTemplate, $, _, Backbone) { 'use strict'; DE.Views.ImageSettings = Backbone.View.extend(_.extend({ el: '#id-image-settings', // Compile our stats template template: _.template(menuTemplate), // Delegated events for creating new items, and clearing completed ones. events: { }, options: { alias: 'ImageSettings' }, initialize: function () { this._initSettings = true; this._state = { WrappingStyle: Asc.c_oAscWrapStyle2.Inline, CanBeFlow: true, Width: 0, Height: 0, FromGroup: false, DisabledControls: false, isOleObject: false, cropMode: false, isPictureControl: false }; this.lockedControls = []; this._locked = false; this._originalProps = null; this.render(); }, render: function () { var el = this.$el || $(this.el); el.html(this.template({ scope: this })); this.labelWidth = el.find('#image-label-width'); this.labelHeight = el.find('#image-label-height'); }, setApi: function(api) { this.api = api; if (this.api) { this.api.asc_registerCallback('asc_onImgWrapStyleChanged', _.bind(this._ImgWrapStyleChanged, this)); this.api.asc_registerCallback('asc_ChangeCropState', _.bind(this._changeCropState, this)); } Common.NotificationCenter.on('storage:image-insert', _.bind(this.insertImageFromStorage, this)); return this; }, setMode: function(mode) { this.mode = mode; }, updateMetricUnit: function() { var value = Common.Utils.Metric.fnRecalcFromMM(this._state.Width); this.labelWidth[0].innerHTML = this.textWidth + ': ' + value.toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); value = Common.Utils.Metric.fnRecalcFromMM(this._state.Height); this.labelHeight[0].innerHTML = this.textHeight + ': ' + value.toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); }, createDelayedControls: function() { var me = this, viewData = [ { icon: 'btn-wrap-inline', data: Asc.c_oAscWrapStyle2.Inline, tip: this.txtInline, selected: true }, { icon: 'btn-wrap-square', data: Asc.c_oAscWrapStyle2.Square, tip: this.txtSquare }, { icon: 'btn-wrap-tight', data: Asc.c_oAscWrapStyle2.Tight, tip: this.txtTight }, { icon: 'btn-wrap-through', data: Asc.c_oAscWrapStyle2.Through, tip: this.txtThrough }, { icon: 'btn-wrap-topbottom', data: Asc.c_oAscWrapStyle2.TopAndBottom, tip: this.txtTopAndBottom }, { icon: 'btn-wrap-infront', data: Asc.c_oAscWrapStyle2.InFront, tip: this.txtInFront }, { icon: 'btn-wrap-behind', data: Asc.c_oAscWrapStyle2.Behind, tip: this.txtBehind } ]; this.cmbWrapType = new Common.UI.ComboDataView({ itemWidth: 50, itemHeight: 50, menuMaxHeight: 300, enableKeyEvents: true, store: new Common.UI.DataViewStore(viewData), cls: 'combo-chart-style', dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: '-10, 0', delayRenderTips: true, itemTemplate: _.template([ '
' ].join('')) }); this.cmbWrapType.render($('#image-combo-wrap')); this.cmbWrapType.openButton.menu.cmpEl.css({ 'min-width': 178, 'max-width': 178 }); this.cmbWrapType.on('click', _.bind(this.onSelectWrap, this)); this.cmbWrapType.openButton.menu.on('show:after', function () { me.cmbWrapType.menuPicker.scroller.update({alwaysVisibleY: true}); }); this.lockedControls.push(this.cmbWrapType); this.btnOriginalSize = new Common.UI.Button({ el: $('#image-button-original-size') }); this.lockedControls.push(this.btnOriginalSize); this.btnFitMargins = new Common.UI.Button({ el: $('#image-button-fit-margins') }); this.lockedControls.push(this.btnFitMargins); var w = Math.max(this.btnOriginalSize.cmpEl.width(), this.btnFitMargins.cmpEl.width()); this.btnOriginalSize.cmpEl.width(w); this.btnFitMargins.cmpEl.width(w); this.btnEditObject = new Common.UI.Button({ el: $('#image-button-edit-object') }); this.lockedControls.push(this.btnEditObject); this.btnOriginalSize.on('click', _.bind(this.setOriginalSize, this)); this.btnEditObject.on('click', _.bind(function(btn){ if (this.api) { var oleobj = this.api.asc_canEditTableOleObject(true); if (oleobj) { var oleEditor = DE.getController('Common.Controllers.ExternalOleEditor').getView('Common.Views.ExternalOleEditor'); if (oleEditor) { oleEditor.setEditMode(true); oleEditor.show(); oleEditor.setOleData(Asc.asc_putBinaryDataToFrameFromTableOleObject(oleobj)); } } else this.api.asc_startEditCurrentOleObject(); } this.fireEvent('editcomplete', this); }, this)); this.btnFitMargins.on('click', _.bind(this.setFitMargins, this)); this.btnRotate270 = new Common.UI.Button({ parentEl: $('#image-button-270', me.$el), cls: 'btn-toolbar', iconCls: 'toolbar__icon btn-rotate-270', value: 0, hint: this.textHint270, dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'small' }); this.btnRotate270.on('click', _.bind(this.onBtnRotateClick, this)); this.lockedControls.push(this.btnRotate270); this.btnRotate90 = new Common.UI.Button({ parentEl: $('#image-button-90', me.$el), cls: 'btn-toolbar', iconCls: 'toolbar__icon btn-rotate-90', value: 1, hint: this.textHint90, dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'small' }); this.btnRotate90.on('click', _.bind(this.onBtnRotateClick, this)); this.lockedControls.push(this.btnRotate90); this.btnFlipV = new Common.UI.Button({ parentEl: $('#image-button-flipv', me.$el), cls: 'btn-toolbar', iconCls: 'toolbar__icon btn-flip-vert', value: 0, hint: this.textHintFlipV, dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'small' }); this.btnFlipV.on('click', _.bind(this.onBtnFlipClick, this)); this.lockedControls.push(this.btnFlipV); this.btnFlipH = new Common.UI.Button({ parentEl: $('#image-button-fliph', me.$el), cls: 'btn-toolbar', iconCls: 'toolbar__icon btn-flip-hor', value: 1, hint: this.textHintFlipH, dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'small' }); this.btnFlipH.on('click', _.bind(this.onBtnFlipClick, this)); this.lockedControls.push(this.btnFlipH); var w = this.btnOriginalSize.cmpEl.outerWidth(); this.btnCrop = new Common.UI.Button({ parentEl: $('#image-button-crop'), cls: 'btn-text-split-default', caption: this.textCrop, split: true, enableToggle: true, allowDepress: true, pressed: this._state.cropMode, width: w, menu : new Common.UI.Menu({ style : 'min-width:' + w + 'px;', items: [ { caption: this.textCrop, checkable: true, allowDepress: true, checked: this._state.cropMode, value: 0 }, { caption: this.textCropToShape, menu: new Common.UI.Menu({ menuAlign: 'tl-tl', cls: 'menu-shapes menu-change-shape', items: [] }) }, { caption: this.textCropFill, value: 1 }, { caption: this.textCropFit, value: 2 }] }), dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'big' }); this.btnCrop.on('click', _.bind(this.onCrop, this)); this.btnCrop.menu.on('item:click', _.bind(this.onCropMenu, this)); this.lockedControls.push(this.btnCrop); this.btnChangeShape= this.btnCrop.menu.items[1]; this.btnSelectImage = new Common.UI.Button({ parentEl: $('#image-button-replace'), cls: 'btn-text-menu-default', caption: this.textInsert, style: "width:100%;", menu: new Common.UI.Menu({ style: 'min-width: 194px;', maxHeight: 200, items: [ {caption: this.textFromFile, value: 0}, {caption: this.textFromUrl, value: 1}, {caption: this.textFromStorage, value: 2} ] }), dataHint: '1', dataHintDirection: 'bottom', dataHintOffset: 'big' }); this.lockedControls.push(this.btnSelectImage); this.btnSelectImage.menu.on('item:click', _.bind(this.onImageSelect, this)); this.btnSelectImage.menu.items[2].setVisible(this.mode.canRequestInsertImage || this.mode.fileChoiceUrl && this.mode.fileChoiceUrl.indexOf("{documentType}")>-1); this.linkAdvanced = $('#image-advanced-link'); $(this.el).on('click', '#image-advanced-link', _.bind(this.openAdvancedSettings, this)); }, _changeCropState: function(state) { this._state.cropMode = state; if (!this.btnCrop) return; this.btnCrop.toggle(state, true); this.btnCrop.menu.items[0].setChecked(state, true); }, createDelayedElements: function() { this.createDelayedControls(); this.updateMetricUnit(); this.onApiAutoShapes(); this._initSettings = false; }, onApiAutoShapes: function() { var me = this; var onShowBefore = function(menu) { me.fillAutoShapes(); menu.off('show:before', onShowBefore); }; me.btnChangeShape.menu.on('show:before', onShowBefore); }, fillAutoShapes: function() { var me = this, recents = Common.localStorage.getItem('de-recent-shapes'); var menuitem = new Common.UI.MenuItem({ template: _.template(''), index: 0 }); me.btnChangeShape.menu.addItem(menuitem); var shapePicker = new Common.UI.DataViewShape({ el: $('#id-img-change-shape-menu'), itemTemplate: _.template(''), groups: me.application.getCollection('ShapeGroups'), parentMenu: me.btnChangeShape.menu, restoreHeight: 652, textRecentlyUsed: me.textRecentlyUsed, recentShapes: recents ? JSON.parse(recents) : null, hideTextRect: true }); shapePicker.on('item:click', function(picker, item, record, e) { if (me.api) { me.api.ChangeShapeType(record.get('data').shapeType); me.fireEvent('editcomplete', me); } if (e.type !== 'click') me.btnCrop.menu.hide(); }); }, ChangeSettings: function(props) { if (this._initSettings) this.createDelayedElements(); this.disableControls(this._locked); if (props ){ this._originalProps = new Asc.asc_CImgProperty(props); var value = props.get_WrappingStyle(); if (this._state.WrappingStyle!==value) { this.cmbWrapType.suspendEvents(); var rec = this.cmbWrapType.menuPicker.store.findWhere({ data: value }); this.cmbWrapType.menuPicker.selectRecord(rec); this.cmbWrapType.resumeEvents(); this._state.WrappingStyle=value; } value = props.get_CanBeFlow() && !this._locked; var fromgroup = props.get_FromGroup() || this._locked; var control_props = this.api.asc_IsContentControl() ? this.api.asc_GetContentControlProperties() : null, isPictureControl = !!control_props && (control_props.get_SpecificType()==Asc.c_oAscContentControlSpecificType.Picture) && !control_props.get_FormPr() || this._locked; this.cmbWrapType.setDisabled(!value || fromgroup || isPictureControl); this._state.CanBeFlow=value; this._state.FromGroup=fromgroup; this._state.isPictureControl=isPictureControl; value = props.get_Width(); if ( Math.abs(this._state.Width-value)>0.001 ) { this.labelWidth[0].innerHTML = this.textWidth + ': ' + Common.Utils.Metric.fnRecalcFromMM(value).toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); this._state.Width = value; } value = props.get_Height(); if ( Math.abs(this._state.Height-value)>0.001 ) { this.labelHeight[0].innerHTML = this.textHeight + ': ' + Common.Utils.Metric.fnRecalcFromMM(value).toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); this._state.Height = value; } this.btnOriginalSize.setDisabled(props.get_ImageUrl()===null || props.get_ImageUrl()===undefined || this._locked); var pluginGuid = props.asc_getPluginGuid(); value = (pluginGuid !== null && pluginGuid !== undefined); if (this._state.isOleObject!==value) { this.btnSelectImage.setVisible(!value); this.btnEditObject.setVisible(value); this._state.isOleObject=value; } this.btnRotate270.setDisabled(value || this._locked); this.btnRotate90.setDisabled(value || this._locked); this.btnFlipV.setDisabled(value || this._locked); this.btnFlipH.setDisabled(value || this._locked); if (this._state.isOleObject) { var plugin = DE.getCollection('Common.Collections.Plugins').findWhere({guid: pluginGuid}); this.btnEditObject.setDisabled(!this.api.asc_canEditTableOleObject() && (plugin===null || plugin ===undefined) || this._locked); } else { this.btnSelectImage.setDisabled(pluginGuid===null || this._locked); } } }, _ImgWrapStyleChanged: function(style) { if (!this.cmbWrapType) return; if (this._state.WrappingStyle!==style) { this.cmbWrapType.suspendEvents(); var rec = this.cmbWrapType.menuPicker.store.findWhere({ data: style }); this.cmbWrapType.menuPicker.selectRecord(rec); this.cmbWrapType.resumeEvents(); this._state.WrappingStyle=style; } }, onSelectWrap: function(combo, record){ if (this.api) { var props = new Asc.asc_CImgProperty(), data = record.get('data'); props.put_WrappingStyle(data); if (this._state.WrappingStyle===Asc.c_oAscWrapStyle2.Inline && data!==Asc.c_oAscWrapStyle2.Inline ) { props.put_PositionH(new Asc.CImagePositionH()); props.get_PositionH().put_UseAlign(false); props.get_PositionH().put_RelativeFrom(Asc.c_oAscRelativeFromH.Column); var val = this._originalProps.get_Value_X(Asc.c_oAscRelativeFromH.Column); props.get_PositionH().put_Value(val); props.put_PositionV(new Asc.CImagePositionV()); props.get_PositionV().put_UseAlign(false); props.get_PositionV().put_RelativeFrom(Asc.c_oAscRelativeFromV.Paragraph); val = this._originalProps.get_Value_Y(Asc.c_oAscRelativeFromV.Paragraph); props.get_PositionV().put_Value(val); } this.api.ImgApply(props); } this.fireEvent('editcomplete', this); }, setOriginalSize: function() { if (this.api) { var imgsize = this.api.get_OriginalSizeImage(); var w = imgsize.get_ImageWidth(); var h = imgsize.get_ImageHeight(); this.labelWidth[0].innerHTML = this.textWidth + ': ' + Common.Utils.Metric.fnRecalcFromMM(w).toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); this.labelHeight[0].innerHTML = this.textHeight + ': ' + Common.Utils.Metric.fnRecalcFromMM(h).toFixed(2) + ' ' + Common.Utils.Metric.getCurrentMetricName(); var properties = new Asc.asc_CImgProperty(); properties.put_Width(w); properties.put_Height(h); properties.put_ResetCrop(true); properties.put_Rot(0); this.api.ImgApply(properties); this.fireEvent('editcomplete', this); } }, setFitMargins: function() { if (this.api) { var section = this.api.asc_GetSectionProps(), ratio = (this._state.Height>0) ? this._state.Width/this._state.Height : 1, pagew = (this.api.asc_GetCurrentColumnWidth) ? this.api.asc_GetCurrentColumnWidth() : (section.get_W() - section.get_LeftMargin() - section.get_RightMargin()), pageh = section.get_H() - section.get_TopMargin() - section.get_BottomMargin(), pageratio = pagew/pageh, w, h; if (ratio>pageratio) { w = pagew; h = w/ratio; } else if (ratio