Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2d166a4fb6
Fetching contributors…

Cannot retrieve contributors at this time

file 156 lines (141 sloc) 4.001 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
package media

import (
"fmt"

// "github.com/ungerik/go-start/debug"
"github.com/ungerik/go-start/model"
"github.com/ungerik/go-start/view"
)

type ImageRefController struct {
view.SetModelValueControllerBase
}

func (self ImageRefController) Supports(metaData *model.MetaData, form *view.Form) bool {
_, ok := metaData.Value.Addr().Interface().(*ImageRef)
return ok
}

func (self ImageRefController) NewInput(withLabel bool, metaData *model.MetaData, form *view.Form) (input view.View, err error) {
imageRef := metaData.Value.Addr().Interface().(*ImageRef)
thumbnailSize := Config.ImageRefController.ThumbnailSize

var img view.View
if imageRef.IsEmpty() {
img = view.IMG(Config.dummyImageURL, thumbnailSize, thumbnailSize)
} else {
image, found, err := imageRef.TryGet()
if err != nil {
return nil, err
}
if found {
version, err := image.Thumbnail(thumbnailSize)
if err != nil {
return nil, err
}
img = version.View("")
} else {
imageRef.Set(nil)
img = view.IMG(Config.dummyImageURL, thumbnailSize, thumbnailSize)
}
}

hiddenInput := &view.HiddenInput{Name: metaData.Selector(), Value: imageRef.String()}

thumbnailFrame := &view.Div{
Class: Config.ImageRefController.ThumbnailFrameClass,
Style: fmt.Sprintf("width:%dpx;height:%dpx;", thumbnailSize, thumbnailSize),
Content: img,
}

uploadList := &view.List{Class: "qq-upload-list"}

removeButton := &view.Div{
Class: "qq-upload-button",
Content: view.HTML("Remove"),
OnClick: fmt.Sprintf(
`jQuery("#%s").attr("value", "");
jQuery("#%s").empty().append("<img src='%s' width='%d' height='%d'/>");
jQuery("#%s").empty();`,
hiddenInput.ID(),
thumbnailFrame.ID(),
Config.dummyImageURL,
thumbnailSize,
thumbnailSize,
uploadList.ID(),
),
}

chooseDialogThumbnails := view.DIV("")
chooseDialogThumbnailsID := chooseDialogThumbnails.ID()
chooseDialog := &view.ModalDialog{
Style: "width:600px;height:400px;",
Content: view.Views{
view.H3("Choose Image:"),
chooseDialogThumbnails,
view.ModalDialogCloseButton("Close"),
},
}

chooseButton := view.DynamicView(
func(ctx *view.Context) (view.View, error) {
return &view.Div{
Class: "qq-upload-button",
Content: view.HTML("Choose existing"),
OnClick: fmt.Sprintf(
`gostart_media.fillChooser('#%s', '%s', function(value){
jQuery('#%s').attr('value', value.id);
var img = '<img src=\"'+value.url+'\" alt=\"'+value.title+'\"/>';
jQuery('#%s').empty().append(img);
%s
});
%s;`,
chooseDialogThumbnailsID,
AllThumbnailsAPI.URL(ctx.ForURLArgsConvert(Config.ImageRefController.ThumbnailSize)),
hiddenInput.ID(),
thumbnailFrame.ID(),
view.ModalDialogCloseScript,
chooseDialog.OpenScript(),
),
}, nil
},
)

uploadButton := view.DIV("")
uploadButtonID := uploadButton.ID()
uploadButton.Content = UploadImageButton(
"#"+uploadButtonID,
"#"+thumbnailFrame.ID(),
"#"+uploadList.ID(),
thumbnailSize,
fmt.Sprintf(
`function(id, fileName, responseJSON) {
var img = "<img src='" + responseJSON.thumbnailURL + "' width='%d' height='%d'/>";
jQuery("#%s").empty().html(img);
jQuery("#%s").attr("value", responseJSON.imageID);
}`,
thumbnailSize,
thumbnailSize,
thumbnailFrame.ID(),
hiddenInput.ID(),
),
)

editor := view.DIV(Config.ImageRefController.Class,
view.RequireScriptURL("/media/media.js", 0),
view.RequireStyle(
`.qq-upload-button {
margin: 0 0 5px 10px;
cursor: pointer;
}
.qq-upload-button:hover {
background-color: #cc0000;
}`,
10,
),
chooseDialog,
hiddenInput,
thumbnailFrame,
&view.Div{
Class: Config.ImageRefController.ActionsClass,
Style: fmt.Sprintf("margin-left: %dpx", thumbnailSize+10),
Content: view.Views{
removeButton,
chooseButton,
uploadButton,
},
},
uploadList,
)

if withLabel {
return view.AddStandardLabel(form, editor, metaData), nil
}
return editor, nil
}
Something went wrong with that request. Please try again.