Add disable option

This commit is contained in:
YNH Webdev
2014-01-02 18:19:59 +01:00
parent d3c66cd7fe
commit ce305d4e54
7 changed files with 212 additions and 94 deletions

View File

@@ -29,6 +29,11 @@ feature
feature --Properties
is_uploaded: BOOLEAN
do
Result := attached id
end
name: STRING
-- File name

View File

@@ -19,9 +19,8 @@ create
feature {NONE} -- Initialization
make (a_removable: BOOLEAN)
make
do
removable := a_removable
make_value_control ("input")
end
@@ -45,35 +44,18 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
do
create Result.make
Result.put_boolean (attached change_event, "callback_change")
Result.put_boolean (attached upload_done_event, "callback_uploaddone")
if attached file as f then
Result.put_string (f.name, "file_name")
Result.put_string (f.type, "file_type")
Result.put_integer (f.size, "file_size")
Result.put_string (f.id, "file_id")
end
Result.put_boolean (removable, "removable")
Result.put_boolean (disabled, "disabled")
end
feature -- Event handling
set_change_event (e: attached like change_event)
-- Set text change event handle
do
change_event := e
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 button click event handle
do
upload_function := e
end
handle_callback (cname: LIST [STRING]; event: STRING; event_parameter: detachable ANY)
local
f_name: detachable STRING
@@ -84,6 +66,8 @@ feature -- Event handling
if Current.control_name.same_string (cname [1]) then
if attached change_event as cevent and event.same_string ("change") then
cevent.call (Void)
elseif attached upload_done_event as udevent and event.same_string ("uploaddone") then
udevent.call (Void)
elseif event.same_string ("uploadfile") and attached {ITERABLE [WSF_UPLOADED_FILE]} event_parameter as files then
if attached file as f then
if attached upload_function as ufunction then
@@ -121,14 +105,51 @@ feature -- Implementation
end
render: STRING
local
attr: STRING
do
Result := render_tag ("", "type=%"file%" ")
attr := "type=%"file%" "
if attached attributes as a then
attr.append (a)
end
if disabled then
attr.append ("disabled=%"disabled%" ")
end
Result := render_tag ("", attr)
end
feature -- Change
set_change_event (e: attached like change_event)
-- Set text change event handle
do
change_event := e
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 button click event handle
do
upload_function := e
end
set_disabled (b: BOOLEAN)
do
if disabled /= b then
disabled := b
state_changes.replace_with_boolean (disabled, "disabled")
end
end
feature -- Properties
removable: BOOLEAN
-- Defines if a file can be removed once it is uploaded
disabled: BOOLEAN
-- Defines if the a file is selectable and if a file can be removed once it is uploaded
file: detachable WSF_FILE
-- Text to be displayed

View File

@@ -22,7 +22,7 @@ feature {NONE} -- Initialization
make (v: STRING)
-- Initialize with specified name and value
do
make_value_control ( "input")
make_value_control ("input")
type := "text"
text := v
end
@@ -42,6 +42,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
do
create Result.make
Result.put_string (text, "text")
Result.put_boolean (disabled, "disabled")
Result.put_boolean (attached change_event, "callback_change")
end
@@ -53,9 +54,9 @@ feature --Event handling
change_event := e
end
handle_callback (cname: LIST[STRING]; event: STRING; event_parameter: detachable ANY)
do
if Current.control_name.same_string (cname[1]) and attached change_event as cevent then
handle_callback (cname: LIST [STRING]; event: STRING; event_parameter: detachable ANY)
do
if Current.control_name.same_string (cname [1]) and attached change_event as cevent then
if event.same_string ("change") then
cevent.call (Void)
end
@@ -65,8 +66,17 @@ feature --Event handling
feature -- Rendering
render: STRING
local
attr: STRING
do
Result := render_tag ("", "type=%"" + type + "%" value=%"" + text + "%"")
attr := "type=%"" + type + "%" value=%"" + text + "%" "
if attached attributes as a then
attr.append (a)
end
if disabled then
attr.append ("disabled=%"disabled%" ")
end
Result := render_tag ("", attr)
end
feature -- Change
@@ -80,6 +90,14 @@ feature -- Change
end
end
set_disabled (b: BOOLEAN)
do
if disabled /= b then
disabled := b
state_changes.replace_with_boolean (disabled, "disabled")
end
end
feature -- Implementation
value: STRING
@@ -89,6 +107,9 @@ feature -- Implementation
feature -- Properties
disabled: BOOLEAN
-- Defines if the input field is editable
text: STRING
-- Text to be displayed

View File

@@ -43,6 +43,7 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- State management
do
create Result.make
Result.put_string (text, "text")
Result.put_boolean (disabled, "disabled")
Result.put_boolean (attached click_event, "callback_click")
end
@@ -54,9 +55,9 @@ feature --Event handling
click_event := e
end
handle_callback (cname: LIST[STRING]; event: STRING; event_parameter: detachable ANY)
handle_callback (cname: LIST [STRING]; event: STRING; event_parameter: detachable ANY)
do
if Current.control_name.same_string (cname[1]) and attached click_event as cevent then
if Current.control_name.same_string (cname [1]) and attached click_event as cevent then
cevent.call (Void)
end
end
@@ -65,8 +66,17 @@ feature -- Rendering
render: STRING
-- HTML representation of this control
local
attr: STRING
do
Result := render_tag (text, attributes)
create attr.make_empty
if attached attributes as a then
attr.append (a)
end
if disabled then
attr.append ("disabled=%"disabled%" ")
end
Result := render_tag (text, attr)
end
feature -- Change
@@ -76,12 +86,23 @@ feature -- Change
do
if not t.same_string (text) then
text := t
state_changes.replace (create {JSON_STRING}.make_json (text), "text")
state_changes.replace_with_string (text, "text")
end
end
set_disabled (b: BOOLEAN)
do
if disabled /= b then
disabled := b
state_changes.replace_with_boolean (disabled, "disabled")
end
end
feature -- Properties
disabled: BOOLEAN
-- Defines if the button is editable
text: STRING
-- The text currently displayed on this button

View File

@@ -340,6 +340,9 @@ class WSF_BUTTON_CONTROL extends WSF_CONTROL
@trigger_callback(@control_name, 'click')
update: (state) ->
if state.disabled != undefined
@state['disabled'] = state.disabled
@$el.prop('disabled', state.disabled)
if state.text?
@state['text'] = state.text
@$el.text(state.text)
@@ -362,6 +365,9 @@ class WSF_INPUT_CONTROL extends WSF_CONTROL
return @$el.val()
update: (state) ->
if state.disabled != undefined
@state['disabled'] = state.disabled
@$el.prop('disabled', state.disabled)
if state.text?
@state['text'] = state.text
@$el.val(state.text)
@@ -434,8 +440,10 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
return @$el.val()
update: (state) ->
if state.removable != undefined
@state['removable'] = state.removable
if state.disabled != undefined
@state['disabled'] = state.disabled
@$el.prop('disabled', state.disabled)
@refresh()
if state.file_name != undefined
@state['file_name'] = state.file_name
if state.file_type != undefined
@@ -443,29 +451,37 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
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()
if @state['file_id'] != state.file_id
@state['file_id'] = state.file_id
if @state['callback_uploaddone']
@trigger_callback(@control_name, 'uploaddone')
@uploading = false
@refresh()
refresh: ()->
if @uploading
return
@progressbar.remove()
@$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 not @state['disabled']
fname.append(" ");
removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger")
self = @
removebtn.click ()->
self.progressbar.remove()
self.$el.parent().find("p").remove()
self.$el.show()
self.$el.val('')
self.change()
fname.append(removebtn)
else
@$el.show()
@$el.val('')
@change()
class WSF_PASSWORD_CONTROL extends WSF_INPUT_CONTROL

View File

@@ -550,6 +550,10 @@ WSF_BUTTON_CONTROL = (function(_super) {
};
WSF_BUTTON_CONTROL.prototype.update = function(state) {
if (state.disabled !== void 0) {
this.state['disabled'] = state.disabled;
this.$el.prop('disabled', state.disabled);
}
if (state.text != null) {
this.state['text'] = state.text;
return this.$el.text(state.text);
@@ -590,6 +594,10 @@ WSF_INPUT_CONTROL = (function(_super) {
};
WSF_INPUT_CONTROL.prototype.update = function(state) {
if (state.disabled !== void 0) {
this.state['disabled'] = state.disabled;
this.$el.prop('disabled', state.disabled);
}
if (state.text != null) {
this.state['text'] = state.text;
return this.$el.val(state.text);
@@ -687,9 +695,10 @@ WSF_FILE_CONTROL = (function(_super) {
};
WSF_FILE_CONTROL.prototype.update = function(state) {
var btn, fname, self;
if (state.removable !== void 0) {
this.state['removable'] = state.removable;
if (state.disabled !== void 0) {
this.state['disabled'] = state.disabled;
this.$el.prop('disabled', state.disabled);
this.refresh();
}
if (state.file_name !== void 0) {
this.state['file_name'] = state.file_name;
@@ -701,32 +710,45 @@ WSF_FILE_CONTROL = (function(_super) {
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);
if (this.state['file_id'] !== state.file_id) {
this.state['file_id'] = state.file_id;
if (this.state['callback_uploaddone']) {
this.trigger_callback(this.control_name, 'uploaddone');
}
} else {
this.$el.show();
this.$el.val('');
return this.change();
this.uploading = false;
}
return this.refresh();
}
};
WSF_FILE_CONTROL.prototype.refresh = function() {
var fname, removebtn, self;
if (this.uploading) {
return;
}
this.progressbar.remove();
this.$el.parent().find("p").remove();
if (this.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['disabled']) {
fname.append(" ");
removebtn = $("<button />").text("Remove").addClass("btn btn-xs btn-danger");
self = this;
removebtn.click(function() {
self.progressbar.remove();
self.$el.parent().find("p").remove();
self.$el.show();
self.$el.val('');
return self.change();
});
return fname.append(removebtn);
}
} else {
this.$el.show();
this.$el.val('');
return this.change();
}
};

View File

@@ -26,13 +26,15 @@ feature -- Implementation
control.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("h1", "", "File Upload Demo"))
create form.make
--File
create filebox.make (true)
create filebox.make
filebox.set_upload_function (agent upload_file)
filebox.set_upload_done_event (agent submit_form)
create n0_container.make ("File Upload", filebox)
n0_container.add_validator (create {WSF_FILESIZE_VALIDATOR}.make (10000000, "File must be smaller than 10MB"))
form.add_control (n0_container)
--File
create filebox2.make (true)
create filebox2.make
filebox2.set_upload_function (agent upload_file)
create n1_container.make ("Auto start Upload", filebox2)
filebox2.set_change_event (agent
do
@@ -49,18 +51,29 @@ feature -- Implementation
button1.set_click_event (agent submit_form)
button1.add_class ("col-lg-offset-2")
form.add_control (button1)
control.add_control (form)
navbar.set_active (5)
end
submit_form
do
form.validate
if form.is_valid then
filebox.start_upload
do
form.validate
if form.is_valid then
if attached filebox.file as f then
if not f.is_uploaded then
filebox.set_disabled (true)
filebox.start_upload
filebox2.set_disabled (true)
button1.set_disabled (true)
button1.set_text ("Uploading ...")
else
button1.set_text ("Done")
end
end
end
end
end
upload_file (f: ITERABLE [WSF_UPLOADED_FILE]): detachable String
do
@@ -90,5 +103,4 @@ feature -- Properties
n1_container: WSF_FORM_ELEMENT_CONTROL [detachable WSF_FILE]
end