]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarFields/Fields/File/SugarFieldFile.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / SugarFields / Fields / File / SugarFieldFile.js
1 /*********************************************************************************
2  * SugarCRM is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36         
37 if ( typeof(SUGAR.field) == 'undefined' ) {
38     SUGAR.field = new Object();
39 }
40
41 if ( typeof(SUGAR.field.file) == 'undefined' ) {
42     SUGAR.field.file = {
43         deleteAttachment: function(elemBaseName,docTypeName,elem) {
44             ajaxStatus.showStatus(SUGAR.language.get("Notes", "LBL_REMOVING_ATTACHMENT"));
45             elem.form.deleteAttachment.value=1;
46             elem.form.action.value="EditView";
47             SUGAR.dashlets.postForm(elem.form, SUGAR.field.file.deleteAttachmentCallbackGen(elemBaseName,docTypeName));
48             elem.form.deleteAttachment.value=0;
49             elem.form.action.value="";
50         },
51         deleteAttachmentCallbackGen: function(elemBaseName,docTypeName) {
52             return function(text) {
53                     if(text == 'true') {
54                             document.getElementById(elemBaseName+'_new').style.display = '';
55                             ajaxStatus.hideStatus();
56                             document.getElementById(elemBaseName+'_old').innerHTML = '';
57                     document.getElementById(docTypeName).disabled = false;
58                     document.getElementById(elemBaseName).value = '';
59                     } else {
60                             document.getElementById(elemBaseName+'_new').style.display = 'none';
61                             ajaxStatus.flashStatus(SUGAR.language.get('Notes', 'ERR_REMOVING_ATTACHMENT'), 2000);
62                     }
63             }
64         },
65         checkEapiLogin: function(res) {
66             var failedLogins = JSON.parse(res.responseText);
67             if ( failedLogins.length == 0 ) { return; }
68
69             for ( var idx in failedLogins ) {
70                 if(confirm(failedLogins[idx].label)) {
71                     window.open(failedLogins[idx].checkURL,'EAPM_CHECK_'+idx);
72                 } else {
73                     document.getElementById(res.argument.docTypeName).value = 'Sugar';
74                     document.getElementById(res.argument.docTypeName).onchange();
75                 }
76             }
77         },
78         setupEapiShowHide: function(elemBaseName,docTypeName,formName) {
79             var externalSearchToggle = function() {
80                 var moreElem = document.getElementById(elemBaseName + "_more");
81                 var hideMore = (moreElem.style.display == 'none');
82                 if ( hideMore ) {
83                     // We're hiding the "more" element, so we clicked on the "less" element and want to hide stuff
84                     moreElem.style.display = '';
85                     document.getElementById(elemBaseName + '_less').style.display = 'none';
86                     document.getElementById(elemBaseName + '_remoteNameSpan').style.display = 'none';
87                     document.getElementById(elemBaseName + '_file').disabled = false;
88                 } else {
89                     // We're not hiding the "more" element, so we clicked on the "more" element and want to show stuff
90                     moreElem.style.display = 'none';
91                     document.getElementById(elemBaseName + '_less').style.display = '';
92                     document.getElementById(elemBaseName + '_remoteNameSpan').style.display = '';
93                     document.getElementById(elemBaseName + '_file').disabled = true;
94                 }
95             }
96
97             var showHideFunc = function() {
98                 var docShowHideElem = document.getElementById(elemBaseName + "_externalApiSelector");
99                 
100                 var dropdownValue = document.getElementById(docTypeName).value;
101                 if ( typeof(SUGAR.eapm) != 'undefined' 
102                      && typeof(SUGAR.eapm[dropdownValue]) != 'undefined' 
103                      && typeof(SUGAR.eapm[dropdownValue].docSearch) != 'undefined'
104                      && SUGAR.eapm[dropdownValue].docSearch ) {
105                     docShowHideElem.style.display = '';
106                     
107
108                     // Double check to make sure their login is valid
109                     YAHOO.util.Connect.asyncRequest('GET', 'index.php?module=EAPM&action=CheckLogins&to_pdf=1&api='+dropdownValue,{success:SUGAR.field.file.checkEapiLogin,argument:{'elemBaseName':elemBaseName,'docTypeName':docTypeName}});
110
111                     // Start a refresh of the document cache in the background. Thanks AJAX!
112                     YAHOO.util.Connect.asyncRequest('GET', 'index.php?module=EAPM&action=flushFileCache&to_pdf=1&api='+dropdownValue,{});
113                 } else {
114                     docShowHideElem.style.display = 'none';
115                     document.getElementById(elemBaseName + '_file').disabled = false;
116                 }
117                 // Update the quick search
118                 sqs_objects[formName+"_"+elemBaseName+"_remoteName"].api = dropdownValue;
119
120                 
121                 // Now time to see if we can select security options
122                 var secLevelBoxElem = document.getElementById(elemBaseName + '_securityLevelBox');
123                 var secLevelElem = document.getElementById(elemBaseName + '_securityLevel');
124                 
125                 secLevelElem.options.length = 0;
126                 
127                 if ( SUGAR.eapm[dropdownValue] && SUGAR.eapm[dropdownValue].sharingOptions ) {
128                     var opts = SUGAR.eapm[dropdownValue].sharingOptions;
129                     var i = 0;
130
131                     for ( idx in opts ) {
132                         secLevelElem.options[i] = new Option(SUGAR.language.get('app_strings',opts[idx]),idx,false,false);
133                         i++;
134                     }
135                     
136                     secLevelBoxElem.style.display='';
137                 } else {
138                     secLevelBoxElem.style.display='none';
139                 }
140                 
141             }
142             document.getElementById(docTypeName).onchange = showHideFunc;
143
144             document.getElementById(elemBaseName + '_externalApiLabel').onclick = externalSearchToggle;
145             showHideFunc();
146         },
147         
148 // Select button / popup related functions
149         openPopup: function(elemBaseName) {
150             window.open('index.php?module=Documents&action=extdoc&isPopup=1&elemBaseName='+elemBaseName+'&apiName='+document.getElementById('doc_type').value,'sugarPopup','width=600,height=400,menubar=no,toolbar=no,status=no,resizeable=yes,scrollbars=yes'); 
151         },
152         
153         clearRemote: function(elemBaseName) {
154             document.getElementById('doc_id').value = '';
155             document.getElementById(elemBaseName).value = '';
156             document.getElementById(elemBaseName + '_remoteName').value = '';
157             document.getElementById('doc_url').value = '';
158         },
159
160         populateFromPopup: function(elemBaseName, docId, docName, docUrl, docDirectUrl) {
161             document.getElementById('doc_id').value = docId;
162             document.getElementById(elemBaseName).value = docId;
163             document.getElementById(elemBaseName + '_remoteName').value = docName;
164             document.getElementById('doc_url').value = docUrl;
165         },
166         
167         getFileExtension:function(fileName) {
168             var lastindex = fileName.lastIndexOf(".");   
169             if(lastindex == -1)
170                 return false;
171             else
172                 return fileName.substr(++lastindex);
173         },
174         isFileExtensionValid: function(fileName) {
175             var docType = document.getElementById('doc_type').value;
176             var fileExtension = this.getFileExtension(fileName);
177             
178             if( typeof(SUGAR.eapm[docType]) == 'undefined' || ! fileExtension || ! SUGAR.eapm[docType].restrictUploadsByExtension ){
179                 return true;
180             }   
181             var whiteSuffixlist = SUGAR.eapm[docType]['restrictUploadsByExtension']; 
182             if(whiteSuffixlist.constructor == Array){
183                 var results = false;
184                 for(var i=0;i<whiteSuffixlist.length;i++){
185                     if( fileExtension.toLowerCase() == whiteSuffixlist[i].toLowerCase() ){
186                         return true;
187                     }
188                 }
189             }
190             return results;
191         },
192         
193         checkFileExtension: function(e,obj) {
194             var sff = SUGAR.field.file; //Scope is set to element.
195             var fileEl = document.getElementById(obj.fileEl);
196             var fileName = fileEl.value;
197             
198             var isValid = sff.isFileExtensionValid(fileName);
199             if( !isValid ){
200                 var errorPannel = new YAHOO.widget.SimpleDialog('sugarMsgWindow', {
201                                 width: '240px',visible: true, fixedcenter: true,constraintoviewport: true,
202                         draggable: true,type:'alert',modal:true,id:'sugarMsgWindow',close:true
203                         });
204                         errorPannel.setBody(SUGAR.language.get("app_strings", "LBL_INVALID_FILE_EXTENSION"));
205                         errorPannel.render(document.body);
206                         errorPannel.show();
207                         fileEl.value = '';
208                         document.getElementById(obj.targEl).value = '';
209             }
210         }
211     }
212 }