Allow remove

This commit is contained in:
YNH Webdev
2014-01-02 03:20:57 +01:00
parent 7bf95cd927
commit d3c66cd7fe
4 changed files with 140 additions and 42 deletions

View File

@@ -166,6 +166,7 @@ class WSF_CONTROL
@controls=(build_control(control_name, state, @) for control_name, state of @fullstate.controls)
else
@controls = []
return
attach_events: ()->
console.log "Attached #{@control_name}"
@@ -190,6 +191,7 @@ class WSF_CONTROL
fn(action)
catch e
console.log "Failed preforming action #{action.type}"
return
process_update: (new_states)->
try
@@ -204,8 +206,6 @@ class WSF_CONTROL
return
return
get_context_state : ()->
if @parent_control? and not @isolation
return @parent_control.get_context_state()
@@ -372,6 +372,8 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
@uploading = false
start_upload: ()->
if @$el[0].files.length==0
return
if @uploading
return
@uploading = true
@@ -415,14 +417,15 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
change: ()->
#update local state
@state['file'] = null
@state['type'] = null
@state['size'] = null
@state['file_name'] = null
@state['file_type'] = null
@state['file_size'] = null
@state['file_id'] = null
if @$el[0].files.length>0
file = @$el[0].files[0]
@state['file'] = file.name
@state['type'] = file.type
@state['size'] = file.size
@state['file_name'] = file.name
@state['file_type'] = file.type
@state['file_size'] = file.size
if @state['callback_change']
@trigger_callback(@control_name, 'change')
@trigger('change')
@@ -431,10 +434,39 @@ class WSF_FILE_CONTROL extends WSF_CONTROL
return @$el.val()
update: (state) ->
if state.upload_file?
@progressbar.hide()
@$el.parent().append($("""<p></p>""").addClass("form-control-static").text(@state['file']))
@state['upload_file'] = state.upload_file
if state.removable != undefined
@state['removable'] = state.removable
if state.file_name != undefined
@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

View File

@@ -263,7 +263,7 @@ WSF_CONTROL = (function() {
WSF_CONTROL.prototype.load_subcontrols = function() {
var control_name, state;
if (this.fullstate.controls != null) {
return this.controls = (function() {
this.controls = (function() {
var _ref, _results;
_ref = this.fullstate.controls;
_results = [];
@@ -274,7 +274,7 @@ WSF_CONTROL = (function() {
return _results;
}).call(this);
} else {
return this.controls = [];
this.controls = [];
}
};
@@ -293,24 +293,22 @@ WSF_CONTROL = (function() {
WSF_CONTROL.prototype.update = function(state) {};
WSF_CONTROL.prototype.process_actions = function(actions) {
var action, fn, _i, _len, _results;
_results = [];
var action, fn, _i, _len;
for (_i = 0, _len = actions.length; _i < _len; _i++) {
action = actions[_i];
try {
fn = null;
if (this[action.type] != null) {
fn = this[action.type];
_results.push(fn.call(this, action));
fn.call(this, action);
} else {
fn = eval(action.type);
_results.push(fn(action));
fn(action);
}
} 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) {
@@ -613,6 +611,9 @@ WSF_FILE_CONTROL = (function(_super) {
WSF_FILE_CONTROL.prototype.start_upload = function() {
var action, file, formData;
if (this.$el[0].files.length === 0) {
return;
}
if (this.uploading) {
return;
}
@@ -665,14 +666,15 @@ WSF_FILE_CONTROL = (function(_super) {
WSF_FILE_CONTROL.prototype.change = function() {
var file;
this.state['file'] = null;
this.state['type'] = null;
this.state['size'] = null;
this.state['file_name'] = null;
this.state['file_type'] = null;
this.state['file_size'] = null;
this.state['file_id'] = null;
if (this.$el[0].files.length > 0) {
file = this.$el[0].files[0];
this.state['file'] = file.name;
this.state['type'] = file.type;
this.state['size'] = file.size;
this.state['file_name'] = file.name;
this.state['file_type'] = file.type;
this.state['file_size'] = file.size;
}
if (this.state['callback_change']) {
this.trigger_callback(this.control_name, 'change');
@@ -685,10 +687,46 @@ WSF_FILE_CONTROL = (function(_super) {
};
WSF_FILE_CONTROL.prototype.update = function(state) {
if (state.upload_file != null) {
this.progressbar.hide();
this.$el.parent().append($("<p></p>").addClass("form-control-static").text(this.state['file']));
return this.state['upload_file'] = state.upload_file;
var btn, fname, self;
if (state.removable !== void 0) {
this.state['removable'] = state.removable;
}
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();
}
}
};

View File

@@ -26,15 +26,14 @@ feature -- Implementation
control.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("h1", "", "File Upload Demo"))
create form.make
--File
create filebox.make
create filebox.make (true)
filebox.set_upload_function (agent upload_file)
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
create filebox2.make (true)
create n1_container.make ("Auto start Upload", filebox2)
filebox2.set_upload_function (agent upload_file)
filebox2.set_change_event (agent
do
n1_container.validate