[js] shrink htmlhelpers, extract massmailing functionality, jslint
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 16 Jul 2009 16:34:02 +0200
changeset 2385 8fa16dc4b8c8
parent 2384 6220783caa4b
child 2386 b246d5cee44e
[js] shrink htmlhelpers, extract massmailing functionality, jslint
web/data/cubicweb.htmlhelpers.js
web/data/cubicweb.massmailing.js
web/data/cubicweb.python.js
web/views/massmailing.py
--- a/web/data/cubicweb.htmlhelpers.js	Thu Jul 16 16:30:58 2009 +0200
+++ b/web/data/cubicweb.htmlhelpers.js	Thu Jul 16 16:34:02 2009 +0200
@@ -16,78 +16,6 @@
     return '';
 }
 
-// XXX this is used exactly ONCE in web/views/massmailing.py
-function insertText(text, areaId) {
-    var textarea = jQuery('#' + areaId);
-    if (document.selection) { // IE
-        var selLength;
-        textarea.focus();
-        sel = document.selection.createRange();
-        selLength = sel.text.length;
-        sel.text = text;
-        sel.moveStart('character', selLength-text.length);
-        sel.select();
-    } else if (textarea.selectionStart || textarea.selectionStart == '0') { // mozilla
-        var startPos = textarea.selectionStart;
-        var endPos = textarea.selectionEnd;
-	// insert text so that it replaces the [startPos, endPos] part
-        textarea.value = textarea.value.substring(0,startPos) + text + textarea.value.substring(endPos,textarea.value.length);
-	// set cursor pos at the end of the inserted text
-        textarea.selectionStart = textarea.selectionEnd = startPos+text.length;
-        textarea.focus();
-    } else { // safety belt for other browsers
-        textarea.value += text;
-    }
-}
-
-/* taken from dojo toolkit */
-// XXX this looks unused
-function setCaretPos(element, start, end){
-    if(!end){ end = element.value.length; }  // NOTE: Strange - should be able to put caret at start of text?
-    // Mozilla
-    // parts borrowed from http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130
-    if(element.setSelectionRange){
-        element.focus();
-        element.setSelectionRange(start, end);
-    } else if(element.createTextRange){ // IE
-        var range = element.createTextRange();
-        with(range){
-            collapse(true);
-            moveEnd('character', end);
-            moveStart('character', start);
-            select();
-        }
-    } else { //otherwise try the event-creation hack (our own invention)
-        // do we need these?
-        element.value = element.value;
-        element.blur();
-        element.focus();
-        // figure out how far back to go
-        var dist = parseInt(element.value.length)-end;
-        var tchar = String.fromCharCode(37);
-        var tcc = tchar.charCodeAt(0);
-        for(var x = 0; x < dist; x++){
-            var te = document.createEvent("KeyEvents");
-            te.initKeyEvent("keypress", true, true, null, false, false, false, false, tcc, tcc);
-            element.dispatchEvent(te);
-        }
-    }
-}
-
-
-// XXX this looks unused
-function setProgressMessage(label) {
-    var body = document.getElementsByTagName('body')[0];
-    body.appendChild(DIV({id: 'progress'}, label));
-    jQuery('#progress').show();
-}
-
-// XXX this looks unused
-function resetProgressMessage() {
-    var body = document.getElementsByTagName('body')[0];
-    jQuery('#progress').hide();
-}
-
 
 /* set body's cursor to 'progress' */
 function setProgressCursor() {
@@ -151,7 +79,7 @@
 
 
 /* toggles visibility of login popup div */
-// XXX used exactly ONCE
+// XXX used exactly ONCE in basecomponents
 function popupLoginBox() {
     toggleVisibility('popupLoginBox');
     jQuery('#__login:visible').focus();
@@ -193,14 +121,6 @@
     forEach(filter(filterfunc, elements), function(cb) {cb.checked=checked;});
 }
 
-/* centers an HTML element on the screen */
-// XXX looks unused
-function centerElement(obj){
-    var vpDim = getViewportDimensions();
-    var elemDim = getElementDimensions(obj);
-    setElementPosition(obj, {'x':((vpDim.w - elemDim.w)/2),
-			     'y':((vpDim.h - elemDim.h)/2)});
-}
 
 /* this function is a hack to build a dom node from html source */
 function html2dom(source) {
@@ -223,12 +143,6 @@
 function isTextNode(domNode) { return domNode.nodeType == 3; }
 function isElementNode(domNode) { return domNode.nodeType == 1; }
 
-// XXX this looks unused
-function changeLinkText(link, newText) {
-    jQuery(link).text(newText);
-}
-
-
 function autogrow(area) {
     if (area.scrollHeight > area.clientHeight && !window.opera) {
 	if (area.rows < 20) {
@@ -236,13 +150,6 @@
 	}
     }
 }
-
-// XXX this looks unused
-function limitTextAreaSize(textarea, size) {
-    var $area = jQuery(textarea);
-    $area.val($area.val().slice(0, size));
-}
-
 //============= page loading events ==========================================//
 
 CubicWeb.rounded = [
@@ -250,8 +157,6 @@
 		    ['div.boxTitle, div.boxPrefTitle, div.sideBoxTitle, th.month', 'top 6px']
 		    ];
 
-
-
 function roundedCorners(node) {
     node = jQuery(node);
     for(var r=0; r < CubicWeb.rounded.length; r++) {
@@ -259,7 +164,8 @@
     }
 }
 
-jQuery(document).ready(function () {roundedCorners(this.body)});
+jQuery(document).ready(function () {roundedCorners(this.body);});
+
+CubicWeb.provide('corners.js');
 
 CubicWeb.provide('htmlhelpers.js');
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/data/cubicweb.massmailing.js	Thu Jul 16 16:34:02 2009 +0200
@@ -0,0 +1,23 @@
+
+function insertText(text, areaId) {
+    var textarea = jQuery('#' + areaId);
+    if (document.selection) { // IE
+        var selLength;
+        textarea.focus();
+        sel = document.selection.createRange();
+        selLength = sel.text.length;
+        sel.text = text;
+        sel.moveStart('character', selLength-text.length);
+        sel.select();
+    } else if (textarea.selectionStart || textarea.selectionStart == '0') { // mozilla
+        var startPos = textarea.selectionStart;
+        var endPos = textarea.selectionEnd;
+	// insert text so that it replaces the [startPos, endPos] part
+        textarea.value = textarea.value.substring(0,startPos) + text + textarea.value.substring(endPos,textarea.value.length);
+	// set cursor pos at the end of the inserted text
+        textarea.selectionStart = textarea.selectionEnd = startPos+text.length;
+        textarea.focus();
+    } else { // safety belt for other browsers
+        textarea.value += text;
+    }
+}
--- a/web/data/cubicweb.python.js	Thu Jul 16 16:30:58 2009 +0200
+++ b/web/data/cubicweb.python.js	Thu Jul 16 16:34:02 2009 +0200
@@ -157,7 +157,7 @@
 // ========== ARRAY EXTENSIONS ========== ///
 Array.prototype.contains = function(element) {
     return findValue(this, element) != -1;
-}
+};
 
 // ========== END OF ARRAY EXTENSIONS ========== ///
 
@@ -201,7 +201,7 @@
  * [0,2,4,6,8]
  */
 function list(iterable) {
-    iterator = iter(iterable);
+    var iterator = iter(iterable);
     var result = [];
     while (true) {
 	/* iterates until StopIteration occurs */
@@ -267,14 +267,6 @@
 function min() { return listMin(arguments); }
 function max() { return listMax(arguments); }
 
-// tricky multiple assign
-// function assign(lst, varnames) {
-//     var length = min(lst.length, varnames.length);
-//     for(var i=0; i<length; i++) {
-// 	window[varnames[i]] = lst[i];
-//     }
-// }
-
 /*
  * >>> d = dict(["x", "y", "z"], [0, 1, 2])
  * >>> d['y']
@@ -335,7 +327,7 @@
 function makeConstructor(userctor) {
     return function() {
 	// this is a proxy to user's __init__
-	if(userctor) {
+	if (userctor) {
 	    userctor.apply(this, arguments);
 	}
     };
@@ -369,7 +361,7 @@
 	}
     }
     var userctor = basemeths['__init__'];
-    constructor = makeConstructor(userctor);
+    var constructor = makeConstructor(userctor);
 
     // python-like interface
     constructor.__name__ = name;
--- a/web/views/massmailing.py	Thu Jul 16 16:30:58 2009 +0200
+++ b/web/views/massmailing.py	Thu Jul 16 16:34:02 2009 +0200
@@ -123,7 +123,7 @@
 
     def call(self):
         req = self.req
-        req.add_js('cubicweb.widgets.js')
+        req.add_js('cubicweb.widgets.js', 'cubicweb.massmailing.js')
         req.add_css('cubicweb.mailform.css')
         from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
         form = self.vreg.select('forms', 'massmailing', self.req, rset=self.rset,