Beispiel #1
0
    public function __construct($name = null, $attributes = null, $data = null)
    {
        if ($attributes === null) {
            $attributes = array();
        }
        parent::__construct($name, $attributes, $data);
        $id = $this->getId();
        $opened = explode(';', @$_COOKIE['am-adv-fieldset']);
        if (in_array($id, $opened)) {
            $this->setAttribute('data-hidden', false);
        } else {
            if (!isset($attributes['data-hidden'])) {
                $this->setAttribute('data-hidden', true);
            }
        }
        $this->addScript()->setScript(<<<CUT
\$(function(){
    \$("#{$id} legend").click(function(){
        var cookieName = 'am-adv-fieldset';
        var fs = \$(this).closest("fieldset");
        var hidden = !fs.data('hidden');
        hidden ? setClosed('{$id}') : setOpened('{$id}');
        fs.data('hidden', hidden);
        fs.find("div.fieldset").toggle(!hidden);
        fs.find(".plus-minus").html(hidden ? '+' : '&minus;' );
        fs.find(".dots").html(hidden ? '&hellip;' : '' );

        function setOpened(id) {
            var openedIds = getOpenedIds();
            if (!isOpened(id)) {
                openedIds.push(id);
            }
            setCookie(cookieName, openedIds.join(';'));
        }

        function setClosed(id) {
            var openedIds = getOpenedIds();
            for (var i=0; i<openedIds.length; i++) {
                if (openedIds[i] == id) {
                    openedIds.splice(i, 1);
                    break;
                }
            }
            setCookie(cookieName, openedIds.join(';'));
        }

        function getOpenedIds() {
            var cookie = getCookie(cookieName);
            return cookie ? cookie.split(';') : [];
        }

        function isOpened(id) {
            var openedIds = getOpenedIds();
            for (var i=0; i<openedIds.length; i++) {
                if (openedIds[i] == id) {
                    return true;
                }
            }
            return false;
        }

        function setCookie(name, value) {
                var today = new Date();
                var expiresDate = new Date();
                expiresDate.setTime(today.getTime() + 365 * 24 * 60 * 60 * 1000); // 1 year
                document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expiresDate.toGMTString() + ";";
            }

        function getCookie(name) {
            var prefix = name + "=";
            var start = document.cookie.indexOf(prefix);
            if (start == -1) return null;
            var end = document.cookie.indexOf(";", start + prefix.length);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start + prefix.length, end));
        }
    });
    \$("#{$id}").data('hidden', !\$("#{$id}").data('hidden')).find('legend').click();
});
CUT
);
    }