source: extensions/cuise/admin/js/fileupload.js @ 19486

Last change on this file since 19486 was 19486, checked in by cljosse, 11 years ago

[style cuise] create cuise

File size: 6.5 KB
Line 
1(function ($) {
2  jQuery.fn.ajaxfileuploader = function (options) {
3    var opts = jQuery.extend({}, jQuery.fn.ajaxfileuploader.defaults, options);
4    var filelist = new Array();
5    var uploadwidget = new Object();
6    var imgwidget = new Object();
7    return this.each(function () {
8      buildElement($(this).attr("id"))
9      jQuery(this).change(onChange);
10    });
11
12    function onChange() {
13      /* Validate Form Before submit */
14      var filename = $(this).val();
15      var pos = filename.lastIndexOf('.');
16      var msg = '';
17      if (pos !== -1) {
18        var extension = filename.substring(pos + 1);
19        var isvalidextension = false;
20        msg = " Invalid file [" + extension + "] not supported:";
21
22        for (i in opts.VALIDFORMAT) {
23          var reg = new RegExp("(" + opts.VALIDFORMAT[i] + ")", "gi");
24          if (extension.match(reg)) {
25            isvalidextension = true; break;
26          }
27        }
28      }
29
30      if (isvalidextension && imgwidget.find("tr").length > opts.MAXUPLOAD) {
31        isvalidextension = false; msg = "only " + opts.MAXUPLOAD + " File can be uploaded ";
32        displayUploadForm();
33      }
34
35      if (isvalidextension) uploadwidget.find("form").submit();
36      if (!isvalidextension || pos == -1)
37        alert(msg);
38      return false;
39    }
40
41    function setbck() {
42      var row = $(this).parents("tr");
43      var imgname = $(this).attr("title");
44      var name = $(this).attr("name");
45      jQuery("#fileInputTxt").val(name);
46      jQuery(".image").css({ backgroundImage: "url(" + imgname + ")" });
47      return;
48
49      var request = $.ajax({
50        url: opts.SCRIPT + "?action=adding",
51        type: "POST",
52        async: false,
53        data: { name: imgname },
54        dataType: "html"
55      });
56      request.done(function (msg) {
57
58        displayUploadForm();
59      });
60
61      request.fail(function (jqXHR, textStatus) {
62        alert("Request failed:User " + textStatus);
63      });
64
65      return false;
66    }
67
68    var del_ok = false;
69    function delRow() {
70      if (!del_ok)
71        if (!confirm("Are you sure, you want to delete this image?"))
72          return;
73
74      del_ok = true;
75
76      var row = $(this).parents("tr");
77      var imgname = $(this).attr("title");
78      var request = $.ajax({
79        url: opts.SCRIPT + "?action=deleteimg",
80        type: "POST",
81        async: false,
82        data: { name: imgname },
83        dataType: "html"
84      });
85      request.done(function (msg) {
86        row.remove();
87        displayUploadForm();
88      });
89
90      request.fail(function (jqXHR, textStatus) {
91        alert("Request failed:User " + textStatus);
92      });
93
94      return false;
95    }
96
97
98    function displayUploadForm() {
99
100      if (imgwidget.find("tr").length < opts.MAXUPLOAD + 1)
101        uploadwidget.find('form input').removeAttr("disabled")
102      else
103        uploadwidget.find('form input').attr("disabled", "disabled")
104    }
105
106    function loaddetails() {
107      var txt = uploadwidget.find("iframe").contents().find('body').html();
108      try {
109        var obj = jQuery.parseJSON(txt);
110      } catch (e) {
111        alert(txt);
112        var obj;
113      }
114
115      if (obj) {
116        for (i in obj['files']) {
117          var name = obj['files'][i].name;
118          var thumbimgurl = obj['files'][i].thumbimgurl;
119          var upload_name = obj['files'][i].upload_name;
120          var errors = obj['files'][i].errors;
121          var imgUrl = obj['files'][i].imgUrl;
122          link = "<a href='javascript:void(0)' class='setbck' name='" + name + "' title='" + opts.PATH_IMG + name + "' >";
123          messages = "<tr id='" + name + "'  style='width:120px'>";
124          messages += "<td>" + link + "<img src='" + opts.PATH_IMG + name + "'></a></td>";
125          if (jQuery(".image").hasClass("texture")) {
126            messages += "<td>" + upload_name + "</td> ";
127            jQuery(errors).each(
128          function (i) {
129            messages += "<td><span>" + this.error + "</span> </td>";
130          }
131          );
132            messages += "<td><a href='javascript:void(0)' class='delrow' title='" + name + "'>Delete</a> </td>";
133          }
134          messages += "</tr>";
135
136          imgwidget.find("table").append(messages);
137          if (jQuery(".image").hasClass("texture")) {
138            jQuery("#fileInputTxt").val(opts.PATH_IMG + name);
139            imgwidget.find("tr[id='" + name + "'] a.delrow").click(delRow);
140            jQuery(".image").css({ backgroundImage: "url(" + opts.PATH_IMG + name + ")" });
141          }
142          imgwidget.find("tr[id='" + name + "'] a.setbck").click(setbck);
143
144        }
145      }
146
147      displayUploadForm();
148    }
149
150    function buildElement(id) {
151      var pos = jQuery("#" + id).offset();
152      var divid = "uploadwidget" + id
153      var formid = "idOfFormElement" + id;
154      var iframeid = "idOfIframeElement" + id;
155
156      jQuery('#result').append(jQuery('<div id="' + divid + '"></div>'));
157      jQuery('#result').append(jQuery('<div id="' + divid + '_imgmgt" class="filemgt"><table></table><div class="paginate"></div></div>'));
158      uploadwidget = jQuery('#' + divid);
159      imgwidget = jQuery('#' + divid + '_imgmgt');
160      if (jQuery(".image").hasClass("texture"))
161        imgwidget.find("table").append("<tr><td>" + opts.TABLEHEAD.name + "</td><td>" + opts.TABLEHEAD.imgname + "</td><td>" + opts.TABLEHEAD.operation + "</td></tr>");
162
163      uploadwidget.append('<iframe name="' + iframeid + '" id="' + iframeid + '" style="width:0px;height:0px" src="' + opts.SCRIPT + '?action=getimages&root=' + opts.PATH +
164       '&root_img=' + opts.PATH_IMG +
165       '&root_theme=' + opts.PATH_THEME + '"></iframe>');
166
167      uploadwidget.append(jQuery('<form action="' + opts.SCRIPT + '?action=addimg&root=' + opts.PATH +
168       '&root_img=' + opts.PATH_IMG +
169       '&root_theme=' + opts.PATH_THEME + '" enctype="multipart/form-data" method="post" target="' + iframeid + '"></form>'));
170      uploadwidget.find('form').append('<input id="fileupload" type="file" name="files" class="file" >');
171
172      uploadwidget.css("position", "absolute")
173      uploadwidget.css("top", pos.top);
174      uploadwidget.css("left", pos.left);
175      uploadwidget.find('iframe').hide();
176
177      uploadwidget.find('iframe').bind('load', loaddetails);
178      jQuery("#" + id).hide();
179      uploadwidget.find('.file').change(onChange);
180
181    }
182
183
184  }
185
186  jQuery(document).ready(function () {
187    jQuery(window).load(function (i) {
188
189    });
190  });
191})(jQuery);
192
193
Note: See TracBrowser for help on using the repository browser.