2
\$\begingroup\$

This plugin is initialized like so:

$("#calendar").ksdCalendar({
    feedUrl: "http://www.kent.k12.wa.us/site/RSS.aspx?DomainID=275&ModuleInstanceID=4937&PageID=4334",
    elemHeight: 750
});

I would like a review on the code, structure, or anything else that comes to mind. What this plugin does is take an RSS feed from JGFeed, parse the entries, then pass them into another calendar plugin which in turn builds and displays the calendar.

(function ( $, window, document, undefined ) {
    "use strict";
    var Calendar = {

        init: function(options, elem) {

            this.options = $.extend( {}, this.options, options );
            this.elem = $(elem);

            this.setupAjax();
            this.getFeed();

            return this;
        },

        options: {
            feedUrl: "",
            elemHeight: 750
        },

        entries: [],

        getFeed: function() {
            var self = this;
            $.jGFeed(this.options.feedUrl, function (feeds) {

                if (!feeds) {
                    return false;
                }

                $.extend(self.entries, feeds.entries);

                self.parseEntries();
            }, 100);
        },

        parseEntries: function() {
            //Rename to fit plugin requirements
            for (var i = 0; i < this.entries.length; i++) {
                var entry = this.entries[i];
                    entry["allDay"] = false;

                //Rename
                entry["url"] = entry["link"];
                delete entry["link"];

                var position = entry.title.indexOf(' - ');

                if (position === -1) {
                    //All day event
                    entry.allDay = true;
                    var space = entry.title.indexOf(" "),
                        title = entry.title.substring(space + 1),
                        firstHalf = entry.title.slice(0, space); //Start date, no time because it's all day event
                } else {
                    var firstHalf = entry.title.slice(0, position), //Start date/time
                        secondHalf = entry.title.substring(position + 3);

                    if (secondHalf.indexOf("AM") !== -1) {
                        var title = secondHalf.substring(secondHalf.indexOf("AM") + 3); //Title if has AM
                    } else {
                        var title = secondHalf.substring(secondHalf.indexOf("PM") + 3); //Title if has PM
                    }

                    secondHalf = secondHalf.slice(0, -(title.length + 1)); //End date/time
                }

                entry["start"] = Date.parse(firstHalf);
                entry["end"] = Date.parse(secondHalf);
                entry.title = title;
            };

            this.setUpCalendar();
        },

        setUpCalendar: function() {
            this.elem.fullCalendar({
                editable:   false,
                weekends:   true,
                header: {
                    left:       'month basicDay',
                    center:     'title',
                    right:      'today prev, next'
                },
                height:     this.options.elemHeight,
                events:     this.entries
            });
        }
    };

    if ( typeof Object.create !== 'function' ) {
        Object.create = function (o) {
            function F() {}
            F.prototype = o;
            return new F();
        };
    }


    $.fn.ksdCalendar = function( options ) {
        if (this.length) {
            return this.each(function() {
                var myCalendar = Object.create(Calendar);
                myCalendar.init(options, this);
                $.data(this, 'ksdCalendar', myCalendar);
            });
        }
    };

})( jQuery, window, document );
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This code looks good, I only have 2 minor items from a once over;

  • I would have a constant for -1 meaning an all day event

    var ALL_DAY_EVENT = -1; // <-- Declared somewhere on top
    if (position === ALL_DAY_EVENT) {
        ...
    
  • This:

    if (secondHalf.indexOf("AM") !== -1) {
        var title = secondHalf.substring(secondHalf.indexOf("AM") + 3); //Title if has AM
    } else {
        var title = secondHalf.substring(secondHalf.indexOf("PM") + 3); //Title if has PM
    }
    

    Declares title with var twice, you should declare title only once, ideally on top before your loop. Also, there is some serious code repetition there. I would suggest something like:

    //Declare meridiem and title somewhere higher prior to the loop
    meridiem = ~secondHalf.indexOf("AM") ? "AM" : "PM"; 
    title = secondHalf.substring(secondHalf.indexOf(meridiem) + 3);
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.