diff --git a/comment/comment.js b/comment/comment.js index 1cea723c3be..30f93ade908 100644 --- a/comment/comment.js +++ b/comment/comment.js @@ -17,303 +17,311 @@ * Comment Helper * @author Dongsheng Cai */ -// initialize commenting system -M.core_comment = (function(){ - function core_comment (args) { - core_comment.superclass.constructor.apply(this, arguments); - } - core_comment.NAME = "COMMENT"; - core_comment.ATTRS = { - options: {}, - lang: {} - }; - Y.extend(core_comment, Y.Base, { - api: M.cfg.wwwroot+'/comment/comment_ajax.php', - initializer: function(args) { - var scope = this; - this.client_id = args.client_id; - this.itemid = args.itemid; - this.commentarea = args.commentarea; - this.courseid = args.courseid; - this.contextid = args.contextid; - if (args.autostart) { - this.view(args.page); - } - if (args.notoggle) { - Y.one('#comment-link-'+this.client_id).setStyle('display', 'none'); - } - // load comments - Y.one('#comment-link-'+this.client_id).on('click', function(e) { - e.preventDefault(); - this.view(0); - return false; - }, this); - }, - post: function() { - var ta = Y.one('#dlg-content-'+this.client_id); - var scope = this; - var value = ta.get('value'); - if (value && value != mstr.moodle.addcomment) { - var params = {'content': value}; - this.request({ - action: 'add', - scope: scope, - params: params, - callback: function(id, obj, args) { - var scope = args.scope; - var cid = scope.client_id; - var ta = Y.one('#dlg-content-'+cid); - ta.set('value', ''); - var container = Y.one('#comment-list-'+cid); - var result = scope.render([obj], true); - var newcomment = Y.Node.create(result.html); - container.appendChild(newcomment); - var ids = result.ids; - var linktext = Y.one('#comment-link-text-'+cid); - linktext.set('innerHTML', mstr.moodle.comments + ' ('+obj.count+')'); - for(var i in ids) { - var attributes = { - color: { to: '#06e' }, - backgroundColor: { to: '#FFE390' } - }; - var anim = new YAHOO.util.ColorAnim(ids[i], attributes); - anim.animate(); - } - } - }, true); - } else { - var attributes = { - backgroundColor: { from: '#FFE390', to:'#FFFFFF' } - }; - var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes); - anim.animate(); - } - }, - request: function(args, noloading) { - var params = {}; - var scope = this; - if (args['scope']) { - scope = args['scope']; - } - //params['page'] = args.page?args.page:''; - params['env'] = ''; - // the form element only accept certain file types - params['sesskey'] = M.cfg.sesskey; - params['action'] = args.action?args.action:''; - params['client_id'] = this.client_id; - params['itemid'] = this.itemid; - params['area'] = this.commentarea; - params['courseid'] = this.courseid; - params['contextid'] = this.contextid; - if (args['params']) { - for (i in args['params']) { - params[i] = args['params'][i]; +M.core_comment = { + /** + * Initialize commenting system + */ + init: function(options) { + var CommentHelper = function(args) { + CommentHelper.superclass.constructor.apply(this, arguments); + } + CommentHelper.NAME = "COMMENT"; + CommentHelper.ATTRS = { + options: {}, + lang: {} + }; + Y.extend(CommentHelper, Y.Base, { + api: M.cfg.wwwroot+'/comment/comment_ajax.php', + initializer: function(args) { + var scope = this; + this.client_id = args.client_id; + this.itemid = args.itemid; + this.commentarea = args.commentarea; + this.courseid = args.courseid; + this.contextid = args.contextid; + if (args.autostart) { + this.view(args.page); } - } - var cfg = { - method: 'POST', - on: { - complete: function(id,o,p) { - if (!o) { - alert('IO FATAL'); - return; - } - var data = json_decode(o.responseText); - if (data.error) { - alert(data.error); - return false; - } else { - args.callback(id,data,p); - return true; - } - } - }, - arguments: { - scope: scope - }, - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'User-Agent': 'MoodleComment/3.0' - }, - data: build_querystring(params) - }; - if (args.form) { - cfg.form = args.form; - } - Y.io(this.api, cfg); - if (!noloading) { - this.wait(); - } - }, - render: function(list, newcmt) { - var ret = {}; - ret.ids = []; - var template = Y.one('#cmt-tmpl'); - var html = ''; - for(var i in list) { - var htmlid = 'comment-'+list[i].id+'-'+this.client_id; - var val = template.get('innerHTML'); - val = val.replace('___name___', list[i].username); - if (list[i]['delete']||newcmt) { - list[i].content = '
' + list[i].content; + if (args.notoggle) { + Y.one('#comment-link-'+this.client_id).setStyle('display', 'none'); } - val = val.replace('___time___', list[i].time); - val = val.replace('___picture___', list[i].avatar); - val = val.replace('___content___', list[i].content); - val = '
  • '+val+'
  • '; - ret.ids.push(htmlid); - html = (val+html); - } - ret.html = html; - return ret; - }, - load: function(page) { - var scope = this; - var container = Y.one('#comment-ctrl-'+this.client_id); - var params = { - 'page': page, - } - this.request({ - scope: scope, - params: params, - callback: function(id, ret, args) { - var linktext = Y.one('#comment-link-text-'+scope.client_id); - linktext.set('innerHTML', mstr.moodle.comments + ' ('+ret.count+')'); - var container = Y.one('#comment-list-'+scope.client_id); - var pagination = Y.one('#comment-pagination-'+scope.client_id); - if (ret.pagination) { - pagination.set('innerHTML', ret.pagination); - } else { - //empty paging bar - pagination.set('innerHTML', ''); - } - var result = scope.render(ret.list); - container.set('innerHTML', result.html); - args.scope.register_pagination(); - args.scope.register_delete_buttons(); - } - }); - }, - delete: function(id) { - var scope = this; - var params = {'commentid': id}; - - function remove_dom(type, anmi, cmt) { - cmt.remove(); - } - this.request({ - action: 'delete', - scope: scope, - params: params, - callback: function(id, resp, args) { - var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id; + // load comments + Y.one('#comment-link-'+this.client_id).on('click', function(e) { + e.preventDefault(); + this.view(0); + return false; + }, this); + }, + post: function() { + var ta = Y.one('#dlg-content-'+this.client_id); + var scope = this; + var value = ta.get('value'); + if (value && value != mstr.moodle.addcomment) { + var params = {'content': value}; + this.request({ + action: 'add', + scope: scope, + params: params, + callback: function(id, obj, args) { + var scope = args.scope; + var cid = scope.client_id; + var ta = Y.one('#dlg-content-'+cid); + ta.set('value', ''); + var container = Y.one('#comment-list-'+cid); + var result = scope.render([obj], true); + var newcomment = Y.Node.create(result.html); + container.appendChild(newcomment); + var ids = result.ids; + var linktext = Y.one('#comment-link-text-'+cid); + linktext.set('innerHTML', mstr.moodle.comments + ' ('+obj.count+')'); + for(var i in ids) { + var attributes = { + color: { to: '#06e' }, + backgroundColor: { to: '#FFE390' } + }; + var anim = new YAHOO.util.ColorAnim(ids[i], attributes); + anim.animate(); + } + } + }, true); + } else { var attributes = { - width:{to:0}, - height:{to:0} + backgroundColor: { from: '#FFE390', to:'#FFFFFF' } }; - var cmt = Y.one('#'+htmlid); - cmt.setStyle('overflow', 'hidden'); - var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut); - anim.onComplete.subscribe(remove_dom, cmt, this); + var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes); anim.animate(); } - }, true); - }, - register_actions: function() { - // add new comment - Y.one('#comment-action-post-'+this.client_id).on('click', function(e) { - e.preventDefault(); - this.post(); - return false; - }, this); - // cancel comment box - Y.one('#comment-action-cancel-'+this.client_id).on('click', function(e) { - e.preventDefault(); - this.view(0); - return false; - }, this); - }, - register_delete_buttons: function() { - var scope = this; - // page buttons - Y.all('div.comment-content a').each( - function(node, id) { - node.on('click', function(e, node) { - var id = node.get('id'); - var re = new RegExp("comment-delete-"+this.client_id+"-(\\d+)", "i"); - var result = id.match(re); - if (result[1]) { - this.delete(result[1]); + }, + request: function(args, noloading) { + var params = {}; + var scope = this; + if (args['scope']) { + scope = args['scope']; + } + //params['page'] = args.page?args.page:''; + params['env'] = ''; + // the form element only accept certain file types + params['sesskey'] = M.cfg.sesskey; + params['action'] = args.action?args.action:''; + params['client_id'] = this.client_id; + params['itemid'] = this.itemid; + params['area'] = this.commentarea; + params['courseid'] = this.courseid; + params['contextid'] = this.contextid; + if (args['params']) { + for (i in args['params']) { + params[i] = args['params'][i]; + } + } + var cfg = { + method: 'POST', + on: { + complete: function(id,o,p) { + if (!o) { + alert('IO FATAL'); + return; + } + var data = json_decode(o.responseText); + if (data.error) { + alert(data.error); + return false; + } else { + args.callback(id,data,p); + return true; + } } - //this.load(result[1]); - }, scope, node); + }, + arguments: { + scope: scope + }, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'User-Agent': 'MoodleComment/3.0' + }, + data: build_querystring(params) + }; + if (args.form) { + cfg.form = args.form; } - ); - }, - register_pagination: function() { - var scope = this; - // page buttons - Y.all('#comment-paging-'+this.client_id+' a').each( - function(node, id) { - node.on('click', function(e, node) { - var id = node.get('id'); - var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i"); - var result = id.match(re); - this.load(result[1]); - }, scope, node); + Y.io(this.api, cfg); + if (!noloading) { + this.wait(); } - ); - }, - view: function(page) { - var container = Y.one('#comment-ctrl-'+this.client_id); - var ta = Y.one('#dlg-content-'+this.client_id); - var img = Y.one('#comment-img-'+this.client_id); - var d = container.getStyle('display'); - if (d=='none'||d=='') { - // show - this.load(page); - container.setStyle('display', 'block'); - img.src=M.cfg.wwwroot+'/pix/t/expanded.png'; - } else { - // hide - container.setStyle('display', 'none'); - img.src=M.cfg.wwwroot+'/pix/t/collapsed.png'; - ta.set('value',''); + }, + render: function(list, newcmt) { + var ret = {}; + ret.ids = []; + var template = Y.one('#cmt-tmpl'); + var html = ''; + for(var i in list) { + var htmlid = 'comment-'+list[i].id+'-'+this.client_id; + var val = template.get('innerHTML'); + val = val.replace('___name___', list[i].username); + if (list[i]['delete']||newcmt) { + list[i].content = '
    ' + list[i].content; + } + val = val.replace('___time___', list[i].time); + val = val.replace('___picture___', list[i].avatar); + val = val.replace('___content___', list[i].content); + val = '
  • '+val+'
  • '; + ret.ids.push(htmlid); + html = (val+html); + } + ret.html = html; + return ret; + }, + load: function(page) { + var scope = this; + var container = Y.one('#comment-ctrl-'+this.client_id); + var params = { + 'page': page, + } + this.request({ + scope: scope, + params: params, + callback: function(id, ret, args) { + var linktext = Y.one('#comment-link-text-'+scope.client_id); + linktext.set('innerHTML', mstr.moodle.comments + ' ('+ret.count+')'); + var container = Y.one('#comment-list-'+scope.client_id); + var pagination = Y.one('#comment-pagination-'+scope.client_id); + if (ret.pagination) { + pagination.set('innerHTML', ret.pagination); + } else { + //empty paging bar + pagination.set('innerHTML', ''); + } + var result = scope.render(ret.list); + container.set('innerHTML', result.html); + args.scope.register_pagination(); + args.scope.register_delete_buttons(); + } + }); + }, + delete: function(id) { + var scope = this; + var params = {'commentid': id}; + + function remove_dom(type, anmi, cmt) { + cmt.remove(); + } + this.request({ + action: 'delete', + scope: scope, + params: params, + callback: function(id, resp, args) { + var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id; + var attributes = { + width:{to:0}, + height:{to:0} + }; + var cmt = Y.one('#'+htmlid); + cmt.setStyle('overflow', 'hidden'); + var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut); + anim.onComplete.subscribe(remove_dom, cmt, this); + anim.animate(); + } + }, true); + }, + register_actions: function() { + // add new comment + Y.one('#comment-action-post-'+this.client_id).on('click', function(e) { + e.preventDefault(); + this.post(); + return false; + }, this); + // cancel comment box + var cancel = Y.one('#comment-action-cancel-'+this.client_id); + if (cancel) { + cancel.on('click', function(e) { + e.preventDefault(); + this.view(0); + return false; + }, this); + } + }, + register_delete_buttons: function() { + var scope = this; + // page buttons + Y.all('div.comment-content a').each( + function(node, id) { + node.on('click', function(e, node) { + var id = node.get('id'); + var re = new RegExp("comment-delete-"+this.client_id+"-(\\d+)", "i"); + var result = id.match(re); + if (result[1]) { + this.delete(result[1]); + } + //this.load(result[1]); + }, scope, node); + } + ); + }, + register_pagination: function() { + var scope = this; + // page buttons + Y.all('#comment-paging-'+this.client_id+' a').each( + function(node, id) { + node.on('click', function(e, node) { + var id = node.get('id'); + var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i"); + var result = id.match(re); + this.load(result[1]); + }, scope, node); + } + ); + }, + view: function(page) { + var container = Y.one('#comment-ctrl-'+this.client_id); + var ta = Y.one('#dlg-content-'+this.client_id); + var img = Y.one('#comment-img-'+this.client_id); + var d = container.getStyle('display'); + if (d=='none'||d=='') { + // show + this.load(page); + container.setStyle('display', 'block'); + img.src=M.cfg.wwwroot+'/pix/t/expanded.png'; + } else { + // hide + container.setStyle('display', 'none'); + img.src=M.cfg.wwwroot+'/pix/t/collapsed.png'; + ta.set('value',''); + } + //toggle_textarea.apply(ta, [false]); + //// reset textarea size + ta.on('click', function() { + this.toggle_textarea(true); + }, this) + //ta.onkeypress = function() { + //if (this.scrollHeight > this.clientHeight && !window.opera) + //this.rows += 1; + //} + ta.on('blur', function() { + this.toggle_textarea(false); + }, this); + this.register_actions(); + return false; + }, + toggle_textarea: function(focus) { + var t = Y.one('#dlg-content-'+this.client_id); + if (focus) { + if (t.get('value') == mstr.moodle.addcomment) { + t.set('value', ''); + t.setStyle('color', 'black'); + } + }else{ + if (t.get('value') == '') { + t.set('value', mstr.moodle.addcomment); + t.setStyle('color','grey'); + t.set('rows', 1); + } + } + }, + wait: function() { + var container = Y.one('#comment-list-'+this.client_id); + container.set('innerHTML', '
    '); } - //toggle_textarea.apply(ta, [false]); - //// reset textarea size - ta.on('click', function() { - this.toggle_textarea(true); - }, this) - //ta.onkeypress = function() { - //if (this.scrollHeight > this.clientHeight && !window.opera) - //this.rows += 1; - //} - ta.on('blur', function() { - this.toggle_textarea(false); - }, this); - this.register_actions(); - return false; - }, - toggle_textarea: function(focus) { - var t = Y.one('#dlg-content-'+this.client_id); - if (focus) { - if (t.get('value') == mstr.moodle.addcomment) { - t.set('value', ''); - t.setStyle('color', 'black'); - } - }else{ - if (t.get('value') == '') { - t.set('value', mstr.moodle.addcomment); - t.setStyle('color','grey'); - t.set('rows', 1); - } - } - }, - wait: function() { - var container = Y.one('#comment-list-'+this.client_id); - container.set('innerHTML', '
    '); - } - }); - return core_comment; -})(); + }); + + new CommentHelper(options); + } +}; diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index 048e7762bda..21f9311b300 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -463,14 +463,19 @@ class page_requirements_manager { * @param array $arguments and array of arguments to be passed to the function. * When generating the function call, this will be escaped using json_encode, * so passing objects and arrays should work. + * @param string $module optional js module name, if specified module is required and + * initialised before the call * @return required_js_function_call The required_js_function_call object. * This allows you to control when the link to the script is output by * calling methods like {@link required_js_function_call::in_head()}, * {@link required_js_function_call::on_dom_ready()} or * {@link required_js_function_call::after_delay()} methods. */ - public function js_function_call($function, $arguments = array()) { - $requirement = new required_js_function_call($this, $function, $arguments); + public function js_function_call($function, $arguments = array(), $module = null) { + if ($module) { + $this->js_module($module); + } + $requirement = new required_js_function_call($this, $function, $arguments, $module); $this->requiredjscode[] = $requirement; return $requirement; } @@ -517,7 +522,7 @@ class page_requirements_manager { * equivalent in the current language. * * The arguments to this function are just like the arguments to get_string - * except that $module is not optional, and there are limitations on how you + * except that $component is not optional, and there are limitations on how you * use $a. Because each string is only stored once in the JavaScript (based * on $identifier and $module) you cannot get the same string with two different * values of $a. If you try, an exception will be thrown. @@ -530,16 +535,16 @@ class page_requirements_manager { * @param string $module the language file to look in. * @param mixed $a any extra data to add into the string (optional). */ - public function string_for_js($identifier, $module, $a = NULL) { - $string = get_string($identifier, $module, $a); - if (!$module) { + public function string_for_js($identifier, $component, $a = NULL) { + $string = get_string($identifier, $component, $a); + if (!$component) { throw new coding_exception('The $module parameter is required for page_requirements_manager::string_for_js.'); } - if (isset($this->stringsforjs[$module][$identifier]) && $this->stringsforjs[$module][$identifier] != $string) { + if (isset($this->stringsforjs[$component][$identifier]) && $this->stringsforjs[$component][$identifier] != $string) { throw new coding_exception("Attempt to re-define already required string '$identifier' " . - "from lang file '$module'. Did you already ask for it with a different \$a?"); + "from lang file '$component'. Did you already ask for it with a different \$a?"); } - $this->stringsforjs[$module][$identifier] = $string; + $this->stringsforjs[$component][$identifier] = $string; } /** @@ -560,20 +565,20 @@ class page_requirements_manager { * * * @param array $identifiers An array of desired strings - * @param string $module The module to load for + * @param string $component The module to load for * @param mixed $a This can either be a single variable that gets passed as extra * information for every string or it can be an array of mixed data where the * key for the data matches that of the identifier it is meant for. * */ - public function strings_for_js($identifiers, $module, $a=NULL) { + public function strings_for_js($identifiers, $component, $a=NULL) { foreach ($identifiers as $key => $identifier) { if (is_array($a) && array_key_exists($key, $a)) { $extra = $a[$key]; } else { $extra = $a; } - $this->string_for_js($identifier, $module, $extra); + $this->string_for_js($identifier, $component, $extra); } } @@ -1252,6 +1257,7 @@ class required_js_object_init extends required_js_code { class required_js_function_call extends required_js_code { protected $function; protected $arguments; + protected $module; protected $delay = 0; /** @@ -1266,15 +1272,20 @@ class required_js_function_call extends required_js_code { * When generating the function call, this will be escaped using json_encode, * so passing objects and arrays should work. */ - public function __construct(page_requirements_manager $manager, $function, $arguments) { + public function __construct(page_requirements_manager $manager, $function, $arguments, $module) { parent::__construct($manager); $this->when = page_requirements_manager::WHEN_IN_YUI; $this->function = $function; $this->arguments = $arguments; + $this->module = $module; } public function get_js_code() { - return js_writer::function_call($this->function, $this->arguments, $this->delay); + $js = js_writer::function_call($this->function, $this->arguments, $this->delay); + if ($this->module) { + $js = "Y.use('$this->module', function() { $js });"; + } + return $js; } /** diff --git a/lib/commentlib.php b/lib/commentlib.php index 3aadfb5d918..2b865d2e607 100644 --- a/lib/commentlib.php +++ b/lib/commentlib.php @@ -274,8 +274,7 @@ EOD; $options->notoggle = true; } - $PAGE->requires->js_module('core_comment'); - $PAGE->requires->js_function_call('initialize_comment', array($options))->on_dom_ready(); + $PAGE->requires->js_function_call('M.core_comment.init', array($options), 'core_comment')->on_dom_ready(); if (!empty(self::$nonjs)) { return $this->print_comments($this->page, $return); @@ -287,13 +286,6 @@ EOD; // print html template if (empty($CFG->commentcommentcode) && !empty($this->viewcap)) { echo ''; - - // shared parameters for commenting - $params = new stdclass; - $params->courseid = $this->course->id; - $params->contextid = $this->contextid; - $PAGE->requires->data_for_js('comment_params', $params); - $CFG->commentcommentcode = true; } diff --git a/lib/javascript-static.js b/lib/javascript-static.js index a3e462b4c05..0019d880c81 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -24,14 +24,6 @@ function launch_filemanager(options) { }); } -function initialize_comment(options) { - Y.use('core_comment', function() { - new M.core_comment(options); - }); -} - -// old JS functions, to be converted soon - // === old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code === function popupchecker(msg) {