หน้าเว็บ

แสดงบทความที่มีป้ายกำกับ javascript แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ javascript แสดงบทความทั้งหมด

วันเสาร์ที่ 18 เมษายน พ.ศ. 2558

angularjs enumeration : javascript

Enum Factory
var mapApp = ...

myApp.factory('Enumerate', function () {

    return function (list, fnc) {

        var Enumerate = function (ctx) {
            if (angular.isObject(ctx)) {
                angular.forEach(ctx, function (val, key) {
                    this[key] = val;
                }, this);
            } else if (angular.isString(ctx)) {
                this.name = ctx;
            } else {
                throw new Error('incorrect arguments.');
            }
        };

        Enumerate.values = [];

        Enumerate.valueOf = function (ctx) {
            var obj = null;
            angular.forEach(Enumerate.values, function (item) {
                if (item.name === ctx) {
                    obj = item;
                    return false;
                }
            });

            if (!obj) {
                obj = new Enumerate(ctx);
                Enumerate.values.push(obj);
            }

            return obj;
        }

        Enumerate.prototype.toString = function () {
            return this.name;
        };

        fnc && angular.forEach(fnc, function (val, key) {
            Enumerate.prototype[key] = val;
        });

        angular.forEach(list, function (item) {
            Enumerate[
                    angular.isObject(item)
                    ? item.name
                    : item
            ] = Enumerate.valueOf(item);
        });

        return Enumerate;
    };
});
Using
myApp..factory('StandardColumn', [
    'Enumerate',
    function (Enumerate) {

        var StandardColumn = function () {

        };

        /* define enum */
        StandardColumn.Format = Enumerate([
            {name: 'STRING', description: 'ตัวหนังสือ'},
            {name: 'NUMBER', description: 'ตัวเลข'},
            {name: 'STUDENT', description: 'นักเรียน'}
        ]);

        StandardColumn.Format.fromDescription = function (desc) {
            var obj = null;
            angular.forEach(StandardColumn.Format.values, function (item) {
                if (item.description === desc) {
                    obj = item;
                    return false;
                }
            })

            return obj;
        };

        StandardColumn.Operation = Enumerate([
            'SUM',
            'AVERAGE'
        ]);

        StandardColumn.prototype = {
            id: null,
            name: null,
            sequence: 1,
            format: StandardColumn.Format.STRING,
            operation: null,
            operateWith: null
        };

        return StandardColumn;
    }
]);

วันศุกร์ที่ 21 พฤศจิกายน พ.ศ. 2557

BottomScrollFixer : javascript


        บางที element ของเรา อาจจะยาวมากๆ  จนล้นหน้าจอ (window) แล้ว element นั้น  ก็เกิด bottom overflow (overflow-x เป็น scroll หรือ auto) ด้วย

ปัญหา

        ปัญหาที่เจอก็คือ  เราต้องเลื่อน scroll ของ window ลงมาข้างล่างสุดของ element นั้นก่อน  เราถึงจะสามารถเลื่อน scroll X ของ element นั้นได้ (เพราะ element มันล้นจอ)

วิธีแก้

        ผมเลยทำการเขียน javascript ขึ้นมาเพื่อแก้ปัญหานี้  โดยให้มันทำการคำนวนและ fixed bottom scroll ให้โดยอัตโนมัติ  โดยที่เราไม่ต้องเลื่อน window scroll ลงไปข้างล่าง  มันจะ fixed ให้ทันทีที่เราเลื่อน scroll ไปถึง element นั้นตามขนาดที่เราได้ fixed ไว้  (withFixedHeight) โดยไม่จำเป็นต้องเลือนลงไปยังข้างล่างสุดของ element bottom scroll จะเลื่อนตามเราไปเรื่อยๆ  จนกว่าจะพ้น element นั้นๆ

วันอังคารที่ 20 พฤษภาคม พ.ศ. 2557

Proxy handle ajax response primefaces : javascript

บางที เราอาจจะอยาก modify response หรือทำอะไรบางอย่าง ก่อนที่ primefaces ajax จะทำงานเสร็จ
PrimeFaces.ajax.AjaxUtils.handleResponse = (function(originMethod) {

    //return proxy method instead origin method
    return function() { 

        //origin method evironment/instance context

        return originMethod.apply(this, arguments); //
    };
})(PrimeFaces.ajax.AjaxUtils.handleResponse);
ตัวอย่างเช่น การ handle validate response
/**
 * call when ajax response validate failed
 */
function validationFailedHandle() {
    //do something...
}

var handleProxy = function(originMethod) {
 
    var slice = Array.prototype.slice;
 
    return function() {
        var args = slice.call(arguments);
        var response = args[0].find('extension').text();
        if (response) {
            var obj = JSON.parse(response);
            obj.validationFailed && validationFailedHandle.apply(this, args);
        }
 
        return originMethod.apply(this, args);
    };
};
 
PrimeFaces.ajax.AjaxUtils.handleResponse = handleProxy(PrimeFaces.ajax.AjaxUtils.handleResponse);