Image preview

This commit is contained in:
YNH Webdev
2014-01-25 23:43:18 +01:00
parent 13349d07a8
commit 5c9edeeae8
3 changed files with 69 additions and 10 deletions

View File

@@ -15,7 +15,7 @@ inherit
end end
create create
make make, make_with_image_preview
feature {NONE} -- Initialization feature {NONE} -- Initialization
@@ -24,6 +24,12 @@ feature {NONE} -- Initialization
make_value_control ("input") make_value_control ("input")
end end
make_with_image_preview
do
make
image_preview := True
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
set_state (new_state: JSON_OBJECT) set_state (new_state: JSON_OBJECT)
@@ -37,6 +43,12 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
end end
create file.make (new_name.unescaped_string_32, new_type.unescaped_string_32, new_size.item.to_integer_32, id); create file.make (new_name.unescaped_string_32, new_type.unescaped_string_32, new_size.item.to_integer_32, id);
end end
if attached {JSON_BOOLEAN} new_state.item ("disabled") as a_disabled then
disabled := a_disabled.item
end
if attached {JSON_BOOLEAN} new_state.item ("image_preview") as a_image_preview then
image_preview := a_image_preview.item
end
end end
state: WSF_JSON_OBJECT state: WSF_JSON_OBJECT
@@ -52,6 +64,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
Result.put_string (f.id, "file_id") Result.put_string (f.id, "file_id")
end end
Result.put_boolean (disabled, "disabled") Result.put_boolean (disabled, "disabled")
Result.put_boolean (image_preview, "image_preview")
end end
feature -- Event handling feature -- Event handling
@@ -115,7 +128,7 @@ feature -- Implementation
if disabled then if disabled then
attr.append ("disabled=%"disabled%" ") attr.append ("disabled=%"disabled%" ")
end end
Result := render_tag ("", attr) Result := render_tag_with_tagname ("div", render_tag ("", attr), Void, "")
end end
feature -- Change feature -- Change
@@ -151,11 +164,22 @@ feature -- Change
file := v file := v
end end
set_image_preview (b: BOOLEAN)
do
if image_preview /= b then
image_preview := b
state_changes.replace_with_boolean (image_preview, "image_preview")
end
end
feature -- Properties feature -- Properties
disabled: BOOLEAN disabled: BOOLEAN
-- Defines if the a file is selectable and if a file can be removed once it is uploaded -- Defines if the a file is selectable and if a file can be removed once it is uploaded
image_preview: BOOLEAN
-- Preview uploaded image
file: detachable WSF_FILE file: detachable WSF_FILE
-- Text to be displayed -- Text to be displayed

View File

@@ -385,7 +385,7 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
@uploading = true @uploading = true
@$el.hide() @$el.hide()
@progressbar = $ """<div class="progress progress-striped active upload"><div rstyle="width: 10%;" class="progress-bar"></div></div>""" @progressbar = $ """<div class="progress progress-striped active upload"><div rstyle="width: 10%;" class="progress-bar"></div></div>"""
@$el.parent().append(@progressbar) @$el.parent().prepend(@progressbar)
formData = new FormData(); formData = new FormData();
action = @callback_url action = @callback_url
@@ -433,6 +433,15 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
@state['file_name'] = file.name @state['file_name'] = file.name
@state['file_type'] = file.type @state['file_type'] = file.type
@state['file_size'] = file.size @state['file_size'] = file.size
if @state['image_preview']
reader = new FileReader();
reader.readAsDataURL(@$el[0].files[0]);
self = @
reader.onload = (e)->
self.$el.parent().find("img").remove()
preview = $("""<img >""").addClass("media thumbnail")
preview[0].src = e.target.result
self.$el.parent().append(preview)
if @state['callback_change'] if @state['callback_change']
@trigger_callback(@control_name, 'change') @trigger_callback(@control_name, 'change')
@trigger('change') @trigger('change')
@@ -441,6 +450,9 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
return @$el.val() return @$el.val()
update: (state) -> update: (state) ->
if state.image_preview != undefined
@state['image_preview'] = state.image_preview
@refresh()
if state.disabled != undefined if state.disabled != undefined
@state['disabled'] = state.disabled @state['disabled'] = state.disabled
@$el.prop('disabled', state.disabled) @$el.prop('disabled', state.disabled)
@@ -463,18 +475,21 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
if @uploading if @uploading
return return
@progressbar?.remove() @progressbar?.remove()
@$el.parent().find("p").remove() @$el.parent().find("p, img").remove()
if @state['file_id'] != null if @state['file_id'] != null
@$el.hide() @$el.hide()
fname = $("""<p></p>""").addClass("form-control-static").text(@state['file_name']) fname = $("""<p></p>""").addClass("form-control-static").text(@state['file_name'])
@$el.parent().append(fname) @$el.parent().append(fname)
if @state['image_preview']
preview = $("""<img >""").attr('src', @state['file_id']).addClass("media thumbnail")
@$el.parent().append(preview)
if not @state['disabled'] if not @state['disabled']
fname.append(" "); fname.append(" ");
removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger") removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger")
self = @ self = @
removebtn.click ()-> removebtn.click ()->
self.progressbar?.remove() self.progressbar?.remove()
self.$el.parent().find("p").remove() self.$el.parent().find("p, img").remove()
self.$el.show() self.$el.show()
self.$el.val('') self.$el.val('')
self.change() self.change()

View File

@@ -628,7 +628,7 @@ WSF_FILE_CONTROL = (function(_super) {
this.uploading = true; this.uploading = true;
this.$el.hide(); this.$el.hide();
this.progressbar = $("<div class=\"progress progress-striped active upload\"><div rstyle=\"width: 10%;\" class=\"progress-bar\"></div></div>"); this.progressbar = $("<div class=\"progress progress-striped active upload\"><div rstyle=\"width: 10%;\" class=\"progress-bar\"></div></div>");
this.$el.parent().append(this.progressbar); this.$el.parent().prepend(this.progressbar);
formData = new FormData(); formData = new FormData();
action = this.callback_url({ action = this.callback_url({
control_name: this.get_full_control_name(), control_name: this.get_full_control_name(),
@@ -674,7 +674,7 @@ WSF_FILE_CONTROL = (function(_super) {
}; };
WSF_FILE_CONTROL.prototype.change = function() { WSF_FILE_CONTROL.prototype.change = function() {
var file; var file, reader, self;
this.state['file_name'] = null; this.state['file_name'] = null;
this.state['file_type'] = null; this.state['file_type'] = null;
this.state['file_size'] = null; this.state['file_size'] = null;
@@ -685,6 +685,18 @@ WSF_FILE_CONTROL = (function(_super) {
this.state['file_type'] = file.type; this.state['file_type'] = file.type;
this.state['file_size'] = file.size; this.state['file_size'] = file.size;
} }
if (this.state['image_preview']) {
reader = new FileReader();
reader.readAsDataURL(this.$el[0].files[0]);
self = this;
reader.onload = function(e) {
var preview;
self.$el.parent().find("img").remove();
preview = $("<img >").addClass("media thumbnail");
preview[0].src = e.target.result;
return self.$el.parent().append(preview);
};
}
if (this.state['callback_change']) { if (this.state['callback_change']) {
this.trigger_callback(this.control_name, 'change'); this.trigger_callback(this.control_name, 'change');
} }
@@ -696,6 +708,10 @@ WSF_FILE_CONTROL = (function(_super) {
}; };
WSF_FILE_CONTROL.prototype.update = function(state) { WSF_FILE_CONTROL.prototype.update = function(state) {
if (state.image_preview !== void 0) {
this.state['image_preview'] = state.image_preview;
this.refresh();
}
if (state.disabled !== void 0) { if (state.disabled !== void 0) {
this.state['disabled'] = state.disabled; this.state['disabled'] = state.disabled;
this.$el.prop('disabled', state.disabled); this.$el.prop('disabled', state.disabled);
@@ -723,18 +739,22 @@ WSF_FILE_CONTROL = (function(_super) {
}; };
WSF_FILE_CONTROL.prototype.refresh = function() { WSF_FILE_CONTROL.prototype.refresh = function() {
var fname, removebtn, self, _ref; var fname, preview, removebtn, self, _ref;
if (this.uploading) { if (this.uploading) {
return; return;
} }
if ((_ref = this.progressbar) != null) { if ((_ref = this.progressbar) != null) {
_ref.remove(); _ref.remove();
} }
this.$el.parent().find("p").remove(); this.$el.parent().find("p, img").remove();
if (this.state['file_id'] !== null) { if (this.state['file_id'] !== null) {
this.$el.hide(); this.$el.hide();
fname = $("<p></p>").addClass("form-control-static").text(this.state['file_name']); fname = $("<p></p>").addClass("form-control-static").text(this.state['file_name']);
this.$el.parent().append(fname); this.$el.parent().append(fname);
if (this.state['image_preview']) {
preview = $("<img >").attr('src', this.state['file_id']).addClass("media thumbnail");
this.$el.parent().append(preview);
}
if (!this.state['disabled']) { if (!this.state['disabled']) {
fname.append(" "); fname.append(" ");
removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger"); removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger");
@@ -744,7 +764,7 @@ WSF_FILE_CONTROL = (function(_super) {
if ((_ref1 = self.progressbar) != null) { if ((_ref1 = self.progressbar) != null) {
_ref1.remove(); _ref1.remove();
} }
self.$el.parent().find("p").remove(); self.$el.parent().find("p, img").remove();
self.$el.show(); self.$el.show();
self.$el.val(''); self.$el.val('');
return self.change(); return self.change();