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

Last change on this file since 20836 was 19990, checked in by cljosse, 11 years ago
  • [style] cuise save
File size: 7.1 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      jQuery(".image").trigger("change");
48      return;
49
50      var request = $.ajax({
51        url: opts.SCRIPT + "?action=adding",
52        type: "POST",
53        async: false,
54        data: { name: imgname },
55        dataType: "html"
56      });
57      request.done(function (msg) {
58
59        displayUploadForm();
60      });
61
62      request.fail(function (jqXHR, textStatus) {
63        alert("Request failed:User " + textStatus);
64      });
65
66      return false;
67    }
68
69    var del_ok = false;
70    function delRow() {
71      if (!del_ok)
72        if (!confirm("Are you sure, you want to delete this image?"))
73          return;
74
75      del_ok = true;
76
77      var row = $(this).parents("tr");
78      var imgname = $(this).attr("title");
79      var request = $.ajax({
80        url: opts.SCRIPT + "?action=deleteimg",
81        type: "POST",
82        async: false,
83        data: { name: imgname },
84        dataType: "html"
85      });
86      request.done(function (msg) {
87        row.remove();
88        displayUploadForm();
89      });
90
91      request.fail(function (jqXHR, textStatus) {
92        alert("Request failed:User " + textStatus);
93      });
94
95      return false;
96    }
97
98
99    function displayUploadForm() {
100
101      if (imgwidget.find("tr").length < opts.MAXUPLOAD + 1)
102        uploadwidget.find('form input').removeAttr("disabled")
103      else
104        uploadwidget.find('form input').attr("disabled", "disabled")
105    }
106
107    function loaddetails() {
108      var txt = uploadwidget.find("iframe").contents().find('body').html();
109      try {
110        var obj = jQuery.parseJSON(txt);
111      } catch (e) {
112        alert(txt);
113        var obj;
114      }
115
116      if (obj) {
117        for (i in obj['files']) {
118          var name = obj['files'][i].name;
119          var thumbimgurl = obj['files'][i].thumbimgurl;
120          var upload_name = obj['files'][i].upload_name;
121          var errors = obj['files'][i].errors;
122          var imgUrl = obj['files'][i].imgUrl;
123          link = "<a href='javascript:void(0)' class='setbck' name='" + name + "' title='" + opts.PATH_IMG + name + "' >";
124          messages = "<tr id='" + name + "'  style='width:120px'>";
125          messages += "<td>" + link + "<img src='" + opts.PATH_IMG + name + "'></a></td>";
126          if (jQuery(".image").hasClass("texture")) {
127            messages += "<td>" + upload_name + "</td> ";
128            jQuery(errors).each(
129          function (i) {
130            messages += "<td><span>" + this.error + "</span> </td>";
131          }
132          );
133            messages += "<td><a href='javascript:void(0)' class='delrow' title='" + name + "'>Delete</a> </td>";
134          }
135          messages += "</tr>";
136
137          imgwidget.find("table").append(messages);
138          if (jQuery(".image").hasClass("texture")) {
139            jQuery("#fileInputTxt").val(opts.PATH_IMG + name);
140            imgwidget.find("tr[id='" + name + "'] a.delrow").click(delRow);
141            jQuery(".image").css({ backgroundImage: "url(" + opts.PATH_IMG + name + ")" });
142          }
143          imgwidget.find("tr[id='" + name + "'] a.setbck").click(setbck);
144
145        }
146      }
147
148      displayUploadForm();
149    }
150
151    function buildElement(id) {
152      var pos = jQuery("#" + id).offset();
153      var divid = "uploadwidget" + id
154      var formid = "idOfFormElement" + id;
155      var iframeid = "idOfIframeElement" + id;
156
157      jQuery('#result').append(jQuery('<div id="' + divid + '"></div>'));
158      jQuery('#result').append(jQuery('<div id="' + divid + '_imgmgt" class="filemgt"><table></table><div class="paginate"></div></div>'));
159      uploadwidget = jQuery('#' + divid);
160      imgwidget = jQuery('#' + divid + '_imgmgt');
161      if (jQuery(".image").hasClass("texture"))
162        imgwidget.find("table").append("<tr><td>" + opts.TABLEHEAD.name + "</td><td>" + opts.TABLEHEAD.imgname + "</td><td>" + opts.TABLEHEAD.operation + "</td></tr>");
163
164      uploadwidget.append('<iframe name="' + iframeid + '" id="' + iframeid + '" style="width:0px;height:0px" src="' + opts.SCRIPT + '?action=getimages&root=' + opts.PATH +
165       '&root_img=' + opts.PATH_IMG +
166       '&root_theme=' + opts.PATH_THEME + '"></iframe>');
167
168      uploadwidget.append(jQuery('<form action="' + opts.SCRIPT + '?action=addimg&root=' + opts.PATH +
169       '&root_img=' + opts.PATH_IMG +
170       '&root_theme=' + opts.PATH_THEME + '" enctype="multipart/form-data" method="post" target="' + iframeid + '"></form>'));
171      uploadwidget.find('form').append('<input id="fileupload" type="file" name="files" class="file" >');
172
173      uploadwidget.css("position", "absolute")
174      uploadwidget.css("top", pos.top);
175      uploadwidget.css("left", pos.left);
176      uploadwidget.find('iframe').hide();
177
178      uploadwidget.find('iframe').bind('load', loaddetails);
179      jQuery("#" + id).hide();
180      uploadwidget.find('.file').change(onChange);
181
182    }
183
184
185  }
186
187  jQuery(document).ready(function () {
188    jQuery(window).load(function (i) {
189     if(!jQuery.farbtastic)  return ;
190
191     var f = jQuery.farbtastic('#picker');
192      var p = jQuery('#picker').css('opacity', 1);
193      var selected;
194      jQuery('.colorwell')
195      .each(function () {
196        f.linkTo(this);
197        $(this).css('opacity', 1);
198
199      })
200      .focus(function () {
201        if (selected) {
202          $(selected).css('opacity', 1).removeClass('colorwell-selected');
203        }
204        f.linkTo(this);
205        p.css('opacity', 1);
206        $(selected = this).css('opacity', 1).addClass('colorwell-selected');
207        col = $(selected = this).css("backgroundColor");
208
209      });
210    });
211  });
212})(jQuery);
213
214
215
Note: See TracBrowser for help on using the repository browser.