Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/jquery.inputfile.js
2941 views
1
/*jslint browser:true */
2
/*global jQuery */
3
4
(function ($) {
5
6
"use strict";
7
8
var methods = {},
9
BASE_CLASS = 'inputfile',
10
BUTTON_CLASS = 'upload-button',
11
PREVIOUS_CLASS = 'previous-file',
12
DELETED_CLASS = 'deleted',
13
LINK_CLASS = 'upload-button-link',
14
REMOVE_CLASS = 'upload-button-remove';
15
16
function selectFile(evt) {
17
18
var $input = $(evt.currentTarget),
19
$base = $input.parents('.' + BASE_CLASS),
20
$div = $base.find('.' + PREVIOUS_CLASS),
21
$link = $div.find('a'),
22
$checkbox = $base.find('input[type="checkbox"]');
23
24
$div.show();
25
26
$checkbox.attr('disabled', 'disabled');
27
$div.removeClass(DELETED_CLASS);
28
29
$link.attr('href', '#');
30
//$link.html($input.val());
31
$link.html($input.val().split('C:\\fakepath\\')[1]);
32
$link.on('click', function () { return false; });
33
}
34
35
function removeSelectedFile(evt) {
36
var $button = $(evt.currentTarget),
37
$base = $button.parents('.' + BASE_CLASS),
38
$file = $base.find('input[type="file"]'),
39
$parent = $button.parent(),
40
$link = $parent.find('a'),
41
$checkbox = $base.find('input[type="checkbox"]');
42
43
if ($file.attr('data-value')) {
44
if ($parent.hasClass(DELETED_CLASS)) {
45
$checkbox.removeAttr('checked');
46
$checkbox.attr('disabled', 'disabled');
47
48
$parent.removeClass(DELETED_CLASS);
49
} else {
50
51
if ($link.attr('href') !== $file.attr('data-value')) {
52
$link.attr('href', $file.attr('data-value'));
53
$link.html($file.attr('data-text'));
54
} else {
55
$checkbox.attr('checked', 'checked');
56
$checkbox.removeAttr('disabled');
57
58
$parent.addClass(DELETED_CLASS);
59
}
60
}
61
62
} else {
63
$parent.hide();
64
$parent.addClass(DELETED_CLASS);
65
66
$checkbox.removeAttr('disabled', 'disabled');
67
$link.html('');
68
$file.replaceWith($file.clone(true));
69
}
70
71
return false;
72
}
73
74
function init(params) {
75
76
this.each(function () {
77
78
var $self = $(this),
79
fileUrl = $self.data('value'),
80
fileText = $self.data('text'),
81
config = $.extend({}, $self.inputfile.config, params),
82
$button,
83
$base,
84
$previous,
85
$link,
86
$remove,
87
removeName,
88
$checkbox,
89
$fileButton;
90
91
// Set the html
92
$self.wrap('<div class="' + BASE_CLASS + '"><div class="' + BUTTON_CLASS + '"></div></div>');
93
94
$button = $self.parent();
95
$base = $button.parent();
96
97
$previous = $('<div class="' + PREVIOUS_CLASS + '"></div>').prependTo($base);
98
$link = $('<a href="#" class="' + LINK_CLASS + '" target="_blank"></a>').appendTo($previous);
99
100
if (!params.dontRemove) {
101
$remove = $('<button class="' + REMOVE_CLASS + ' ' + config.removeButtonClass + '"></button>').appendTo($previous);
102
103
$remove.append($(config.removeText).addClass('remove-icon'));
104
$remove.append($(config.restoreText).addClass('restore-icon'));
105
106
removeName = config.removeName || $self.attr('name');
107
$checkbox = $('<input type="checkbox" name="' + removeName + '" value="' + config.removeValue + '" disabled/>');
108
109
$checkbox.hide().appendTo($remove);
110
}
111
112
if (!fileUrl) {
113
$previous.hide();
114
} else {
115
$link.attr('href', fileUrl);
116
$link.html(fileText || fileUrl);
117
}
118
119
$fileButton = $('<button class="' + config.uploadButtonClass + '">' + config.uploadText + '</button>').insertBefore($self);
120
121
// Event listeners
122
$self.on('change', selectFile);
123
124
if ($remove) {
125
$remove.on('click', removeSelectedFile);
126
}
127
});
128
}
129
130
$.fn.inputfile = function (method) {
131
132
if (methods[method]) {
133
return methods[method].apply($(this), Array.prototype.slice.call(arguments, 1));
134
} else if (!method || (method && method.constructor === Object)) {
135
return init.apply(this, arguments);
136
}
137
};
138
139
$.fn.inputfile.config = {
140
uploadText: '<i class="icon-upload"></i> Upload file',
141
removeText: '<i class="icon-trash"></i>',
142
restoreText: '<i class="icon-undo"></i>',
143
144
uploadButtonClass: 'btn',
145
removeButtonClass: 'btn',
146
147
removeName: '',
148
removeValue: 1,
149
150
dontRemove: false
151
};
152
153
}(jQuery));
154