text: this.ckan.module('image-upload',function($,_){return{options:{is_url:false,is_upload:false,field_upload:'image_upload',field_url:'image_url',field_clear:'clear_upload',field_name:'name',upload_label:'',i18n:{upload:_('Upload'),url:_('Link'),remove:_('Remove'),upload_label:_('Image'),label_for_url:_('URL'),label_for_upload:_('File'),upload_tooltip:_('Upload a file on your computer'),url_tooltip:_('Link to a URL on the internet (you can also link to an API)')}},_nameIsDirty:false,initialize:function(){$.proxyAll(this,/_on/);var options=this.options;var field_upload='input[name="'+options.field_upload+'"]';var field_url='input[name="'+options.field_url+'"]';var field_clear='input[name="'+options.field_clear+'"]';var field_name='input[name="'+options.field_name+'"]';this.input=$(field_upload,this.el);this.field_url=$(field_url,this.el).parents('.control-group');this.field_image=this.input.parents('.control-group');this.field_url_input=$('input',this.field_url);this.field_name=this.el.parents('form').find(field_name);this.label_location=$('label[for="field-image-url"]');this.is_data_resource=(this.options.field_url==='url')&&(this.options.field_upload==='upload');var checkbox=$(field_clear,this.el);if(checkbox.length>0){checkbox.parents('.control-group').remove();}
  this.field_clear=$('<input type="hidden" name="clear_upload">').appendTo(this.el);this.button_url=$('<a href="javascript:;" class="btn"><i class="icon-globe"></i>'+this.i18n('url')+'</a>').prop('title',this.i18n('url_tooltip')).on('click',this._onFromWeb).insertAfter(this.input);this.button_upload=$('<a href="javascript:;" class="btn"><i class="icon-cloud-upload"></i>'+this.i18n('upload')+'</a>').insertAfter(this.input);$('<a href="javascript:;" class="btn btn-danger btn-remove-url">'+this.i18n('remove')+'</a>').prop('title',this.i18n('remove')).on('click',this._onRemove).insertBefore(this.field_url_input);$('label[for="field-image-upload"]').text(options.upload_label||this.i18n('upload_label'));this.input.on('mouseover',this._onInputMouseOver).on('mouseout',this._onInputMouseOut).on('change',this._onInputChange).prop('title',this.i18n('upload_tooltip')).css('width',this.button_upload.outerWidth());this.fields=$('<i />').add(this.button_upload).add(this.button_url).add(this.input).add(this.field_url).add(this.field_image);this.field_name.on('change',this._onModifyName);if(this.field_name.val()){this._nameIsDirty=true;}
  if(options.is_url){this._showOnlyFieldUrl();this._updateUrlLabel(this.i18n('label_for_url'));}else if(options.is_upload){this._showOnlyFieldUrl();this.field_url_input.prop('readonly',true);var filename=this._fileNameFromUpload(this.field_url_input.val());this.field_url_input.val(filename);this._updateUrlLabel(this.i18n('label_for_upload'));}else{this._showOnlyButtons();}},_fileNameFromUpload:function(url){url=url.substring(0,(url.indexOf("#")===-1)?url.length:url.indexOf("#"));url=url.substring(0,(url.indexOf("?")===-1)?url.length:url.indexOf("?"));url=url.substring(url.lastIndexOf("/")+1,url.length);return url;},_updateUrlLabel:function(label_text){if(!this.is_data_resource){return;}
  this.label_location.text(label_text);},_onFromWeb:function(){this._showOnlyFieldUrl();this.field_url_input.focus().on('blur',this._onFromWebBlur);if(this.options.is_upload){this.field_clear.val('true');}
  this._updateUrlLabel(this.i18n('label_for_url'));},_onRemove:function(){this._showOnlyButtons();this.field_url_input.val('');this.field_url_input.prop('readonly',false);this.field_clear.val('true');},_onInputChange:function(){var file_name=this.input.val().split(/^C:\\fakepath\\/).pop();this.field_url_input.val(file_name);this.field_url_input.prop('readonly',true);this.field_clear.val('');this._showOnlyFieldUrl();this._autoName(file_name);this._updateUrlLabel(this.i18n('label_for_upload'));},_showOnlyButtons:function(){this.fields.hide();this.button_upload.add(this.field_image).add(this.button_url).add(this.input).show();},_showOnlyFieldUrl:function(){this.fields.hide();this.field_url.show();},_onInputMouseOver:function(){this.button_upload.addClass('hover');},_onInputMouseOut:function(){this.button_upload.removeClass('hover');},_onModifyName:function(){this._nameIsDirty=true;},_onFromWebBlur:function(){var url=this.field_url_input.val().match(/([^\/]+)\/?$/)
  if(url){this._autoName(url.pop());}},_autoName:function(name){if(!this._nameIsDirty){this.field_name.val(name);}}};});