/**
  * {@inheritdoc}
  */
 protected function build(Definition $definition, \Model $model, DefinitionMapper $mapper, Filter $filter = null, Definition $parent = null)
 {
     parent::build($definition, $model, $mapper, $filter, $parent);
     /** @var MarkerClusterGroup $definition */
     if ($model->iconCreateFunction) {
         $definition->setIconCreateFunction(new Expression($model->iconCreateFunction));
     }
     if ($model->polygonOptions) {
         $definition->setPolygonOptions((array) json_decode($model->polygonOptions, true));
     }
     if (!$model->disableDefaultStyle) {
         $this->assets->addStylesheet('assets/leaflet/libs/leaflet-markercluster/MarkerCluster.Default.css');
     }
     $collection = LayerModel::findBy(array('pid=?', 'active=1'), array($model->id), array('order' => 'sorting'));
     if ($collection) {
         foreach ($collection as $layerModel) {
             $layer = $mapper->handle($layerModel);
             if ($layer instanceof Layer) {
                 $definition->addLayer($layer);
                 if ($layer instanceof OmnivoreLayer) {
                     $callback = new AnonymousFunction();
                     $callback->addLine('layers.' . $definition->getId() . '.addLayers(this.getLayers())');
                     $layer->on('ready', $callback);
                 }
             }
         }
     }
 }
 /**
  * Load all libraries for an icon.
  *
  * @param Icon $icon Icon definition
  *
  * @return void.
  */
 protected function loadIconsLibraries($icon)
 {
     foreach ($icon::getRequiredLibraries() as $library) {
         if (!isset($this->libraries[$library])) {
             continue;
         }
         $assets = $this->libraries[$library];
         if (!empty($assets['css'])) {
             list($source, $type) = (array) $assets['css'];
             $this->assets->addStylesheet($source, $type ?: Assets::TYPE_FILE);
         }
         if (!empty($assets['javascript'])) {
             list($source, $type) = (array) $assets['javascript'];
             $this->assets->addJavascript($source, $type ?: Assets::TYPE_FILE);
         }
     }
 }
 /**
  * Get map javascript.
  *
  * @param MapModel|int $mapId     The map database id. MapModel accepted as well.
  * @param Filter       $filter    Optional request filter.
  * @param string       $elementId Optional element id. If none given the mapId or alias is used.
  * @param string       $template  The template being used for generating.
  * @param string       $style     Optional style attributes.
  *
  * @return string
  * @throws \Exception If generating went wrong.
  */
 public function generate($mapId, Filter $filter = null, $elementId = null, $template = 'leaflet_map_js', $style = '')
 {
     if ($mapId instanceof MapModel) {
         $model = $mapId;
         $mapId = $mapId->id;
     } else {
         $model = $this->getModel($mapId);
     }
     if ($model->cache) {
         $cacheKey = $this->getCacheKey($mapId, $filter, $elementId, $template, $style);
         if ($this->cache->contains($cacheKey)) {
             $cached = $this->cache->fetch($cacheKey);
             $this->assets->fromArray($cached['assets']);
             return $cached['javascript'];
         }
     }
     $buffer = $this->doGenerate($mapId, $filter, $elementId, $template, $model, $style);
     if ($model->cache) {
         $this->cache->save($cacheKey, ['assets' => $this->assets->toArray(), 'javascript' => $buffer], (int) $model->cacheLifeTime);
     }
     return $buffer;
 }