Browse Source

tool announcement: moving embeded javascript to file #4617

Laurent Opprecht 13 years ago
parent
commit
ab0bc8fc26

+ 16 - 147
main/announcements/announcements.inc.php

@@ -832,153 +832,22 @@ class AnnouncementManager  {
 	* this goes into the $htmlHeadXtra[] array
 	*/
 	public static function to_javascript() {
-		return "<script type=\"text/javascript\" language=\"JavaScript\">
-	
-		<!-- Begin javascript menu swapper
-	
-		function move(fbox,	tbox)
-		{
-			var	arrFbox	= new Array();
-			var	arrTbox	= new Array();
-			var	arrLookup =	new	Array();
-	
-			var	i;
-			for	(i = 0;	i <	tbox.options.length; i++)
-			{
-				arrLookup[tbox.options[i].text]	= tbox.options[i].value;
-				arrTbox[i] = tbox.options[i].text;
-			}
-	
-			var	fLength	= 0;
-			var	tLength	= arrTbox.length;
-	
-			for(i =	0; i < fbox.options.length;	i++)
-			{
-				arrLookup[fbox.options[i].text]	= fbox.options[i].value;
-	
-				if (fbox.options[i].selected &&	fbox.options[i].value != \"\")
-				{
-					arrTbox[tLength] = fbox.options[i].text;
-					tLength++;
-				}
-				else
-				{
-					arrFbox[fLength] = fbox.options[i].text;
-					fLength++;
-				}
-			}
-	
-			arrFbox.sort();
-			arrTbox.sort();
-	
-			var arrFboxGroup = new Array();
-			var arrFboxUser = new Array();
-			var prefix_x;
-	
-			for (x = 0; x < arrFbox.length; x++) {
-				prefix_x = arrFbox[x].substring(0,2);
-				if (prefix_x == 'G:') {
-					arrFboxGroup.push(arrFbox[x]);
-				} else {
-					arrFboxUser.push(arrFbox[x]);
-				}
-			}
-	
-			arrFboxGroup.sort();
-			arrFboxUser.sort();
-			arrFbox = arrFboxGroup.concat(arrFboxUser);
-	
-			var arrTboxGroup = new Array();
-			var arrTboxUser = new Array();
-			var prefix_y;
-	
-			for (y = 0; y < arrTbox.length; y++) {
-				prefix_y = arrTbox[y].substring(0,2);
-				if (prefix_y == 'G:') {
-					arrTboxGroup.push(arrTbox[y]);
-				} else {
-					arrTboxUser.push(arrTbox[y]);
-				}
-			}
-	
-			arrTboxGroup.sort();
-			arrTboxUser.sort();
-			arrTbox = arrTboxGroup.concat(arrTboxUser);
-	
-			fbox.length	= 0;
-			tbox.length	= 0;
-	
-			var	c;
-			for(c =	0; c < arrFbox.length; c++)
-			{
-				var	no = new Option();
-				no.value = arrLookup[arrFbox[c]];
-				no.text	= arrFbox[c];
-				fbox[c]	= no;
-			}
-			for(c =	0; c < arrTbox.length; c++)
-			{
-				var	no = new Option();
-				no.value = arrLookup[arrTbox[c]];
-				no.text	= arrTbox[c];
-				tbox[c]	= no;
-			}
-		}
-	
-		function validate()
-		{
-			var	f =	document.new_calendar_item;
-			f.submit();
-			return true;
-		}
-	
-	
-		function selectAll(cbList, bSelect, showwarning) {
-	
-			if (document.getElementById('emailTitle').value==''){
-				document.getElementById('msg_error').innerHTML='".get_lang('FieldRequired')."';
-				document.getElementById('msg_error').style.display='block';
-				document.getElementById('emailTitle').focus();
-			} else {			
-				if (cbList.length <	1) {
-					//if (!confirm(\"".get_lang('Send2All')."\")) {
-					//	return false;
-					//}
-				}				
-				for	(var i=0; i<cbList.length; i++)
-				cbList[i].selected = cbList[i].checked = bSelect;				
-				document.f1.submit();
-			}	
-		}
-	
-		function reverseAll(cbList)
-		{
-			for	(var i=0; i<cbList.length; i++)
-			{
-				cbList[i].checked  = !(cbList[i].checked)
-				cbList[i].selected = !(cbList[i].selected)
-			}
-		}
-	
-	
-		function plus_attachment() {
-			if (document.getElementById('options').style.display == 'none') {
-				document.getElementById('options').style.display = 'block';
-				document.getElementById('plus').innerHTML='&nbsp;<img style=\"vertical-align:middle;\" src=\"../img/div_hide.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
-			} else {
-				document.getElementById('options').style.display = 'none';
-				document.getElementById('plus').innerHTML='&nbsp;<img style=\"vertical-align:middle;\" src=\"../img/div_show.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
-			}
-		}
-		
-	
-	
-	
-		//	End	-->
-		</script>";
+            $www = api_get_path(WEB_PATH);
+            if(api_get_setting('server_type') == 'test')
+            {
+                $src = $www . 'main/announcements/resources/js/main.js';            
+            }
+            else
+            {
+                $src = $www . 'main/announcements/resources/js/main.min.js'; 
+            }
+            $result = Javascript::tag($src);
+            
+            $code = Javascript::get_lang('FieldRequired', 'Send2All', 'AddAnAttachment');            
+            $result .= Javascript::tag_code($code);
+            return $result;
 	}
-	
-	
+        
 	/*
 				SENT_TO_FORM
 	*/
@@ -1204,7 +1073,7 @@ class AnnouncementManager  {
 	 * @return int
 	 */
 	public static function edit_announcement_attachment_file($id_attach, $file, $file_comment) {
-		global $_course;
+		global $_course; 
 		$tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
 	    $return = 0;
 	    $course_id = api_get_course_int_id();

+ 146 - 0
main/announcements/resources/js/main.js

@@ -0,0 +1,146 @@
+// Begin javascript menu swapper
+
+function move(fbox, tbox) {
+    "use strict";
+    var arrFbox = [];
+    var arrTbox = [];
+    var arrLookup = [];
+
+    var i;
+    for (i = 0; i < tbox.options.length; i++) {
+        arrLookup[tbox.options[i].text] = tbox.options[i].value;
+        arrTbox[i] = tbox.options[i].text;
+    }
+
+    var fLength = 0;
+    var tLength = arrTbox.length;
+
+    for (i = 0; i < fbox.options.length; i++)
+    {
+        arrLookup[fbox.options[i].text] = fbox.options[i].value;
+
+        if (fbox.options[i].selected && fbox.options[i].value != "")
+        {
+            arrTbox[tLength] = fbox.options[i].text;
+            tLength++;
+        } 
+        else 
+        {
+            arrFbox[fLength] = fbox.options[i].text;
+            fLength++;
+        }
+    }
+    
+    arrFbox.sort();
+    arrTbox.sort();
+    
+    var arrFboxGroup = [];
+    var arrFboxUser = [];
+    var prefix_x;
+    
+    var x;
+    for (x = 0; x < arrFbox.length; x++) {
+        prefix_x = arrFbox[x].substring(0, 2);
+        if (prefix_x == 'G:') {
+            arrFboxGroup.push(arrFbox[x]);
+        } else {
+            arrFboxUser.push(arrFbox[x]);
+        }
+    }
+    
+    arrFboxGroup.sort();
+    arrFboxUser.sort();
+    arrFbox = arrFboxGroup.concat(arrFboxUser);
+    
+    var arrTboxGroup = [];
+    var arrTboxUser = [];
+    var prefix_y;
+    
+    var y;
+    for (y = 0; y < arrTbox.length; y++) {
+        prefix_y = arrTbox[y].substring(0, 2);
+        if (prefix_y == 'G:') {
+            arrTboxGroup.push(arrTbox[y]);
+        } else {
+            arrTboxUser.push(arrTbox[y]);
+        }
+    }
+    
+    arrTboxGroup.sort();
+    arrTboxUser.sort();
+    arrTbox = arrTboxGroup.concat(arrTboxUser);
+    
+    fbox.length = 0;
+    tbox.length = 0;
+    
+    var c;
+    for (c = 0; c < arrFbox.length; c++) 
+    {
+        var no = new Option();
+        no.value = arrLookup[arrFbox[c]];
+        no.text = arrFbox[c];
+        fbox[c] = no;
+    }
+    for (c = 0; c < arrTbox.length; c++) 
+    {
+        var no = new Option();
+        no.value = arrLookup[arrTbox[c]];
+        no.text = arrTbox[c];
+        tbox[c] = no;
+    }
+}
+
+function validate() 
+{
+    "use strict";
+    var f = document.new_calendar_item;
+    f.submit();
+    return true;
+}
+
+
+function selectAll(cbList, bSelect, showwarning) {
+    "use strict";
+    
+    if (document.getElementById('emailTitle').value == '') {
+        document.getElementById('msg_error').innerHTML = lang.FieldRequired;
+        document.getElementById('msg_error').style.display = 'block';
+        document.getElementById('emailTitle').focus();
+    } else {
+        //if (cbList.length < 1) {
+        //if (!confirm(lang.Send2All)) {
+        //	return false;
+        //}
+        //}
+        var i;
+        for (i = 0; i < cbList.length; i++)
+        {
+            cbList[i].selected = cbList[i].checked = bSelect;
+        }
+        document.f1.submit();
+    }
+}
+
+function reverseAll(cbList) 
+{
+    "use strict";
+    var i;
+    for (i = 0; i < cbList.length; i++) 
+    {
+        cbList[i].checked = !(cbList[i].checked);
+        cbList[i].selected = !(cbList[i].selected);
+    }
+}
+
+
+function plus_attachment() {
+    "use strict";
+    if (document.getElementById('options').style.display == 'none') {
+        document.getElementById('options').style.display = 'block';
+        document.getElementById('plus').innerHTML = '&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;' + lang.AddAnAttachment;
+    } else {
+        document.getElementById('options').style.display = 'none';
+        document.getElementById('plus').innerHTML = '&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;' + lang.AddAnAttachment;
+    }
+}
+// End

+ 4 - 0
main/announcements/resources/js/main.min.js

@@ -0,0 +1,4 @@
+function move(b,d){var e=[],f=[],i=[],a;for(a=0;a<d.options.length;a++)i[d.options[a].text]=d.options[a].value,f[a]=d.options[a].text;var c=0,h=f.length;for(a=0;a<b.options.length;a++)i[b.options[a].text]=b.options[a].value,b.options[a].selected&&""!=b.options[a].value?(f[h]=b.options[a].text,h++):(e[c]=b.options[a].text,c++);e.sort();f.sort();a=[];var c=[],g;for(g=0;g<e.length;g++)h=e[g].substring(0,2),"G:"==h?a.push(e[g]):c.push(e[g]);a.sort();c.sort();e=a.concat(c);a=[];c=[];for(g=0;g<f.length;g++)h=
+f[g].substring(0,2),"G:"==h?a.push(f[g]):c.push(f[g]);a.sort();c.sort();f=a.concat(c);b.length=0;for(a=d.length=0;a<e.length;a++)c=new Option,c.value=i[e[a]],c.text=e[a],b[a]=c;for(a=0;a<f.length;a++)c=new Option,c.value=i[f[a]],c.text=f[a],d[a]=c}function validate(){document.new_calendar_item.submit();return!0}
+function selectAll(b,d){if(""==document.getElementById("emailTitle").value)document.getElementById("msg_error").innerHTML=lang.FieldRequired,document.getElementById("msg_error").style.display="block",document.getElementById("emailTitle").focus();else{var e;for(e=0;e<b.length;e++)b[e].selected=b[e].checked=d;document.f1.submit()}}function reverseAll(b){var d;for(d=0;d<b.length;d++)b[d].checked=!b[d].checked,b[d].selected=!b[d].selected}
+function plus_attachment(){"none"==document.getElementById("options").style.display?(document.getElementById("options").style.display="block",document.getElementById("plus").innerHTML='&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'+lang.AddAnAttachment):(document.getElementById("options").style.display="none",document.getElementById("plus").innerHTML='&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'+lang.AddAnAttachment)};

+ 2 - 0
main/inc/lib/autoload.class.php

@@ -34,6 +34,8 @@ class Autoload
         $result['Redirect'] = $dir . '/redirect.class.php';
         $result['Request'] = $dir . '/request.class.php';
         $result['AnnouncementEmail'] = $sys. 'announcements/announcement_email.class.php';
+        $result['Javascript'] = $dir . '/javascript.class.php';
+        $result['ClosureCompiler'] = $dir . '/closure_compiler.class.php';
         
         return $result;
     }

+ 92 - 0
main/inc/lib/closure_compiler.class.php

@@ -0,0 +1,92 @@
+<?php
+
+/**
+ * Proxy for the closure compiler. Allows to minify javascript files.
+ * This makes use of CURL and calls the closure service.
+ * 
+ * @license see /license.txt
+ * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
+ * @see http://closure-compiler.appspot.com/home
+ * @see https://developers.google.com/closure/compiler/
+ *
+ */
+class ClosureCompiler
+{
+
+    const PARAM_OUTPUT_FORMAT = 'output_format';
+    const PARAM_OUTPUT_INFO = 'output_info';
+    const PARAM_COMPILATION_LEVEL = 'compilation_level';
+    const PARAM_JS_CODE = 'js_code';
+    const LEVEL_SIMPLE = 'SIMPLE_OPTIMIZATIONS';
+    const LEVEL_WHITESPACE = 'WHITESPACE_ONLY';
+    const LEVEL_ADVANCED = 'ADVANCED_OPTIMIZATIONS';
+    const OUTPUT_FORMAT_TEXT = 'text';
+    const OUTPUT_INFO_CODE = 'compiled_code';
+
+    /**
+     * Url of the service
+     * 
+     * @return string 
+     */
+    public static function url()
+    {
+        return 'closure-compiler.appspot.com/compile';
+    }
+
+    /**
+     * Post data the closure compiler service. By default it returns minimized code
+     * with the simple option.
+     * 
+     * @param string $code Javascript code to minimify
+     * @param array $params parameters to pass to the service
+     * @return string minimified code
+     */
+    public static function post($code, $params = array())
+    {
+        if (!isset($params[self::PARAM_OUTPUT_FORMAT]))
+        {
+            $params[self::PARAM_OUTPUT_FORMAT] = self::OUTPUT_FORMAT_TEXT;
+        }
+        if (!isset($params[self::PARAM_OUTPUT_INFO]))
+        {
+            $params[self::PARAM_OUTPUT_INFO] = self::OUTPUT_INFO_CODE;
+        }
+        if (!isset($params[self::PARAM_COMPILATION_LEVEL]))
+        {
+            $params[self::PARAM_JS_CODE] = $code;
+        }
+
+        $params[self::PARAM_COMPILATION_LEVEL] = self::LEVEL_SIMPLE;
+
+        $fields = array();
+        foreach ($params as $key => $value)
+        {
+            $fields[] = $key . '=' . urlencode($value);
+        }
+        $fields = implode('&', $fields);
+
+        $headers = array("Content-type: application/x-www-form-urlencoded");
+
+        $url = self::url();
+
+        $ch = curl_init();
+
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
+        //curl_setopt($ch, CURLOPT_HEADER, false);
+        //curl_setopt($ch, CURLOPT_VERBOSE, true);
+
+        $content = curl_exec($ch);
+        $error = curl_error($ch);
+        $info = curl_getinfo($ch);
+
+        curl_close($ch);
+
+        return $content;
+    }
+
+}

+ 91 - 0
main/inc/lib/javascript.class.php

@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * Javascript utility functions.
+ *
+ * @license see /license.txt
+ * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
+ */
+class Javascript
+{
+
+    /**
+     * Minimify Javascript.
+     * 
+     * If called with a path 
+     * 
+     *  input:   /dir/dir/file.js 
+     *  returns: /dir/dir/file.min.js
+     * 
+     * Otherwise returns the actual code.
+     * 
+     * @param string $arg either a path or actual code
+     * @return string either a path to minified content (ends with min.js) or actual code
+     */
+    public static function minify($arg)
+    {
+        if (is_readable($arg))
+        {
+            $path = $arg;
+            $code = file_get_contents($path);
+            $code = ClosureCompiler::post($code);
+
+            $min_path = $path;
+            $min_path = str_replace('.js', '', $min_path);
+            $min_path .= '.min.js';
+            file_put_contents($min_path, $code);
+            return $min_path;
+        }
+        else
+        {
+            return ClosureCompiler::post($code);
+        }
+    }
+
+    /**
+     * Returns lang object that contains the translation.
+     * 
+     *   Javascript::get_lang('string1', 'string2', 'string3');
+     * 
+     * returns
+     * 
+     * if(typeof(lang) == "undefined")
+     * {
+     *      var lang = {};
+     * }
+     * lang.string1 = "...";
+     * lang.string2 = "...";
+     * lang.string3 = "...
+     * 
+     * @param type $_
+     * @return type 
+     */
+    public static function get_lang($_)
+    {
+        $result = array();
+        $result[] = 'if(typeof(lang) == "undefined")';
+        $result[] = '{';
+        $result[] = '   var lang = {};';
+        $result[] = '}';
+
+        $keys = func_get_args();
+        foreach ($keys as $key)
+        {
+            $value = get_lang($key);
+            $result[] = 'lang.' . $key . ' = "' . $value . '";';
+        }
+        return implode("\n", $result);
+    }
+
+    public static function tag($src)
+    {
+        return '<script type="text/javascript" src="' . $src . '"></script>';
+    }
+
+    public static function tag_code($code)
+    {
+        $new_line = "\n";
+        return '<script type="text/javascript">' . $new_line . $code . $new_line . '</script>';
+    }
+
+}