Allow remove
This commit is contained in:
@@ -19,8 +19,9 @@ create
|
|||||||
|
|
||||||
feature {NONE} -- Initialization
|
feature {NONE} -- Initialization
|
||||||
|
|
||||||
make
|
make (a_removable: BOOLEAN)
|
||||||
do
|
do
|
||||||
|
removable := a_removable
|
||||||
make_value_control ("input")
|
make_value_control ("input")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -28,9 +29,14 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
|
|||||||
|
|
||||||
set_state (new_state: JSON_OBJECT)
|
set_state (new_state: JSON_OBJECT)
|
||||||
-- Restore text from json
|
-- Restore text from json
|
||||||
|
local
|
||||||
|
id: detachable STRING
|
||||||
do
|
do
|
||||||
if attached {JSON_STRING} new_state.item ("file") as new_name and attached {JSON_STRING} new_state.item ("type") as new_type and attached {JSON_NUMBER} new_state.item ("size") as new_size then
|
if attached {JSON_STRING} new_state.item ("file_name") as new_name and attached {JSON_STRING} new_state.item ("file_type") as new_type and attached {JSON_NUMBER} new_state.item ("file_size") as new_size then
|
||||||
create file.make (new_name.unescaped_string_32, new_type.unescaped_string_32, new_size.item.to_integer_32, VOID);
|
if attached {JSON_STRING} new_state.item ("file_id") as a_id then
|
||||||
|
id := a_id.unescaped_string_32
|
||||||
|
end
|
||||||
|
create file.make (new_name.unescaped_string_32, new_type.unescaped_string_32, new_size.item.to_integer_32, id);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -45,6 +51,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
|
|||||||
Result.put_integer (f.size, "file_size")
|
Result.put_integer (f.size, "file_size")
|
||||||
Result.put_string (f.id, "file_id")
|
Result.put_string (f.id, "file_id")
|
||||||
end
|
end
|
||||||
|
Result.put_boolean (removable, "removable")
|
||||||
end
|
end
|
||||||
|
|
||||||
feature -- Event handling
|
feature -- Event handling
|
||||||
@@ -55,6 +62,12 @@ feature -- Event handling
|
|||||||
change_event := e
|
change_event := e
|
||||||
end
|
end
|
||||||
|
|
||||||
|
set_upload_done_event (e: attached like upload_done_event)
|
||||||
|
-- Set text change event handle
|
||||||
|
do
|
||||||
|
upload_done_event := e
|
||||||
|
end
|
||||||
|
|
||||||
set_upload_function (e: attached like upload_function)
|
set_upload_function (e: attached like upload_function)
|
||||||
-- Set button click event handle
|
-- Set button click event handle
|
||||||
do
|
do
|
||||||
@@ -63,18 +76,28 @@ feature -- Event handling
|
|||||||
|
|
||||||
handle_callback (cname: LIST [STRING]; event: STRING; event_parameter: detachable ANY)
|
handle_callback (cname: LIST [STRING]; event: STRING; event_parameter: detachable ANY)
|
||||||
local
|
local
|
||||||
a_file: WSF_FILE
|
f_name: detachable STRING
|
||||||
|
f_type: detachable STRING
|
||||||
|
f_size: detachable INTEGER
|
||||||
|
f_id: detachable STRING
|
||||||
do
|
do
|
||||||
if Current.control_name.same_string (cname [1]) then
|
if Current.control_name.same_string (cname [1]) then
|
||||||
if attached change_event as cevent and event.same_string ("change") then
|
if attached change_event as cevent and event.same_string ("change") then
|
||||||
cevent.call (Void)
|
cevent.call (Void)
|
||||||
elseif attached upload_function as ufunction and event.same_string ("uploadfile") and attached {ITERABLE [WSF_UPLOADED_FILE]} event_parameter as files then
|
elseif event.same_string ("uploadfile") and attached {ITERABLE [WSF_UPLOADED_FILE]} event_parameter as files then
|
||||||
if attached file as f then
|
if attached file as f then
|
||||||
a_file := f
|
if attached upload_function as ufunction then
|
||||||
else
|
f.set_id (ufunction.item ([files]))
|
||||||
create a_file.make ("", "", 0, VOID)
|
end
|
||||||
|
f_name := f.name
|
||||||
|
f_type := f.type
|
||||||
|
f_size := f.size
|
||||||
|
f_id := f.id
|
||||||
end
|
end
|
||||||
a_file.set_id (ufunction.item ([files]))
|
state_changes.replace_with_string (f_name, "file_name")
|
||||||
|
state_changes.replace_with_string (f_type, "file_type")
|
||||||
|
state_changes.replace_with_integer (f_size, "file_size")
|
||||||
|
state_changes.replace_with_string (f_id, "file_id")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -104,12 +127,18 @@ feature -- Implementation
|
|||||||
|
|
||||||
feature -- Properties
|
feature -- Properties
|
||||||
|
|
||||||
|
removable: BOOLEAN
|
||||||
|
-- Defines if a file can be removed once it is uploaded
|
||||||
|
|
||||||
file: detachable WSF_FILE
|
file: detachable WSF_FILE
|
||||||
-- Text to be displayed
|
-- Text to be displayed
|
||||||
|
|
||||||
change_event: detachable PROCEDURE [ANY, TUPLE]
|
change_event: detachable PROCEDURE [ANY, TUPLE]
|
||||||
-- Procedure to be execued on change
|
-- Procedure to be execued on change
|
||||||
|
|
||||||
|
upload_done_event: detachable PROCEDURE [ANY, TUPLE]
|
||||||
|
-- Procedure to be execued when upload was successful
|
||||||
|
|
||||||
upload_function: detachable FUNCTION [ANY, TUPLE [ITERABLE [WSF_UPLOADED_FILE]], detachable STRING]
|
upload_function: detachable FUNCTION [ANY, TUPLE [ITERABLE [WSF_UPLOADED_FILE]], detachable STRING]
|
||||||
-- Store uploaded file and return server side file id
|
-- Store uploaded file and return server side file id
|
||||||
|
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ class WSF_CONTROL
|
|||||||
@controls=(build_control(control_name, state, @) for control_name, state of @fullstate.controls)
|
@controls=(build_control(control_name, state, @) for control_name, state of @fullstate.controls)
|
||||||
else
|
else
|
||||||
@controls = []
|
@controls = []
|
||||||
|
return
|
||||||
|
|
||||||
attach_events: ()->
|
attach_events: ()->
|
||||||
console.log "Attached #{@control_name}"
|
console.log "Attached #{@control_name}"
|
||||||
@@ -190,6 +191,7 @@ class WSF_CONTROL
|
|||||||
fn(action)
|
fn(action)
|
||||||
catch e
|
catch e
|
||||||
console.log "Failed preforming action #{action.type}"
|
console.log "Failed preforming action #{action.type}"
|
||||||
|
return
|
||||||
|
|
||||||
process_update: (new_states)->
|
process_update: (new_states)->
|
||||||
try
|
try
|
||||||
@@ -204,8 +206,6 @@ class WSF_CONTROL
|
|||||||
return
|
return
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
get_context_state : ()->
|
get_context_state : ()->
|
||||||
if @parent_control? and not @isolation
|
if @parent_control? and not @isolation
|
||||||
return @parent_control.get_context_state()
|
return @parent_control.get_context_state()
|
||||||
@@ -372,6 +372,8 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
|
|||||||
@uploading = false
|
@uploading = false
|
||||||
|
|
||||||
start_upload: ()->
|
start_upload: ()->
|
||||||
|
if @$el[0].files.length==0
|
||||||
|
return
|
||||||
if @uploading
|
if @uploading
|
||||||
return
|
return
|
||||||
@uploading = true
|
@uploading = true
|
||||||
@@ -415,14 +417,15 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
|
|||||||
|
|
||||||
change: ()->
|
change: ()->
|
||||||
#update local state
|
#update local state
|
||||||
@state['file'] = null
|
@state['file_name'] = null
|
||||||
@state['type'] = null
|
@state['file_type'] = null
|
||||||
@state['size'] = null
|
@state['file_size'] = null
|
||||||
|
@state['file_id'] = null
|
||||||
if @$el[0].files.length>0
|
if @$el[0].files.length>0
|
||||||
file = @$el[0].files[0]
|
file = @$el[0].files[0]
|
||||||
@state['file'] = file.name
|
@state['file_name'] = file.name
|
||||||
@state['type'] = file.type
|
@state['file_type'] = file.type
|
||||||
@state['size'] = file.size
|
@state['file_size'] = file.size
|
||||||
if @state['callback_change']
|
if @state['callback_change']
|
||||||
@trigger_callback(@control_name, 'change')
|
@trigger_callback(@control_name, 'change')
|
||||||
@trigger('change')
|
@trigger('change')
|
||||||
@@ -431,10 +434,39 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
|
|||||||
return @$el.val()
|
return @$el.val()
|
||||||
|
|
||||||
update: (state) ->
|
update: (state) ->
|
||||||
if state.upload_file?
|
if state.removable != undefined
|
||||||
@progressbar.hide()
|
@state['removable'] = state.removable
|
||||||
@$el.parent().append($("""<p></p>""").addClass("form-control-static").text(@state['file']))
|
if state.file_name != undefined
|
||||||
@state['upload_file'] = state.upload_file
|
@state['file_name'] = state.file_name
|
||||||
|
if state.file_type != undefined
|
||||||
|
@state['file_type'] = state.file_type
|
||||||
|
if state.file_size != undefined
|
||||||
|
@state['file_size'] = state.file_size
|
||||||
|
if state.file_id != undefined
|
||||||
|
@uploading = false
|
||||||
|
@progressbar.remove()
|
||||||
|
@state['file_id'] = state.file_id
|
||||||
|
@$el.parent().find("p").remove()
|
||||||
|
if state.file_id!=null
|
||||||
|
@$el.hide()
|
||||||
|
fname = $("""<p></p>""").addClass("form-control-static").text(@state['file_name'])
|
||||||
|
@$el.parent().append(fname)
|
||||||
|
if @state['removable']
|
||||||
|
fname.append(" ");
|
||||||
|
btn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger")
|
||||||
|
self = @
|
||||||
|
btn.click ()->
|
||||||
|
self.progressbar.remove()
|
||||||
|
self.$el.parent().find("p").remove()
|
||||||
|
self.$el.show()
|
||||||
|
self.$el.val('')
|
||||||
|
self.change()
|
||||||
|
fname.append(btn)
|
||||||
|
else
|
||||||
|
@$el.show()
|
||||||
|
@$el.val('')
|
||||||
|
@change()
|
||||||
|
|
||||||
|
|
||||||
class WSF_PASSWORD_CONTROL extends WSF_INPUT_CONTROL
|
class WSF_PASSWORD_CONTROL extends WSF_INPUT_CONTROL
|
||||||
|
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ WSF_CONTROL = (function() {
|
|||||||
WSF_CONTROL.prototype.load_subcontrols = function() {
|
WSF_CONTROL.prototype.load_subcontrols = function() {
|
||||||
var control_name, state;
|
var control_name, state;
|
||||||
if (this.fullstate.controls != null) {
|
if (this.fullstate.controls != null) {
|
||||||
return this.controls = (function() {
|
this.controls = (function() {
|
||||||
var _ref, _results;
|
var _ref, _results;
|
||||||
_ref = this.fullstate.controls;
|
_ref = this.fullstate.controls;
|
||||||
_results = [];
|
_results = [];
|
||||||
@@ -274,7 +274,7 @@ WSF_CONTROL = (function() {
|
|||||||
return _results;
|
return _results;
|
||||||
}).call(this);
|
}).call(this);
|
||||||
} else {
|
} else {
|
||||||
return this.controls = [];
|
this.controls = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -293,24 +293,22 @@ WSF_CONTROL = (function() {
|
|||||||
WSF_CONTROL.prototype.update = function(state) {};
|
WSF_CONTROL.prototype.update = function(state) {};
|
||||||
|
|
||||||
WSF_CONTROL.prototype.process_actions = function(actions) {
|
WSF_CONTROL.prototype.process_actions = function(actions) {
|
||||||
var action, fn, _i, _len, _results;
|
var action, fn, _i, _len;
|
||||||
_results = [];
|
|
||||||
for (_i = 0, _len = actions.length; _i < _len; _i++) {
|
for (_i = 0, _len = actions.length; _i < _len; _i++) {
|
||||||
action = actions[_i];
|
action = actions[_i];
|
||||||
try {
|
try {
|
||||||
fn = null;
|
fn = null;
|
||||||
if (this[action.type] != null) {
|
if (this[action.type] != null) {
|
||||||
fn = this[action.type];
|
fn = this[action.type];
|
||||||
_results.push(fn.call(this, action));
|
fn.call(this, action);
|
||||||
} else {
|
} else {
|
||||||
fn = eval(action.type);
|
fn = eval(action.type);
|
||||||
_results.push(fn(action));
|
fn(action);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_results.push(console.log("Failed preforming action " + action.type));
|
console.log("Failed preforming action " + action.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return _results;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WSF_CONTROL.prototype.process_update = function(new_states) {
|
WSF_CONTROL.prototype.process_update = function(new_states) {
|
||||||
@@ -613,6 +611,9 @@ WSF_FILE_CONTROL = (function(_super) {
|
|||||||
|
|
||||||
WSF_FILE_CONTROL.prototype.start_upload = function() {
|
WSF_FILE_CONTROL.prototype.start_upload = function() {
|
||||||
var action, file, formData;
|
var action, file, formData;
|
||||||
|
if (this.$el[0].files.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.uploading) {
|
if (this.uploading) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -665,14 +666,15 @@ WSF_FILE_CONTROL = (function(_super) {
|
|||||||
|
|
||||||
WSF_FILE_CONTROL.prototype.change = function() {
|
WSF_FILE_CONTROL.prototype.change = function() {
|
||||||
var file;
|
var file;
|
||||||
this.state['file'] = null;
|
this.state['file_name'] = null;
|
||||||
this.state['type'] = null;
|
this.state['file_type'] = null;
|
||||||
this.state['size'] = null;
|
this.state['file_size'] = null;
|
||||||
|
this.state['file_id'] = null;
|
||||||
if (this.$el[0].files.length > 0) {
|
if (this.$el[0].files.length > 0) {
|
||||||
file = this.$el[0].files[0];
|
file = this.$el[0].files[0];
|
||||||
this.state['file'] = file.name;
|
this.state['file_name'] = file.name;
|
||||||
this.state['type'] = file.type;
|
this.state['file_type'] = file.type;
|
||||||
this.state['size'] = file.size;
|
this.state['file_size'] = file.size;
|
||||||
}
|
}
|
||||||
if (this.state['callback_change']) {
|
if (this.state['callback_change']) {
|
||||||
this.trigger_callback(this.control_name, 'change');
|
this.trigger_callback(this.control_name, 'change');
|
||||||
@@ -685,10 +687,46 @@ WSF_FILE_CONTROL = (function(_super) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
WSF_FILE_CONTROL.prototype.update = function(state) {
|
WSF_FILE_CONTROL.prototype.update = function(state) {
|
||||||
if (state.upload_file != null) {
|
var btn, fname, self;
|
||||||
this.progressbar.hide();
|
if (state.removable !== void 0) {
|
||||||
this.$el.parent().append($("<p></p>").addClass("form-control-static").text(this.state['file']));
|
this.state['removable'] = state.removable;
|
||||||
return this.state['upload_file'] = state.upload_file;
|
}
|
||||||
|
if (state.file_name !== void 0) {
|
||||||
|
this.state['file_name'] = state.file_name;
|
||||||
|
}
|
||||||
|
if (state.file_type !== void 0) {
|
||||||
|
this.state['file_type'] = state.file_type;
|
||||||
|
}
|
||||||
|
if (state.file_size !== void 0) {
|
||||||
|
this.state['file_size'] = state.file_size;
|
||||||
|
}
|
||||||
|
if (state.file_id !== void 0) {
|
||||||
|
this.uploading = false;
|
||||||
|
this.progressbar.remove();
|
||||||
|
this.state['file_id'] = state.file_id;
|
||||||
|
this.$el.parent().find("p").remove();
|
||||||
|
if (state.file_id !== null) {
|
||||||
|
this.$el.hide();
|
||||||
|
fname = $("<p></p>").addClass("form-control-static").text(this.state['file_name']);
|
||||||
|
this.$el.parent().append(fname);
|
||||||
|
if (this.state['removable']) {
|
||||||
|
fname.append(" ");
|
||||||
|
btn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger");
|
||||||
|
self = this;
|
||||||
|
btn.click(function() {
|
||||||
|
self.progressbar.remove();
|
||||||
|
self.$el.parent().find("p").remove();
|
||||||
|
self.$el.show();
|
||||||
|
self.$el.val('');
|
||||||
|
return self.change();
|
||||||
|
});
|
||||||
|
return fname.append(btn);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.$el.show();
|
||||||
|
this.$el.val('');
|
||||||
|
return this.change();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -26,15 +26,14 @@ feature -- Implementation
|
|||||||
control.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("h1", "", "File Upload Demo"))
|
control.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("h1", "", "File Upload Demo"))
|
||||||
create form.make
|
create form.make
|
||||||
--File
|
--File
|
||||||
create filebox.make
|
create filebox.make (true)
|
||||||
filebox.set_upload_function (agent upload_file)
|
filebox.set_upload_function (agent upload_file)
|
||||||
create n0_container.make ("File Upload", filebox)
|
create n0_container.make ("File Upload", filebox)
|
||||||
n0_container.add_validator (create {WSF_FILESIZE_VALIDATOR}.make (10000000, "File must be smaller than 10MB"))
|
n0_container.add_validator (create {WSF_FILESIZE_VALIDATOR}.make (10000000, "File must be smaller than 10MB"))
|
||||||
form.add_control (n0_container)
|
form.add_control (n0_container)
|
||||||
--File
|
--File
|
||||||
create filebox2.make
|
create filebox2.make (true)
|
||||||
create n1_container.make ("Auto start Upload", filebox2)
|
create n1_container.make ("Auto start Upload", filebox2)
|
||||||
filebox2.set_upload_function (agent upload_file)
|
|
||||||
filebox2.set_change_event (agent
|
filebox2.set_change_event (agent
|
||||||
do
|
do
|
||||||
n1_container.validate
|
n1_container.validate
|
||||||
|
|||||||
Reference in New Issue
Block a user