web-apps/apps/spreadsheeteditor/mobile/app/controller/Settings.js
2019-04-27 16:43:10 +03:00

503 lines
24 KiB
JavaScript

/*
*
* (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
*
*/
/**
* Settings.js
*
* Created by Maxim Kadushkin on 12/05/2016
* Copyright (c) 2018 Ascensio System SIA. All rights reserved.
*
*/
define([
'core',
'spreadsheeteditor/mobile/app/view/Settings'
], function (core) {
'use strict';
SSE.Controllers.Settings = Backbone.Controller.extend(_.extend((function() {
// private
var rootView,
inProgress,
infoObj,
modalView,
_licInfo,
templateInsert,
_pageSizesIndex = 0,
_pageSizesCurrent = [0, 0],
txtCm = Common.Utils.Metric.getMetricName(Common.Utils.Metric.c_MetricUnits.cm),
_pageSizes = [
{ caption: 'US Letter', subtitle: Common.Utils.String.format('21,59{0} x 27,94{0}', txtCm), value: [215.9, 279.4] },
{ caption: 'US Legal', subtitle: Common.Utils.String.format('21,59{0} x 35,56{0}', txtCm), value: [215.9, 355.6] },
{ caption: 'A4', subtitle: Common.Utils.String.format('21{0} x 29,7{0}', txtCm), value: [210, 297] },
{ caption: 'A5', subtitle: Common.Utils.String.format('14,8{0} x 21{0}', txtCm), value: [148, 210] },
{ caption: 'B5', subtitle: Common.Utils.String.format('17,6{0} x 25{0}', txtCm), value: [176, 250] },
{ caption: 'Envelope #10', subtitle: Common.Utils.String.format('10,48{0} x 24,13{0}', txtCm), value: [104.8, 241.3] },
{ caption: 'Envelope DL', subtitle: Common.Utils.String.format('11{0} x 22{0}', txtCm), value: [110, 220] },
{ caption: 'Tabloid', subtitle: Common.Utils.String.format('27,94{0} x 43,18{0}', txtCm), value: [279.4, 431.8] },
{ caption: 'A3', subtitle: Common.Utils.String.format('29,7{0} x 42{0}', txtCm), value: [297, 420] },
{ caption: 'Tabloid Oversize', subtitle: Common.Utils.String.format('30,48{0} x 45,71{0}', txtCm), value: [304.8, 457.1] },
{ caption: 'ROC 16K', subtitle: Common.Utils.String.format('19,68{0} x 27,3{0}', txtCm), value: [196.8, 273] },
{ caption: 'Envelope Choukei 3', subtitle: Common.Utils.String.format('11,99{0} x 23,49{0}', txtCm), value: [119.9, 234.9] },
{ caption: 'Super B/A3', subtitle: Common.Utils.String.format('33,02{0} x 48,25{0}', txtCm), value: [330.2, 482.5] },
{ caption: 'A0', subtitle: Common.Utils.String.format('84,1{0} x 118,9{0}', txtCm), value: [841, 1189] },
{ caption: 'A1', subtitle: Common.Utils.String.format('59,4{0} x 84,1{0}', txtCm), value: [594, 841] },
{ caption: 'A2', subtitle: Common.Utils.String.format('42{0} x 59,4{0}', txtCm), value: [420, 594] },
{ caption: 'A6', subtitle: Common.Utils.String.format('10,5{0} x 14,8{0}', txtCm), value: [105, 148] }
],
_metricText = Common.Utils.Metric.getMetricName(Common.Utils.Metric.getCurrentMetric())
var mm2Cm = function(mm) {
return parseFloat((mm/10.).toFixed(2));
};
return {
models: [],
collections: [],
views: [
'Settings'
],
initialize: function () {
Common.NotificationCenter.on('settingscontainer:show', _.bind(this.initEvents, this));
this.addListeners({
'Settings': {
'page:show' : this.onPageShow
, 'settings:showhelp': function(e) {
window.open('{{SUPPORT_URL}}', "_blank");
this.hideModal();
}
}
});
this.localMarginProps = null;
},
setApi: function (api) {
this.api = api;
this.api.asc_registerCallback('asc_onSendThemeColorSchemes', _.bind(this.onSendThemeColorSchemes, this));
this.api.asc_registerCallback('asc_onSheetsChanged', _.bind(this.sheetChanged, this));
this.api.asc_registerCallback('asc_onUpdateSheetViewSettings', _.bind(this.sheetChanged, this));
},
onLaunch: function () {
this.createView('Settings').render();
},
setMode: function (mode) {
this.getView('Settings').setMode(mode);
if (mode.canBranding)
_licInfo = mode.customization;
},
initEvents: function () {
},
rootView : function() {
return rootView;
},
showModal: function() {
uiApp.closeModal();
if (Common.SharedSettings.get('phone')) {
modalView = uiApp.popup(
'<div class="popup settings container-settings">' +
'<div class="content-block">' +
'<div class="view settings-root-view navbar-through">' +
this.getView('Settings').rootLayout() +
'</div>' +
'</div>' +
'</div>'
);
} else {
modalView = uiApp.popover(
'<div class="popover settings container-settings">' +
'<div class="popover-angle"></div>' +
'<div class="popover-inner">' +
'<div class="content-block">' +
'<div class="view popover-view settings-root-view navbar-through">' +
this.getView('Settings').rootLayout() +
'</div>' +
'</div>' +
'</div>' +
'</div>',
$$('#toolbar-settings')
);
}
if (Framework7.prototype.device.android === true) {
$$('.view.settings-root-view.navbar-through').removeClass('navbar-through').addClass('navbar-fixed');
$$('.view.settings-root-view .navbar').prependTo('.view.settings-root-view > .pages > .page');
}
rootView = uiApp.addView('.settings-root-view', {
dynamicNavbar: true,
domCache: true
});
Common.NotificationCenter.trigger('settingscontainer:show');
this.onPageShow(this.getView('Settings'));
},
hideModal: function() {
if (modalView) {
uiApp.closeModal(modalView);
}
},
onPageShow: function(view, pageId) {
var me = this;
$('#settings-search').single('click', _.bind(me._onSearch, me));
$(modalView).find('.formats a').single('click', _.bind(me._onSaveFormat, me));
$('#settings-print').single('click', _.bind(me._onPrint, me));
me.initSettings(pageId);
},
initSettings: function (pageId) {
var me = this;
if (pageId == '#settings-about-view') {
// About
me.setLicInfo(_licInfo);
} else if ('#settings-application-view' == pageId) {
me.initPageApplicationSettings();
Common.Utils.addScrollIfNeed('.page[data-page=settings-application-view]', '.page[data-page=settings-application-view] .page-content');
} else if ('#color-schemes-view' == pageId) {
me.initPageColorSchemes();
Common.Utils.addScrollIfNeed('.page[data-page=color-schemes-view]', '.page[data-page=color-schemes-view] .page-content');
} else if ('#settings-spreadsheet-view' == pageId) {
me.initSpreadsheetSettings();
} else if ('#settings-page-size-view' == pageId) {
me.initSpreadsheetPageSize();
} else if ('#margins-view' == pageId) {
me.initSpreadsheetMargins();
}
},
initSpreadsheetSettings: function() {
var me = this,
$pageSpreadsheetSettings = $('.page[data-page=settings-spreadsheet-view]'),
$switchHideHeadings = $pageSpreadsheetSettings.find('#hide-headings input'),
$switchHideGridlines = $pageSpreadsheetSettings.find('#hide-gridlines input'),
$pageOrientation = $('.page[data-page=settings-spreadsheet-view] input:radio[name=table-orientation]');
$switchHideHeadings.single('change', _.bind(me.clickCheckboxHideHeadings, me));
$switchHideGridlines.single('change', _.bind(me.clickCheckboxHideGridlines, me));
var params = me.sheetChanged();
$switchHideHeadings.prop('checked',!params.asc_getShowRowColHeaders());
$switchHideGridlines.prop('checked',!params.asc_getShowGridLines());
// Init orientation
var currentSheet = this.api.asc_getActiveWorksheetIndex(),
props = this.api.asc_getPageOptions(currentSheet),
opt = props.asc_getPageSetup();
if(Number(opt.asc_getOrientation()) === 0) {
$('.page[data-page=settings-spreadsheet-view] input:radio[name=table-orientation][value="0"]').prop( "checked", true );
} else {
$('.page[data-page=settings-spreadsheet-view] input:radio[name=table-orientation][value="1"]').prop( "checked", true );
}
$pageOrientation.single('change', _.bind(me.onOrientationChange, me));
//Init format
var $pageSize = $('#settings-spreadsheet-format');
this.changeCurrentPageSize(opt.asc_getWidth(), opt.asc_getHeight());
$pageSize.find('.item-title').text(_pageSizes[_pageSizesIndex]['caption']);
var valueUnit = Common.localStorage.getItem('se-mobile-settings-unit');
valueUnit = (valueUnit!==null) ? parseInt(valueUnit) : Common.Utils.Metric.getDefaultMetric();
Common.Utils.Metric.setCurrentMetric(valueUnit);
var curMetricName = Common.Utils.Metric.getMetricName(Common.Utils.Metric.getCurrentMetric()),
sizeW = +(Common.Utils.Metric.fnRecalcFromMM(_pageSizes[_pageSizesIndex]['value'][0]).toFixed(2)),
sizeH = +(Common.Utils.Metric.fnRecalcFromMM(_pageSizes[_pageSizesIndex]['value'][1]).toFixed(2));
var pageSizeTxt = sizeW + ' ' + curMetricName + ' x ' + sizeH + ' ' + curMetricName;
$pageSize.find('.item-subtitle').text(pageSizeTxt);
},
changeCurrentPageSize: function(w, h) {
if (Math.abs(_pageSizesCurrent[0] - w) > 0.1 ||
Math.abs(_pageSizesCurrent[1] - h) > 0.1) {
_pageSizesCurrent = [w, h];
_.find(_pageSizes, function(size, index) {
if (Math.abs(size.value[0] - w) < 0.1 && Math.abs(size.value[1] - h) < 0.1) {
_pageSizesIndex = index;
}
}, this);
}
},
initSpreadsheetPageSize: function() {
this.getView('Settings').renderPageSizes(_pageSizes, _pageSizesIndex);
$('.page[data-page=settings-page-size-view] input:radio[name=spreadsheet-format]').single('change', _.bind(this.onFormatChange, this));
Common.Utils.addScrollIfNeed('.page[data-page=settings-page-size-view]', '.page[data-page=settings-page-size-view] .page-content');
},
onFormatChange: function(e) {
var rawValue = $(e.currentTarget).val(),
value = rawValue.split(',');
this.api.asc_changeDocSize(parseFloat(value[0]), parseFloat(value[1]), this.api.asc_getActiveWorksheetIndex());
this.initSpreadsheetSettings();
},
initSpreadsheetMargins: function() {
var me = this;
// Init page margins
var currentSheet = me.api.asc_getActiveWorksheetIndex(),
props = me.api.asc_getPageOptions(currentSheet);
me.localMarginProps = props.asc_getPageMargins();
_metricText = Common.Utils.Metric.getMetricName(Common.Utils.Metric.getCurrentMetric());
var left = +(parseFloat(Common.Utils.Metric.fnRecalcFromMM(me.localMarginProps.asc_getLeft())).toFixed(2)),
top = +(parseFloat(Common.Utils.Metric.fnRecalcFromMM(me.localMarginProps.asc_getTop())).toFixed(2)),
right = +(parseFloat(Common.Utils.Metric.fnRecalcFromMM(me.localMarginProps.asc_getRight())).toFixed(2)),
bottom = +(parseFloat(Common.Utils.Metric.fnRecalcFromMM(me.localMarginProps.asc_getBottom())).toFixed(2));
if (me.localMarginProps) {
$('#spreadsheet-margin-top .item-after label').text(top + ' ' + _metricText);
$('#spreadsheet-margin-bottom .item-after label').text(bottom + ' ' + _metricText);
$('#spreadsheet-margin-left .item-after label').text(left + ' ' + _metricText);
$('#spreadsheet-margin-right .item-after label').text(right + ' ' + _metricText);
}
_.each(["top", "left", "bottom", "right"], function(align) {
$(Common.Utils.String.format('#spreadsheet-margin-{0} .button', align)).single('click', _.bind(me.onPageMarginsChange, me, align));
})
},
onPageMarginsChange: function (align, e) {
var me = this,
$button = $(e.currentTarget),
step = 1, // mm
txtCm = Common.Utils.Metric.getMetricName(Common.Utils.Metric.c_MetricUnits.cm),
marginValue = null;
var maxMarginsH = 482.5,
maxMarginsW = 482.5;
if(Common.Utils.Metric.getCurrentMetric() == Common.Utils.Metric.c_MetricUnits.pt) {
step = 1;
} else {
step = 0.1;
}
step = Common.Utils.Metric.fnRecalcToMM(step);
switch (align) {
case 'left': marginValue = me.localMarginProps.asc_getLeft(); break;
case 'top': marginValue = me.localMarginProps.asc_getTop(); break;
case 'right': marginValue = me.localMarginProps.asc_getRight(); break;
case 'bottom': marginValue = me.localMarginProps.asc_getBottom(); break;
}
var changeProps = new Asc.asc_CPageMargins();
changeProps.asc_setTop(me.localMarginProps.asc_getTop());
changeProps.asc_setBottom(me.localMarginProps.asc_getBottom());
changeProps.asc_setLeft(me.localMarginProps.asc_getLeft());
changeProps.asc_setRight(me.localMarginProps.asc_getRight());
if ($button.hasClass('decrement')) {
marginValue = Math.max(0, marginValue - step);
} else {
marginValue = Math.min((align == 'left' || align == 'right') ? maxMarginsW : maxMarginsH, marginValue + step);
}
switch (align) {
case 'left': changeProps.asc_setLeft(marginValue); break;
case 'top': changeProps.asc_setTop(marginValue); break;
case 'right': changeProps.asc_setRight(marginValue); break;
case 'bottom': changeProps.asc_setBottom(marginValue); break;
}
$(Common.Utils.String.format('#document-margin-{0} .item-after label', align)).text(parseFloat(Common.Utils.Metric.fnRecalcFromMM(marginValue)).toFixed(2) + ' ' + _metricText);
me.api.asc_changePageMargins(changeProps.asc_getLeft(), changeProps.asc_getRight(), changeProps.asc_getTop(), changeProps.asc_getBottom(), me.api.asc_getActiveWorksheetIndex());
me.initSpreadsheetMargins();
},
onOrientationChange: function(e) {
var value = $(e.currentTarget).attr('value');
this.api.asc_changePageOrient(Number(value) === Asc.c_oAscPageOrientation.PagePortrait, this.api.asc_getActiveWorksheetIndex());
},
clickCheckboxHideHeadings: function(e) {
var $target = $(e.currentTarget),
checked = $target.prop('checked');
this.api.asc_setDisplayHeadings(!checked);
},
clickCheckboxHideGridlines: function(e) {
var $target = $(e.currentTarget),
checked = $target.prop('checked');
this.api.asc_setDisplayGridlines(!checked);
},
sheetChanged: function() {
var params = this.api.asc_getSheetViewSettings();
return(params);
},
initPageColorSchemes: function() {
$('#color-schemes-content').html(templateInsert);
$('.color-schemes-menu').on('click', _.bind(this.onColorSchemaClick, this));
},
onSendThemeColorSchemes: function (schemas) {
templateInsert = "";
_.each(schemas, function (schema, index) {
var colors = schema.get_colors();//schema.colors;
templateInsert = templateInsert + "<a class='color-schemes-menu item-link no-indicator'><input type='hidden' value='" + index + "'><div class='item-content'><div class='item-inner'><span class='color-schema-block'>";
for (var j = 2; j < 7; j++) {
var clr = '#' + Common.Utils.ThemeColor.getHexColor(colors[j].get_r(), colors[j].get_g(), colors[j].get_b());
templateInsert = templateInsert + "<span class='color' style='background: " + clr + ";'></span>"
}
templateInsert = templateInsert + "</span><span class='text'>" + schema.get_name() + "</span></div></div></a>";
}, this);
},
onColorSchemaClick: function(event) {
if (this.api) {
var ind = $(event.currentTarget).children('input').val();
this.api.asc_ChangeColorScheme(ind);
}
},
setLicInfo: function(data){
if (data && typeof data == 'object' && typeof(data.customer)=='object') {
$('.page[data-page=settings-about-view] .logo').hide();
$('#settings-about-tel').parent().hide();
$('#settings-about-licensor').show();
var customer = data.customer,
value = customer.name;
value && value.length ?
$('#settings-about-name').text(value) :
$('#settings-about-name').hide();
value = customer.address;
value && value.length ?
$('#settings-about-address').text(value) :
$('#settings-about-address').parent().hide();
(value = customer.mail) && value.length ?
$('#settings-about-email').attr('href', "mailto:"+value).text(value) :
$('#settings-about-email').parent().hide();
if ((value = customer.www) && value.length) {
var http = !/^https?:\/{2}/i.test(value) ? "http:\/\/" : '';
$('#settings-about-url').attr('href', http+value).text(value);
} else
$('#settings-about-url').hide();
if ((value = customer.info) && value.length) {
$('#settings-about-info').show().text(value);
}
if ( (value = customer.logo) && value.length ) {
$('#settings-about-logo').show().html('<img src="'+value+'" style="max-width:216px; max-height: 35px;" />');
}
}
},
initPageApplicationSettings: function() {
var me = this,
$unitMeasurement = $('.page[data-page=settings-application-view] input:radio[name=unit-of-measurement]');
$unitMeasurement.single('change', _.bind(me.unitMeasurementChange, me));
var value = Common.localStorage.getItem('se-mobile-settings-unit');
value = (value!==null) ? parseInt(value) : Common.Utils.Metric.getDefaultMetric();
$unitMeasurement.val([value]);
},
unitMeasurementChange: function (e) {
var value = $(e.currentTarget).val();
value = (value!==null) ? parseInt(value) : Common.Utils.Metric.getDefaultMetric();
Common.Utils.Metric.setCurrentMetric(value);
Common.localStorage.setItem("se-mobile-settings-unit", value);
},
// API handlers
_onSearch: function (e) {
var toolbarView = SSE.getController('Toolbar').getView('Toolbar');
if (toolbarView) {
toolbarView.showSearch();
}
this.hideModal();
},
_onPrint: function(e) {
var me = this;
_.defer(function () {
me.api.asc_Print();
});
me.hideModal();
},
_onSaveFormat: function(e) {
var me = this,
format = $(e.currentTarget).data('format');
if (format) {
if (format == Asc.c_oAscFileType.TXT) {
uiApp.confirm(
me.warnDownloadAs,
me.notcriticalErrorTitle,
function () {
me.api.asc_DownloadAs(format);
}
);
} else {
me.api.asc_DownloadAs(format);
}
me.hideModal();
}
},
notcriticalErrorTitle : 'Warning',
warnDownloadAs : 'If you continue saving in this format all features except the text will be lost.<br>Are you sure you want to continue?'
}
})(), SSE.Controllers.Settings || {}))
});