/** * Runs the widget */ public function run() { $id = $this->id; // Determine what options should be passed $options = array(); foreach ($this->_defaultButtons as $button) { $options[$button] = $this->buttons === null || array_search($button, $this->buttons) !== false; } if (isset($this->events)) { $options['events'] = $this->events; } $options = CJavascript::encode($options); echo CHtml::openTag('textarea', $this->htmlOptions); echo $this->value; Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').wysihtml5({$options});"); echo '</textarea>'; }
private function createInfiniteScrollScript() { $options = $this->buildInifiniteScrollOptions(); $contentLoadedCallback = CJavascript::encode($this->contentLoadedCallback); Yii::app()->clientScript->registerScript(uniqid(), "\$('{$this->contentSelector}').infinitescroll({$options}, {$contentLoadedCallback});"); }
/** * Register script that will handle its behavior */ public function registerClientScript() { Yii::app()->bootstrap->registerAssetCss('bootstrap-relational.css'); $cs = Yii::app()->getClientScript(); if ($this->afterAjaxUpdate !== null) { if (!$this->afterAjaxUpdate instanceof CJavaScriptExpression && strpos($this->afterAjaxUpdate, 'js:') !== 0) { $this->afterAjaxUpdate = new CJavaScriptExpression($this->afterAjaxUpdate); } else { $this->afterAjaxUpdate = $this->afterAjaxUpdate; } } else { $this->afterAjaxUpdate = 'js:$.noop'; } $this->ajaxErrorMessage = CHtml::encode($this->ajaxErrorMessage); $afterAjaxUpdate = CJavaScript::encode($this->afterAjaxUpdate); $span = count($this->grid->columns); $loadingPic = CHtml::image(Yii::app()->bootstrap->getAssetsUrl() . '/img/loading.gif'); $cache = $this->cacheData ? 'true' : 'false'; $data = !empty($this->submitData) && is_array($this->submitData) ? $this->submitData : 'js:{}'; $data = CJavascript::encode($data); $js = <<<EOD \$(document).on('click','.{$this->cssClass}', function(){ \tvar span = {$span}; \tvar that = \$(this); \tvar status = that.data('status'); \tvar rowid = that.data('rowid'); \tvar tr = \$('#relatedinfo'+rowid); \tvar parent = that.parents('tr'); \tvar afterAjaxUpdate = {$afterAjaxUpdate}; \tif(status && status=='on'){return} \tthat.data('status','on'); \tif(tr.length && !tr.is(':visible') && {$cache}) \t{ \t\ttr.slideDown(); \t\tthat.data('status','off'); \t\treturn; \t}else if(tr.length && tr.is(':visible')) \t{ \t\ttr.slideUp(); \t\tthat.data('status','off'); \t\treturn; \t} \tif(tr.length) \t{ \t\ttr.find('td').html('{$loadingPic}'); \t\tif(!tr.is(':visible')){ \t\t\ttr.slideDown(); \t\t} \t} \telse \t{ \t\tvar td = \$('<td/>').html('{$loadingPic}').attr({'colspan':{$span}}); \t\ttr = \$('<tr/>').prop({'id':'relatedinfo'+rowid}).append(td); \t\t/* we need to maintain zebra styles :) */ \t\tvar fake = \$('<tr/>').append(\$('<td/>').attr({'colspan':{$span}})).css('display','none'); \t\tparent.after(tr); \t\ttr.after(fake); \t} \tvar data = \$.extend({$data}, {id:rowid}); \t\$.ajax({ \t\turl: '{$this->url}', \t\tdata: data, \t\tsuccess: function(data){ \t\t tr.find('td').html(data); \t\t that.data('status','off'); \t\t if(\$.isFunction(afterAjaxUpdate)) \t\t { \t\t afterAjaxUpdate(tr,rowid,data); \t\t } \t\t}, \t\terror: function() \t\t{ \t\t\ttr.find('td').html('{$this->ajaxErrorMessage}'); \t\t\tthat.data('status','off'); \t\t} \t}); }); EOD; $cs->registerScript(__CLASS__ . '#' . $this->id, $js); }
/** * Returns the javascript code responsable of handling the drag and drop sorting through an Ajax request * * @return string drag and drop sorting javascript code */ protected function getSortScript() { return ' var grid_id = ' . Cjavascript::encode($this->getId()) . '; var grid_selector = ' . Cjavascript::encode('#' . $this->getId()) . '; var tbody_selector = ' . Cjavascript::encode('#' . $this->getId() . ' tbody') . '; /*apply sortable*/ $(tbody_selector).sortable(' . CJavascript::encode($this->jqueryUiSortableOptions) . '); /*helper to keep each table cell width while dragging*/ $(tbody_selector).sortable("option", "helper", function(e, ui) { ui.children().each(function() { $(this).width($(this).width()); }); return ui; }); /*add dragged row index before moving as an attribute. Used to know if item is moved forward or backward*/ $(tbody_selector).bind("sortstart", function(event, ui) { ui.item.attr("data-prev-index", ui.item.index()); }); /*trigger ajax sorting when grid is updated*/ $(tbody_selector).bind("sortupdate", function(event, ui) { $(grid_selector).addClass(' . CJavascript::encode($this->loadingCssClass) . '); var data = {}; data["dragged_item_id"] = parseInt(ui.item.attr("data-id")); var prev_index = parseInt(ui.item.attr("data-prev-index")); var new_index = parseInt(ui.item.index()); /*which item place take dragged item*/ if (prev_index < new_index) { data["replacement_item_id"] = ui.item.prev().attr("data-id"); } else { data["replacement_item_id"] = ui.item.next().attr("data-id"); } data["model"] = ' . Cjavascript::encode($this->dataProvider->modelClass) . '; data["order_field"] = ' . Cjavascript::encode($this->orderField) . '; data["YII_CSRF_TOKEN"] = ' . Cjavascript::encode(Yii::app()->getRequest()->getCsrfToken()) . '; ui.item.removeAttr("data-prev-index"); ' . CHtml::ajax(array('type' => 'POST', 'url' => Yii::App()->controller->createAbsoluteUrl($this->orderUrl), 'data' => 'js:data', 'success' => 'js:function() { $(grid_selector).removeClass(' . CJavascript::encode($this->loadingCssClass) . '); /*update the whole grid again to update row class values*/ if ("' . (string) $this->updateAfterSorting . '") { $.fn.yiiGridView.update(grid_id); } if ("' . (string) $this->successMessage . '") { alert(' . CJavascript::encode($this->successMessage) . '); } } ', 'error' => 'js:function() { $(grid_selector).removeClass(' . CJavascript::encode($this->loadingCssClass) . '); alert(' . CJavascript::encode($this->errorMessage) . '); $.fn.yiiGridView.update(grid_id); } ')) . ' }); '; }
/** * Register script that will handle its behavior */ public function registerClientScript() { Yii::app()->bootstrap->registerAssetCss('bootstrap-relational.css'); /** @var $cs CClientScript */ $cs = Yii::app()->getClientScript(); if ($this->afterAjaxUpdate !== null) { if (!$this->afterAjaxUpdate instanceof CJavaScriptExpression && strpos($this->afterAjaxUpdate, 'js:') !== 0) { $this->afterAjaxUpdate = new CJavaScriptExpression($this->afterAjaxUpdate); } } else { $this->afterAjaxUpdate = 'js:$.noop'; } $this->ajaxErrorMessage = CHtml::encode($this->ajaxErrorMessage); $afterAjaxUpdate = CJavaScript::encode($this->afterAjaxUpdate); $span = count($this->grid->columns); $loadingPic = CHtml::image(Yii::app()->bootstrap->getAssetsUrl() . '/img/loading.gif'); $cache = $this->cacheData ? 'true' : 'false'; $data = !empty($this->submitData) && is_array($this->submitData) ? $this->submitData : 'js:{}'; $data = CJavascript::encode($data); $token = Yii::app()->request->enableCsrfValidation ? Yii::app()->request->csrfToken : ''; $cssClass = '.' . str_replace(' ', '.', $this->cssClass); $js = <<<EOD \$(document).on('click','{$cssClass}', function(){ \tvar span = {$span}; \tvar that = \$(this); \tvar afterAjaxUpdate = {$afterAjaxUpdate}; \ttoggleRelateCol(span, that,afterAjaxUpdate); \t }); EOD; $cs->registerScript(__CLASS__ . '#' . $this->id, $js); $fn = <<<EOD function toggleRelateCol(span, that, tr, afterAjaxUpdate){ \t \tvar status = that.data('status'); \tvar rowid = that.data('rowid'); \tvar tr = \$('#relatedinfo'+rowid); \tvar parent = that.parents('tr').eq(0); \tif (status && status=='on'){return} \t\tthat.data('status','on'); \t \tif (tr.length && !tr.is(':visible') && {$cache}) \t{ \t\ttr.slideDown(); \t\tthat.data('status','off'); \t\treturn; \t}else if (tr.length && tr.is(':visible')) \t{ \t\ttr.hide(); \t\tthat.data('status','off'); \t\treturn; \t} \tif (tr.length) \t{ \t\ttr.find('td').html('{$loadingPic}'); \t\tif (!tr.is(':visible')){ \t\t\ttr.slideDown(); \t\t} \t} \telse \t{ \t\tvar td = \$('<td/>').html('{$loadingPic}').attr({'colspan':span,'class':'relatedinfo-container'}); \t\ttr = \$('<tr/>').prop({'id':'relatedinfo'+rowid}).append(td); \t\t/* we need to maintain zebra styles :) */ \t\tvar fake = \$('<tr class="hide"/>').append(\$('<td/>').attr({'colspan':span,'class':'relatedinfo-container'})); \t\tparent.after(tr); \t\ttr.after(fake); \t} \tvar data = \$.extend({$data}, {id:rowid,YII_CSRF_TOKEN:'{$token}'}); \t\$.ajax({ \t\turl: '{$this->url}', \t\tdata: data, \t\ttype: 'post', \t\tsuccess: function(data){ \t\t\ttr.find('td').html(data); \t\t\tthat.data('status','off'); \t\t\tif (\$.isFunction(afterAjaxUpdate)) \t\t\t{ \t\t\t\tafterAjaxUpdate(tr,rowid,data); \t\t\t} \t\t}, \t\terror: function() \t\t{ \t\t\ttr.find('td').html('{$this->ajaxErrorMessage}'); \t\t\tthat.data('status','off'); \t\t} \t}); }\t\t EOD; $cs->registerScript(__CLASS__ . 'Functions', $fn, CClientScript::POS_END); }
/** * Register script that will handle its behavior */ public function registerClientScript() { $assets = Yii::app()->assetManager->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets'); /** @var $cs CClientScript */ $cs = Yii::app()->getClientScript(); $cs->registerCssFile($assets . '/expand-column.css'); if ($this->afterAjaxUpdate !== null) { if (!$this->afterAjaxUpdate instanceof CJavaScriptExpression && strpos($this->afterAjaxUpdate, 'js:') !== 0) { $this->afterAjaxUpdate = new CJavaScriptExpression($this->afterAjaxUpdate); } } else { $this->afterAjaxUpdate = 'js:$.noop'; } $this->ajaxErrorMessage = CHtml::encode($this->ajaxErrorMessage); $afterAjaxUpdate = CJavaScript::encode($this->afterAjaxUpdate); $span = count($this->grid->columns); $loadingPic = CHtml::image($assets . '/loading.gif'); $cache = $this->cacheData ? 'true' : 'false'; $data = !empty($this->submitData) && is_array($this->submitData) ? $this->submitData : 'js:{}'; $data = CJavascript::encode($data); list($parentId) = explode('_', $this->id); $js = <<<EOD \$('#{$parentId} .{$this->columnCssClass}').live('click',function(){ \tvar span = {$span}; \tvar that = \$(this); \tvar status = that.data('status'); \tvar rowid = that.data('rowid'); \tvar tr = \$('#relatedinfo'+rowid); \tvar parent = that.parents('tr').eq(0); \tvar afterAjaxUpdate = {$afterAjaxUpdate}; \tif (status && status=='on'){return} \tthat.data('status','on'); \tif (tr.length && !tr.is(':visible') && {$cache}) \t{ \t\ttr.slideDown(); \t\tthat.data('status','off'); \t\treturn; \t}else if (tr.length && tr.is(':visible')) \t{ \t\ttr.slideUp(); \t\tthat.data('status','off'); \t\treturn; \t} \tif (tr.length) \t{ \t\ttr.find('td').html('{$loadingPic}'); \t\tif (!tr.is(':visible')){ \t\t\ttr.slideDown(); \t\t} \t} \telse \t{ \t\tvar td = \$('<td/>').html('{$loadingPic}').attr({'class':'{$this->rowCssClass}','colspan':{$span}}); \t\ttr = \$('<tr/>').prop({'id':'relatedinfo'+rowid}).append(td); \t\t/* we need to maintain zebra styles :)*/ \t\tvar fake = \$('<tr/>').append(\$('<td/>').attr({'style':'display:none','colspan':{$span}})); \t\tparent.after(tr); \t\ttr.after(fake); \t} \tvar data = \$.extend({$data}, {id:rowid}); \t\$.ajax({ \t\turl: '{$this->url}', \t\tdata: data, \t\tsuccess: function(data){ \t\t\ttr.find('td').html(data); \t\t\tthat.data('status','off'); \t\t\tif (\$.isFunction(afterAjaxUpdate)) \t\t\t{ \t\t\t\tafterAjaxUpdate(tr,rowid,data); \t\t\t} \t\t}, \t\terror: function() \t\t{ \t\t\ttr.find('td').html('{$this->ajaxErrorMessage}'); \t\t\tthat.data('status','off'); \t\t} \t}); }); EOD; $cs->registerScript(__CLASS__ . '#' . $this->id, $js); }