Skip to content

Commit

Permalink
Code
Browse files Browse the repository at this point in the history
  • Loading branch information
iamawebgeek committed Jan 16, 2017
1 parent 0f4dc0f commit f230529
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ nbproject
.project
.settings

# lock file
composer.lock

# mac deployment helpers
switch
index
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "iamwebdesigner/yii2-jquitelight",
"description": "Yii2 Widget for jquitelight highlight plugin",
"license": "Apache 2.0",
"authors": [
{
"name": "iamwebdesigner",
"email": "iamawebgeek@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"bower-asset/jquitelight": "~2.1"
},
"autoload": {
"psr-4": {
"iamwebdesigner\\jquitelight\\": "src"
}
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}
80 changes: 80 additions & 0 deletions src/Jquitelight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace iamwebdesigner\jquitelight;

use iamwebdesigner\jquitelight\assets\JquitelightAsset;
use yii\base\Exception;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;

/**
* Class Jquitelight
* @package iamwebdesigner\jquitelight
* @author iamwebdesigner
*/
class Jquitelight extends Widget
{
/**
* HTML attributes for rendering container
* @var array
*/
public $htmlOptions = [];
/**
* The options to be passed for javascript plugin.
* @var array
*/
public $clientOptions = [];
/**
* Whether to wrap content in a div or not
* if false then jQuery.unwrap method used to get rid of created div
* @var bool
*/
public $wrapContent = true;
/**
* List of keywords to be highlighted
* can be assoc array as this value if passed for first argument of mark method of js plugin
* @var array
*/
public $keywords = [];

/**
* Initialize plugin and open wrapping container
*/
public function init()
{
parent::init();
if (!$this->htmlOptions['id']) {
$this->htmlOptions['id'] = $this->getId();
}
if (empty($this->keywords)) {
throw new Exception('No keywords specified for highlighting');
}
Html::beginTag('div', $this->htmlOptions);
}

/**
* Register assets and close wrapping container
*/
public function run()
{
$this->registerAssets();
Html::endTag('div');
}

/**
* Register scripts
*/
protected function registerAssets()
{
$view = $this->getView();
$id = $this->htmlOptions['id'];
JquitelightAsset::register($view);
$keywords = Json::encode($this->keywords);
$options = Json::encode($this->clientOptions);
$view->registerJs("jQuery('#$id').mark($keywords, $options);", $view::POS_END);
if (!$this->wrapContent) {
$view->registerJs("jQuery('#$id').find(':eq(0)').unwrap();", $view::POS_END);
}
}
}
21 changes: 21 additions & 0 deletions src/assets/JquitelightAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace iamwebdesigner\jquitelight\assets;

use yii\web\AssetBundle;

/**
* Class JquitelightAsset
* @package iamwebdesigner\jquitelight\assets
* @author iamwebdesigner
*/
class JquitelightAsset extends AssetBundle
{
public $sourcePath = '@bower/jquitelight';
public $js = [
'jquitelight.min.js'
];
public $depends = [
'yii\web\JqueryAsset'
];
}

0 comments on commit f230529

Please sign in to comment.