2016-04-01 13:17:09 +00:00
/ *
*
2018-03-01 12:16:38 +00:00
* ( c ) Copyright Ascensio System Limited 2010 - 2018
2016-04-01 13:17:09 +00:00
*
* 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 Lubanas st . 125 a - 25 , Riga , Latvia ,
* EU , LV - 1021.
*
* 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
*
* /
2016-03-11 00:48:53 +00:00
define ( [
'core' ,
'spreadsheeteditor/main/app/view/FileMenuPanels' ,
'spreadsheeteditor/main/app/view/PrintSettings'
] , function ( ) {
'use strict' ;
SSE . Controllers . Print = Backbone . Controller . extend ( _ . extend ( {
views : [
'MainSettingsPrint'
] ,
initialize : function ( ) {
2016-03-31 11:13:16 +00:00
var value = Common . localStorage . getItem ( "sse-print-settings-range" ) ;
2016-04-05 11:52:34 +00:00
value = ( value !== null ) ? parseInt ( value ) : Asc . c _oAscPrintType . ActiveSheets ;
2016-04-01 13:01:12 +00:00
this . adjPrintParams = new Asc . asc _CAdjustPrint ( ) ;
2016-03-31 11:13:16 +00:00
this . adjPrintParams . asc _setPrintType ( value ) ;
2016-04-01 13:01:12 +00:00
this . _changedProps = null ;
2016-03-11 00:48:53 +00:00
this . addListeners ( {
'MainSettingsPrint' : {
'show' : _ . bind ( this . onShowMainSettingsPrint , this ) ,
'render:after' : _ . bind ( this . onAfterRender , this )
} ,
'PrintSettings' : {
'changerange' : _ . bind ( this . onChangeRange , this )
}
} ) ;
} ,
onLaunch : function ( ) {
this . printSettings = this . createView ( 'MainSettingsPrint' ) ;
} ,
onAfterRender : function ( view ) {
2016-04-01 13:01:12 +00:00
this . printSettings . cmbSheet . on ( 'selected' , _ . bind ( this . comboSheetsChange , this , this . printSettings ) ) ;
2016-03-11 00:48:53 +00:00
this . printSettings . btnOk . on ( 'click' , _ . bind ( this . querySavePrintSettings , this ) ) ;
2018-04-05 11:29:37 +00:00
Common . NotificationCenter . on ( 'print' , _ . bind ( this . openPrintSettings , this , 'print' ) ) ;
Common . NotificationCenter . on ( 'download:settings' , _ . bind ( this . openPrintSettings , this , 'download' ) ) ;
2016-04-01 13:01:12 +00:00
this . registerControlEvents ( this . printSettings ) ;
2016-03-11 00:48:53 +00:00
} ,
setApi : function ( o ) {
this . api = o ;
this . api . asc _registerCallback ( 'asc_onSheetsChanged' , _ . bind ( this . updateSheetsInfo , this ) ) ;
} ,
updateSheetsInfo : function ( ) {
if ( this . printSettings . isVisible ( ) ) {
2016-04-01 13:01:12 +00:00
this . updateSettings ( this . printSettings ) ;
2016-03-11 00:48:53 +00:00
} else {
this . isFillSheets = false ;
}
} ,
2016-04-01 13:01:12 +00:00
updateSettings : function ( panel ) {
2016-03-11 00:48:53 +00:00
var wc = this . api . asc _getWorksheetsCount ( ) , i = - 1 ;
2016-04-01 13:01:12 +00:00
var items = [ ] ;
2016-03-11 00:48:53 +00:00
while ( ++ i < wc ) {
if ( ! this . api . asc _isWorksheetHidden ( i ) ) {
items . push ( {
displayValue : this . api . asc _getWorksheetName ( i ) ,
value : i
} ) ;
}
}
2016-04-01 13:01:12 +00:00
panel . cmbSheet . store . reset ( items ) ;
var item = panel . cmbSheet . store . findWhere ( { value : panel . cmbSheet . getValue ( ) } ) ||
panel . cmbSheet . store . findWhere ( { value : this . api . asc _getActiveWorksheetIndex ( ) } ) ;
2016-03-11 00:48:53 +00:00
if ( item ) {
2016-04-01 13:01:12 +00:00
panel . cmbSheet . setValue ( item . get ( 'value' ) ) ;
2016-03-11 00:48:53 +00:00
}
} ,
2016-04-01 13:01:12 +00:00
comboSheetsChange : function ( panel , combo , record ) {
this . fillPageOptions ( panel , this . _changedProps [ record . value ] ? this . _changedProps [ record . value ] : this . api . asc _getPageOptions ( record . value ) ) ;
2016-03-11 00:48:53 +00:00
} ,
fillPageOptions : function ( panel , props ) {
var opt = props . asc _getPageSetup ( ) ;
var item = panel . cmbPaperOrientation . store . findWhere ( { value : opt . asc _getOrientation ( ) } ) ;
if ( item ) panel . cmbPaperOrientation . setValue ( item . get ( 'value' ) ) ;
var w = opt . asc _getWidth ( ) ;
var h = opt . asc _getHeight ( ) ;
item = panel . cmbPaperSize . store . findWhere ( { value : w + '|' + h } ) ;
if ( item )
panel . cmbPaperSize . setValue ( item . get ( 'value' ) ) ;
else
panel . cmbPaperSize . setValue ( 'Custom (' + w + ' x ' + h ) ;
2016-04-01 13:01:12 +00:00
var fitwidth = opt . asc _getFitToWidth ( ) ,
fitheight = opt . asc _getFitToHeight ( ) ;
if ( ! fitwidth && ! fitheight ) panel . cmbLayout . setValue ( 0 ) ;
else if ( fitwidth && fitheight ) panel . cmbLayout . setValue ( 1 ) ;
else if ( fitwidth && ! fitheight ) panel . cmbLayout . setValue ( 2 ) ;
else panel . cmbLayout . setValue ( 3 ) ;
item = panel . cmbPaperOrientation . store . findWhere ( { value : opt . asc _getOrientation ( ) } ) ;
if ( item ) panel . cmbPaperOrientation . setValue ( item . get ( 'value' ) ) ;
2016-03-11 00:48:53 +00:00
opt = props . asc _getPageMargins ( ) ;
2016-04-01 14:06:16 +00:00
panel . spnMarginLeft . setValue ( Common . Utils . Metric . fnRecalcFromMM ( opt . asc _getLeft ( ) ) , true ) ;
panel . spnMarginTop . setValue ( Common . Utils . Metric . fnRecalcFromMM ( opt . asc _getTop ( ) ) , true ) ;
panel . spnMarginRight . setValue ( Common . Utils . Metric . fnRecalcFromMM ( opt . asc _getRight ( ) ) , true ) ;
panel . spnMarginBottom . setValue ( Common . Utils . Metric . fnRecalcFromMM ( opt . asc _getBottom ( ) ) , true ) ;
2016-03-11 00:48:53 +00:00
2016-04-01 14:06:16 +00:00
panel . chPrintGrid . setValue ( props . asc _getGridLines ( ) , true ) ;
panel . chPrintRows . setValue ( props . asc _getHeadings ( ) , true ) ;
2016-03-11 00:48:53 +00:00
} ,
2016-04-01 13:01:12 +00:00
fillPrintOptions : function ( props ) {
this . printSettingsDlg . setRange ( props . asc _getPrintType ( ) ) ;
this . onChangeRange ( ) ;
2016-03-11 00:48:53 +00:00
} ,
2016-04-01 13:01:12 +00:00
onChangeRange : function ( ) {
var printtype = this . printSettingsDlg . getRange ( ) ,
store = this . printSettingsDlg . cmbSheet . store ,
2016-04-05 11:52:34 +00:00
item = ( printtype !== Asc . c _oAscPrintType . EntireWorkbook ) ? store . findWhere ( { value : this . api . asc _getActiveWorksheetIndex ( ) } ) : store . at ( 0 ) ;
2016-04-01 13:01:12 +00:00
if ( item ) {
this . printSettingsDlg . cmbSheet . setValue ( item . get ( 'value' ) ) ;
this . comboSheetsChange ( this . printSettingsDlg , this . printSettingsDlg . cmbSheet , item . toJSON ( ) ) ;
}
2016-04-05 11:52:34 +00:00
this . printSettingsDlg . cmbSheet . setDisabled ( printtype !== Asc . c _oAscPrintType . EntireWorkbook ) ;
2016-03-11 00:48:53 +00:00
} ,
getPageOptions : function ( panel ) {
var props = new Asc . asc _CPageOptions ( ) ;
props . asc _setGridLines ( panel . chPrintGrid . getValue ( ) == 'indeterminate' ? undefined : panel . chPrintGrid . getValue ( ) == 'checked' ? 1 : 0 ) ;
props . asc _setHeadings ( panel . chPrintRows . getValue ( ) == 'indeterminate' ? undefined : panel . chPrintRows . getValue ( ) == 'checked' ? 1 : 0 ) ;
var opt = new Asc . asc _CPageSetup ( ) ;
opt . asc _setOrientation ( panel . cmbPaperOrientation . getValue ( ) == '-' ? undefined : panel . cmbPaperOrientation . getValue ( ) ) ;
var pagew = /^\d{3}\.?\d*/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
var pageh = /\d{3}\.?\d*$/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
opt . asc _setWidth ( ! pagew ? undefined : parseFloat ( pagew [ 0 ] ) ) ;
opt . asc _setHeight ( ! pageh ? undefined : parseFloat ( pageh [ 0 ] ) ) ;
2016-04-01 13:01:12 +00:00
var value = panel . cmbLayout . getValue ( ) ;
opt . asc _setFitToWidth ( value == 1 || value == 2 ) ;
2016-04-01 14:06:16 +00:00
opt . asc _setFitToHeight ( value == 1 || value == 3 ) ;
2016-03-11 00:48:53 +00:00
props . asc _setPageSetup ( opt ) ;
opt = new Asc . asc _CPageMargins ( ) ;
opt . asc _setLeft ( panel . spnMarginLeft . getValue ( ) == '-' ? undefined : Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginLeft . getNumberValue ( ) ) ) ; // because 1.91*10=19.0999999...
opt . asc _setTop ( panel . spnMarginTop . getValue ( ) == '-' ? undefined : Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginTop . getNumberValue ( ) ) ) ;
opt . asc _setRight ( panel . spnMarginRight . getValue ( ) == '-' ? undefined : Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginRight . getNumberValue ( ) ) ) ;
opt . asc _setBottom ( panel . spnMarginBottom . getValue ( ) == '-' ? undefined : Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginBottom . getNumberValue ( ) ) ) ;
props . asc _setPageMargins ( opt ) ;
return props ;
} ,
2016-04-01 13:01:12 +00:00
savePageOptions : function ( panel ) {
2018-07-30 14:51:28 +00:00
this . api . asc _savePagePrintOptions ( this . _changedProps ) ;
2018-06-28 09:27:11 +00:00
Common . NotificationCenter . trigger ( 'page:settings' ) ;
2016-03-11 00:48:53 +00:00
} ,
onShowMainSettingsPrint : function ( ) {
2016-04-01 13:01:12 +00:00
this . _changedProps = [ ] ;
2016-03-11 00:48:53 +00:00
if ( ! this . isFillSheets ) {
this . isFillSheets = true ;
2016-04-01 13:01:12 +00:00
this . updateSettings ( this . printSettings ) ;
2016-03-11 00:48:53 +00:00
}
2016-04-01 13:01:12 +00:00
var item = this . printSettings . cmbSheet . store . findWhere ( { value : this . api . asc _getActiveWorksheetIndex ( ) } ) ;
if ( item ) {
this . printSettings . cmbSheet . setValue ( item . get ( 'value' ) ) ;
this . comboSheetsChange ( this . printSettings , this . printSettings . cmbSheet , item . toJSON ( ) ) ;
2016-03-11 00:48:53 +00:00
}
} ,
2018-07-18 11:07:24 +00:00
openPrintSettings : function ( type , cmp , format , asUrl ) {
2018-07-27 13:27:48 +00:00
if ( this . printSettingsDlg && this . printSettingsDlg . isVisible ( ) ) return ;
2016-04-04 08:17:21 +00:00
if ( this . api ) {
2018-04-05 11:29:37 +00:00
this . asUrl = asUrl ;
2018-07-18 11:07:24 +00:00
this . downloadFormat = format ;
2016-03-11 00:48:53 +00:00
this . printSettingsDlg = ( new SSE . Views . PrintSettings ( {
2018-04-05 11:29:37 +00:00
type : type ,
2016-03-11 00:48:53 +00:00
handler : _ . bind ( this . resultPrintSettings , this ) ,
afterrender : _ . bind ( function ( ) {
2016-04-01 13:01:12 +00:00
this . _changedProps = [ ] ;
this . updateSettings ( this . printSettingsDlg ) ;
this . printSettingsDlg . cmbSheet . on ( 'selected' , _ . bind ( this . comboSheetsChange , this , this . printSettingsDlg ) ) ;
this . fillPrintOptions ( this . adjPrintParams ) ;
this . registerControlEvents ( this . printSettingsDlg ) ;
2016-03-11 00:48:53 +00:00
} , this )
} ) ) ;
this . printSettingsDlg . show ( ) ;
}
} ,
resultPrintSettings : function ( result , value ) {
var view = SSE . getController ( 'Toolbar' ) . getView ( 'Toolbar' ) ;
if ( result == 'ok' ) {
if ( this . checkMargins ( this . printSettingsDlg ) ) {
2016-04-01 13:01:12 +00:00
this . savePageOptions ( this . printSettingsDlg ) ;
2016-03-11 00:48:53 +00:00
2016-03-31 11:13:16 +00:00
var printtype = this . printSettingsDlg . getRange ( ) ;
this . adjPrintParams . asc _setPrintType ( printtype ) ;
2018-07-30 14:51:28 +00:00
this . adjPrintParams . asc _setPageOptionsMap ( this . _changedProps ) ;
2016-03-31 11:13:16 +00:00
Common . localStorage . setItem ( "sse-print-settings-range" , printtype ) ;
2018-04-05 11:29:37 +00:00
if ( this . printSettingsDlg . type == 'print' )
this . api . asc _Print ( this . adjPrintParams , Common . Utils . isChrome || Common . Utils . isSafari || Common . Utils . isOpera ) ;
else
2018-07-18 11:07:24 +00:00
this . api . asc _DownloadAs ( this . downloadFormat , this . asUrl , this . adjPrintParams ) ;
2018-04-05 11:29:37 +00:00
Common . component . Analytics . trackEvent ( ( this . printSettingsDlg . type == 'print' ) ? 'Print' : 'DownloadAs' ) ;
Common . component . Analytics . trackEvent ( 'ToolBar' , ( this . printSettingsDlg . type == 'print' ) ? 'Print' : 'DownloadAs' ) ;
2016-04-01 13:01:12 +00:00
Common . NotificationCenter . trigger ( 'edit:complete' , view ) ;
2016-03-11 00:48:53 +00:00
} else
return true ;
2016-04-01 13:01:12 +00:00
} else
Common . NotificationCenter . trigger ( 'edit:complete' , view ) ;
2018-07-25 11:24:45 +00:00
this . printSettingsDlg = null ;
2016-03-11 00:48:53 +00:00
} ,
querySavePrintSettings : function ( ) {
if ( this . checkMargins ( this . printSettings ) ) {
2016-04-01 13:01:12 +00:00
this . savePageOptions ( this . printSettings ) ;
2016-03-11 00:48:53 +00:00
this . printSettings . applySettings ( ) ;
}
} ,
checkMargins : function ( panel ) {
2016-04-05 11:52:34 +00:00
if ( panel . cmbPaperOrientation . getValue ( ) == Asc . c _oAscPageOrientation . PagePortrait ) {
2016-03-11 00:48:53 +00:00
var pagewidth = /^\d{3}\.?\d*/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
var pageheight = /\d{3}\.?\d*$/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
} else {
pageheight = /^\d{3}\.?\d*/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
pagewidth = /\d{3}\.?\d*$/ . exec ( panel . cmbPaperSize . getValue ( ) ) ;
}
pagewidth = parseFloat ( pagewidth [ 0 ] ) ;
pageheight = parseFloat ( pageheight [ 0 ] ) ;
var ml = Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginLeft . getNumberValue ( ) ) ;
var mr = Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginRight . getNumberValue ( ) ) ;
var mt = Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginTop . getNumberValue ( ) ) ;
var mb = Common . Utils . Metric . fnRecalcToMM ( panel . spnMarginBottom . getNumberValue ( ) ) ;
var result = false ;
if ( ml > pagewidth ) result = 'left' ; else
if ( mr > pagewidth - ml ) result = 'right' ; else
if ( mt > pageheight ) result = 'top' ; else
if ( mb > pageheight - mt ) result = 'bottom' ;
if ( result ) {
Common . UI . warning ( {
title : this . textWarning ,
msg : this . warnCheckMargings ,
callback : function ( btn , text ) {
switch ( result ) {
case 'left' : panel . spnMarginLeft . $el . focus ( ) ; return ;
case 'right' : panel . spnMarginRight . $el . focus ( ) ; return ;
case 'top' : panel . spnMarginTop . $el . focus ( ) ; return ;
case 'bottom' : panel . spnMarginBottom . $el . focus ( ) ; return ;
}
}
} ) ;
return false ;
}
return true ;
} ,
2016-04-01 13:01:12 +00:00
registerControlEvents : function ( panel ) {
panel . cmbPaperSize . on ( 'selected' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . cmbPaperOrientation . on ( 'selected' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . cmbLayout . on ( 'selected' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . spnMarginTop . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . spnMarginBottom . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . spnMarginLeft . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . spnMarginRight . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . chPrintGrid . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
panel . chPrintRows . on ( 'change' , _ . bind ( this . propertyChange , this , panel ) ) ;
} ,
propertyChange : function ( panel ) {
if ( this . _changedProps ) {
this . _changedProps [ panel . cmbSheet . getValue ( ) ] = this . getPageOptions ( panel ) ;
}
} ,
2016-03-11 00:48:53 +00:00
warnCheckMargings : 'Margins are incorrect' ,
strAllSheets : 'All Sheets' ,
textWarning : 'Warning'
} , SSE . Controllers . Print || { } ) ) ;
} ) ;