Browse Source

Template restore action on another section, one more product image generation onption

Vova Tkach 5 years ago
parent
commit
97f9607fce

+ 1 - 0
assets/sys.svg.icon.go

@@ -15,3 +15,4 @@ var SysSvgIconView = `<svg viewBox="0 0 16 16" width="16" height="16" class="sic
 var SysSvgIconBug = `<svg viewBox="4 4 18 18" width="16" height="16" class="sicon" version="1.1"><path fill-rule="evenodd" d="M20 8h-2.81c-.45-.78-1.07-1.45-1.82-1.96L17 4.41 15.59 3l-2.17 2.17C12.96 5.06 12.49 5 12 5c-.49 0-.96.06-1.41.17L8.41 3 7 4.41l1.62 1.63C7.88 6.55 7.26 7.22 6.81 8H4v2h2.09c-.05.33-.09.66-.09 1v1H4v2h2v1c0 .34.04.67.09 1H4v2h2.81c1.04 1.79 2.97 3 5.19 3s4.15-1.21 5.19-3H20v-2h-2.09c.05-.33.09-.66.09-1v-1h2v-2h-2v-1c0-.34-.04-.67-.09-1H20V8zm-6 8h-4v-2h4v2zm0-4h-4v-2h4v2z"></path></svg>`
 var SysSvgIconCopy = `<svg viewBox="0 0 24 24" width="16" height="16" class="sicon" version="1.1"><path fill="none" d="M0 0h24v24H0z"/><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm-1 4l6 6v10c0 1.1-.9 2-2 2H7.99C6.89 23 6 22.1 6 21l.01-14c0-1.1.89-2 1.99-2h7zm-1 7h5.5L14 6.5V12z" /></svg>`
 var SysSvgIconShop = `<svg viewBox="0 0 24 24" width="16" height="16" class="sicon" version="1.1"><path d="M7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zM1 2v2h2l3.6 7.59-1.35 2.45c-.16.28-.25.61-.25.96 0 1.1.9 2 2 2h12v-2H7.42c-.14 0-.25-.11-.25-.25l.03-.12.9-1.63h7.45c.75 0 1.41-.41 1.75-1.03l3.58-6.49c.08-.14.12-.31.12-.48 0-.55-.45-1-1-1H5.21l-.94-2H1zm16 16c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2z"/><path d="M0 0h24v24H0z" fill="none"/></svg>`
+var SysSvgIconRestore = `<svg viewBox="0 0 24 24" width="16" height="16" class="sicon" version="1.1"><path d="M0 0h24v24H0z" fill="none"/><path d="M13 3c-4.97 0-9 4.03-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42C8.27 19.99 10.51 21 13 21c4.97 0 9-4.03 9-9s-4.03-9-9-9zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z"/></svg>`

+ 31 - 18
image.go

@@ -2,6 +2,8 @@ package main
 
 import (
 	"fmt"
+	"image"
+	"image/color"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -14,16 +16,35 @@ import (
 	"github.com/disintegration/imaging"
 )
 
-func image_generate(width, height int, resize bool, fsrc, fdst string) {
+func image_generate(width, height int, resize int, fsrc, fdst string) {
 	src, err := imaging.Open(fsrc)
 	if err == nil {
-		if !resize {
+		if resize == 0 {
 			src = imaging.Fill(src, width, height, imaging.Center, imaging.Lanczos)
+			if err := imaging.Save(src, fdst); err != nil {
+				fmt.Printf("Image generation error (1): %v\n", err)
+			}
+		} else if resize == 1 {
+			src = imaging.Fit(src, width, height, imaging.Lanczos)
+			if err := imaging.Save(src, fdst); err != nil {
+				fmt.Printf("Image generation error (2): %v\n", err)
+			}
 		} else {
 			src = imaging.Fit(src, width, height, imaging.Lanczos)
-		}
-		if err := imaging.Save(src, fdst); err != nil {
-			fmt.Printf("Image generation error: %v\n", err)
+			dst := imaging.New(width, height, color.NRGBA{255, 255, 255, 255})
+			x := 0
+			y := 0
+			if src.Bounds().Dx() < width {
+				x = int((width - src.Bounds().Dx()) / 2)
+			}
+			if src.Bounds().Dy() < height {
+				y = int((height - src.Bounds().Dy()) / 2)
+			}
+			dst = imaging.Paste(dst, src, image.Pt(x, y))
+			if err := imaging.Save(dst, fdst); err != nil {
+				fmt.Printf("Image generation error (3): %v\n", err)
+			}
+			return
 		}
 	}
 }
@@ -31,32 +52,24 @@ func image_generate(width, height int, resize bool, fsrc, fdst string) {
 func image_create(www, src, dst, typ string, conf *config.Config) {
 	width := (*conf).Shop.Thumbnails.Thumbnail0[0]
 	height := (*conf).Shop.Thumbnails.Thumbnail0[1]
-	resize := false
+	resize := 0
 
 	if typ == "thumb-1" {
 		width = (*conf).Shop.Thumbnails.Thumbnail1[0]
 		height = (*conf).Shop.Thumbnails.Thumbnail1[1]
-		if (*conf).Shop.Thumbnails.Thumbnail1[2] == 1 {
-			resize = true
-		}
+		resize = (*conf).Shop.Thumbnails.Thumbnail1[2]
 	} else if typ == "thumb-2" {
 		width = (*conf).Shop.Thumbnails.Thumbnail2[0]
 		height = (*conf).Shop.Thumbnails.Thumbnail2[1]
-		if (*conf).Shop.Thumbnails.Thumbnail2[2] == 1 {
-			resize = true
-		}
+		resize = (*conf).Shop.Thumbnails.Thumbnail2[2]
 	} else if typ == "thumb-3" {
 		width = (*conf).Shop.Thumbnails.Thumbnail3[0]
 		height = (*conf).Shop.Thumbnails.Thumbnail3[1]
-		if (*conf).Shop.Thumbnails.Thumbnail3[2] == 1 {
-			resize = true
-		}
+		resize = (*conf).Shop.Thumbnails.Thumbnail3[2]
 	} else if typ == "thumb-full" {
 		width = (*conf).Shop.Thumbnails.ThumbnailFull[0]
 		height = (*conf).Shop.Thumbnails.ThumbnailFull[1]
-		if (*conf).Shop.Thumbnails.ThumbnailFull[2] == 1 {
-			resize = true
-		}
+		resize = (*conf).Shop.Thumbnails.ThumbnailFull[2]
 	}
 
 	image_generate(width, height, resize, src, dst)

+ 20 - 0
modules/module_settings.go

@@ -204,6 +204,11 @@ func (this *Modules) RegisterModule_Settings() *Module {
 							resize_list += ` selected`
 						}
 						resize_list += `>Resize</option>`
+						resize_list += `<option value="2"`
+						if (*wrap.Config).Shop.Thumbnails.Thumbnail1[2] == 2 {
+							resize_list += ` selected`
+						}
+						resize_list += `>Fit into size</option>`
 						resize_list += `</select>`
 						return `<div class="form-group n3">` +
 							`<div class="row">` +
@@ -246,6 +251,11 @@ func (this *Modules) RegisterModule_Settings() *Module {
 							resize_list += ` selected`
 						}
 						resize_list += `>Resize</option>`
+						resize_list += `<option value="2"`
+						if (*wrap.Config).Shop.Thumbnails.Thumbnail2[2] == 2 {
+							resize_list += ` selected`
+						}
+						resize_list += `>Fit into size</option>`
 						resize_list += `</select>`
 						return `<div class="form-group n3">` +
 							`<div class="row">` +
@@ -288,6 +298,11 @@ func (this *Modules) RegisterModule_Settings() *Module {
 							resize_list += ` selected`
 						}
 						resize_list += `>Resize</option>`
+						resize_list += `<option value="2"`
+						if (*wrap.Config).Shop.Thumbnails.Thumbnail3[2] == 2 {
+							resize_list += ` selected`
+						}
+						resize_list += `>Fit into size</option>`
 						resize_list += `</select>`
 						return `<div class="form-group n3">` +
 							`<div class="row">` +
@@ -330,6 +345,11 @@ func (this *Modules) RegisterModule_Settings() *Module {
 							resize_list += ` selected`
 						}
 						resize_list += `>Resize</option>`
+						resize_list += `<option value="2"`
+						if (*wrap.Config).Shop.Thumbnails.ThumbnailFull[2] == 2 {
+							resize_list += ` selected`
+						}
+						resize_list += `>Fit into size</option>`
 						resize_list += `</select>`
 						return `<div class="form-group n3">` +
 							`<div class="row">` +

+ 4 - 4
modules/module_settings_act_thumbnails.go

@@ -104,7 +104,7 @@ func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
 		if pfi_shop_thumbnail_h_1 > 1000 {
 			pfi_shop_thumbnail_h_1 = 1000
 		}
-		if pfi_shop_thumbnail_r_1 > 1 {
+		if pfi_shop_thumbnail_r_1 > 2 {
 			pfi_shop_thumbnail_r_1 = 1
 		}
 		if pfi_shop_thumbnail_r_1 < 0 {
@@ -117,7 +117,7 @@ func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
 		if pfi_shop_thumbnail_h_2 > 1000 {
 			pfi_shop_thumbnail_h_2 = 1000
 		}
-		if pfi_shop_thumbnail_r_2 > 1 {
+		if pfi_shop_thumbnail_r_2 > 2 {
 			pfi_shop_thumbnail_r_2 = 1
 		}
 		if pfi_shop_thumbnail_r_2 < 0 {
@@ -130,7 +130,7 @@ func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
 		if pfi_shop_thumbnail_h_3 > 1000 {
 			pfi_shop_thumbnail_h_3 = 1000
 		}
-		if pfi_shop_thumbnail_r_3 > 1 {
+		if pfi_shop_thumbnail_r_3 > 2 {
 			pfi_shop_thumbnail_r_3 = 1
 		}
 		if pfi_shop_thumbnail_r_3 < 0 {
@@ -143,7 +143,7 @@ func (this *Modules) RegisterAction_SettingsThumbnails() *Action {
 		if pfi_shop_thumbnail_h_full > 1000 {
 			pfi_shop_thumbnail_h_full = 1000
 		}
-		if pfi_shop_thumbnail_r_full > 1 {
+		if pfi_shop_thumbnail_r_full > 2 {
 			pfi_shop_thumbnail_r_full = 1
 		}
 		if pfi_shop_thumbnail_r_full < 0 {

+ 24 - 4
modules/module_template.go

@@ -42,6 +42,7 @@ func (this *Modules) RegisterModule_Template() *Module {
 		Icon:   assets.SysSvgIconView,
 		Sub: &[]MISub{
 			{Mount: "default", Name: "Editor", Show: true, Icon: assets.SysSvgIconEdit},
+			{Mount: "restore", Name: "Restore", Show: true, Icon: assets.SysSvgIconRestore},
 		},
 	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
 		content := ""
@@ -50,6 +51,7 @@ func (this *Modules) RegisterModule_Template() *Module {
 			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
 				{Name: "Editor"},
 			})
+
 			files := this.template_GetThemeFiles(wrap)
 			if len(files) > 0 {
 				selected_file, _ := url.QueryUnescape(wrap.R.URL.Query().Get("file"))
@@ -89,7 +91,7 @@ func (this *Modules) RegisterModule_Template() *Module {
 						CallBack: func(field *builder.DataFormField) string {
 							return `<div class="form-group n1">` +
 								`<div class="row">` +
-								`<div class="col-7">` +
+								`<div class="col-12">` +
 								`<div style="position:relative;">` +
 								`<button type="button" class="btn btn-success" onclick="return fave.ActionRestoreThemeFile('template-restore-file','` + selected_file + `','Are you sure want to restore theme file?');" style="position:absolute;right:0;">Restore</button>` +
 								`<select class="form-control ignore-lost-data" id="lbl_file" name="file" onchange="setTimeout(function(){$('#lbl_file').val('` + selected_file + `')},500);document.location='/cp/` + wrap.CurrModule + `/?file='+encodeURI(this.value);">` +
@@ -97,9 +99,6 @@ func (this *Modules) RegisterModule_Template() *Module {
 								`</select>` +
 								`</div>` +
 								`</div>` +
-								`<div class="col-5">` +
-								`<button type="button" class="btn btn-danger" onclick="return fave.ActionRestoreThemeFile('template-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');">Restore All</button>` +
-								`</div>` +
 								`</div>` +
 								`</div>`
 						},
@@ -126,6 +125,27 @@ func (this *Modules) RegisterModule_Template() *Module {
 					</div>
 				</div>`
 			}
+		} else if wrap.CurrSubModule == "restore" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "Restore"},
+			})
+
+			content += builder.DataForm(wrap, []builder.DataFormField{
+				{
+					Kind: builder.DFKText,
+					CallBack: func(field *builder.DataFormField) string {
+						return `<div class="form-group last"><div class="row"><div class="col-12"><div class="alert alert-danger" style="margin:0;"><strong>WARNING!</strong> This action will restore current theme files to original, you will lost you theme changes! Think twice before run this action! If you still want to do this, please press <b>Restore</b> red button!</div></div></div></div>`
+					},
+				},
+				{
+					Kind: builder.DFKSubmit,
+					CallBack: func(field *builder.DataFormField) string {
+						return `<div class="row d-lg-none"><div class="col-12"><div class="pt-3"><button type="button" class="btn btn-danger" onclick="return fave.ActionRestoreThemeFile('template-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');">Restore</button></div></div></div>`
+					},
+				},
+			})
+
+			sidebar += `<button class="btn btn-danger btn-sidebar" onclick="return fave.ActionRestoreThemeFile('template-restore-file-all','all','WARNING! Are you sure want to restore all theme files?');" id="add-edit-button">Restore</button>`
 		}
 		return this.getSidebarModules(wrap), content, sidebar
 	})