Project

General

Profile

1
$(function() {
2
    // add note
3
    altair_sticky_notes.add_note();
4
    // note actions
5
    altair_sticky_notes.actions();
6
    // load sticky notes
7
    altair_sticky_notes.load_notes();
8

    
9
    // slider fix
10
    $('#top_bar').find('.uk-slidenav-position').on('focusitem.uk.slider', function(event, index, item) {
11
        $(event.target).addClass('uk-slidenav-hover');
12
    });
13
});
14

    
15
var $note_template = $('#note_template').html(),
16
    $note_form_template = $('#note_form_template').html(),
17
    $note_form_text = $('.note_form_text'),
18
    $form = $('#note_form'),
19
    $form_card = $form.closest('.md-card'),
20
    $grid = $('#notes_grid'),
21
    pallete = ['md-bg-white','md-bg-red-500','md-bg-pink-500','md-bg-purple-500','md-bg-indigo-500','md-bg-blue-500','md-bg-cyan-500','md-bg-teal-500','md-bg-green-500','md-bg-lime-500','md-bg-yellow-500','md-bg-amber-500','md-bg-brown-500','md-bg-blue-grey-500'],
22
    $card = $form.closest('.md-card'),
23
    // callback after note color select
24
    changeBg = function(remove) {
25
        var color = $('#notes_colors_wrapper').find('input').val();
26
        if(typeof color != 'undefined') {
27
            $.each(pallete,function (key,value) {
28
                var color = replaceColor(value);
29
                if($card.hasClass(color)) {
30
                    $card.removeClass(color);
31
                }
32
            });
33
            if(!remove) {
34
                $card.addClass(replaceColor(color));
35
            }
36
        }
37
    },
38
    // notes color picker
39
    notesColorPicker = altair_helpers.color_picker($('<div id="notes_colors_wrapper"></div>'),pallete,changeBg).prop('outerHTML');
40

    
41
function hide_note_form() {
42
    $form.fadeOut('fast',function() {
43
        changeBg(true);
44
        $form.html('');
45
        $form_card.velocity('reverse', {
46
            complete: function() {
47
                $note_form_text.show();
48
                $form_card.removeClass('card-expanded');
49
            }
50
        });
51
        $window.resize();
52
    });
53
}
54

    
55
function replaceColor(color) {
56
    if(color) {
57
        var replaceColor = color.split('-'),
58
            lastEl = replaceColor[replaceColor.length -1];
59
        if(!isNaN(lastEl)) {
60
            replaceColor.pop();
61
            var replacedColor = replaceColor.join('-') + '-100'
62
        } else {
63
            var replacedColor = replaceColor.join('-')
64
        }
65
        return replacedColor;
66
    } else {
67
        return false;
68
    }
69
}
70

    
71
altair_sticky_notes = {
72
    add_note: function() {
73

    
74
        $form_card.css({
75
            minHeight: $form_card.actual('height')
76
        });
77

    
78
        // show note form
79
        $form_card.on('click', function(e) {
80
            if(!$form_card.hasClass('card-expanded')) {
81
                var template = Handlebars.compile($note_form_template),
82
                    context = {
83
                        'labels': [
84
                            {
85
                                'text': 'label 1',
86
                                'text_safe': 'label_1',
87
                                'type': 'default'
88
                            },
89
                            {
90
                                'text': 'label 2',
91
                                'text_safe': 'label_2',
92
                                'type': 'warning'
93
                            },
94
                            {
95
                                'text': 'label 3',
96
                                'text_safe': 'label_3',
97
                                'type': 'danger'
98
                            },
99
                            {
100
                                'text': 'label 4',
101
                                'val': 'label_4',
102
                                'type': 'success'
103
                            },
104
                            {
105
                                'text': 'label 5',
106
                                'text_safe': 'label_5',
107
                                'type': 'primary'
108
                            }
109
                        ]
110
                    };
111
                    html = template(context);
112

    
113
                $form.hide().html(html);
114
                $note_form_text.hide();
115

    
116
                var minHeight = $form.actual('height');
117

    
118
                $form_card
119
                    .velocity({
120
                        minHeight: minHeight
121
                    }, {
122
                        duration: 200,
123
                        easing: easing_swiftOut,
124
                        complete: function (elements) {
125
                            $form.fadeIn('fast');
126
                            $('#note_f_title').focus();
127
                            $window.resize();
128
                        }
129
                    });
130

    
131
                $form_card.addClass('card-expanded');
132

    
133
                $('#notes_cp').append(notesColorPicker);
134
                // initialize md inputs
135
                altair_md.inputs($form);
136
                // initialize textarea autosize
137
                altair_forms.textarea_autosize();
138
                // initialize iCheck checkboxes
139
                altair_md.checkbox_radio($form.find('[data-md-icheck]'));
140
            }
141

    
142
        });
143

    
144
        // add note
145
        $form.on('click', '#note_add', function(e) {
146
            e.preventDefault();
147
            var $title_input = $('#note_f_title'),
148
                title = $title_input.val(),
149
                $content_input = $('#note_f_content'),
150
                content = $content_input.val().replace(/\n/g, '<br />');
151

    
152
            $title_input.removeClass('md-input-danger');
153
            $content_input.removeClass('md-input-danger');
154

    
155
            if(title == '') {
156
                $title_input.addClass('md-input-danger');
157
                altair_md.update_input($title_input);
158
                return;
159
            }
160
            if(content == '') {
161
                $content_input.addClass('md-input-danger');
162
                altair_md.update_input($content_input);
163
                return;
164
            }
165

    
166
            if(title && content) {
167
                var color = $('#notes_cp').find('input').val(),
168
                    context = [{
169
                        'time': new Date().getTime(),
170
                        'color': replaceColor(color),
171
                        'title': title,
172
                        'content': content
173
                    }],
174
                    checkboxes = $('input[name="checkboxes"]'),
175
                    labels = $('input[name="labels"]');
176

    
177
                if(checkboxes) {
178
                    var checklist = [];
179
                    $.each(checkboxes,function (key,value) {
180
                        var $this = $(value);
181
                        checklist.push({
182
                            'id': $this.attr('id'),
183
                            'checked': $this.is(':checked'),
184
                            'title': $this.attr('data-title')
185
                        })
186
                    });
187
                    context[0].checklist = checklist;
188
                }
189

    
190
                if(labels) {
191
                    var labels_list = [];
192
                    $.each(labels,function (key,value) {
193
                        var $this = $(value);
194
                        if($this.is(':checked')) {
195
                            labels_list.push({
196
                                'type': $this.attr('data-style'),
197
                                'text': $this.attr('data-label'),
198
                                'text_safe': $this.attr('data-label').split(' ').join('-')
199
                            })
200
                        }
201
                    });
202
                    if(labels_list) {
203
                        context[0].labels = labels_list;
204
                    }
205
                }
206

    
207
                var template = Handlebars.compile($note_template),
208
                    html = template(context);
209

    
210
                $grid.prepend(html);
211
                $window.resize();
212

    
213
                // initialize iCheck
214
                altair_md.checkbox_radio($grid.find('[data-md-icheck]'));
215

    
216
                hide_note_form();
217
            }
218

    
219
        });
220

    
221
        // add checklist item
222
        var addChecklist_item = function() {
223
            var $note_form_checkbox_template = $('#note_form_checkbox_template').html(),
224
                template = Handlebars.compile($note_form_checkbox_template),
225
                $title_input = $('#checklist_item'),
226
                title = $title_input.val(),
227
                context = {
228
                    id: randID_generator(),
229
                    'title':  title
230
                },
231
                checklist_item = template(context);
232

    
233
            if(title != '') {
234
                var $list = $('#notes_checklist').children('ul');
235
                $list.append(checklist_item);
236
                $title_input.val('');
237
                altair_md.checkbox_radio($list.find('[data-md-icheck]'));
238
            }
239
        };
240

    
241
        // remove checklist item
242
        $form.on('click','.remove_checklist_item',function() {
243
            var $this = $(this),
244
                $thisItem = $this.closest('li');
245
            $thisItem.remove();
246
        });
247

    
248
        $form.on('click', '#checkbox_add', function(e) {
249
            e.preventDefault();
250
            addChecklist_item();
251
        });
252
        $form.on('keypress', '#checklist_item', function(e) {
253
            if(e.which == 13) {
254
                addChecklist_item();
255
            }
256
        });
257

    
258
        // close note form
259
        $(document).on('click keydown', function(e) {
260
            var code = e.which || e.keyCode;
261
            if ( $form_card.hasClass('card-expanded') && (!$(e.target).closest($form_card).length  || code == 27) ) {
262
                if(code != 27 && code != 1) {
263
                    return;
264
                }
265
                hide_note_form();
266
            }
267
        });
268

    
269
    },
270
    // note actions
271
    actions: function() {
272
        // remove note
273
        $grid.on('click','.note_action_remove', function() {
274
            var $this_note = $(this).closest('.md-card').parent();
275
            $this_note.addClass('uk-animation-scale-up uk-animation-reverse');
276
            setTimeout(function() {
277
                $this_note.remove();
278
            },300);
279
        });
280
    },
281
    // load notes
282
    load_notes: function() {
283
        var context = [
284
            {
285
                'time': 1475604600000,
286
                'color': 'md-bg-red-100',
287
                'title': 'Lorem impsum dolor sit',
288
                'content': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam molestias quidem repellendus saepe vero! Assumenda fugiat perferendis reiciendis repellat voluptas?',
289
                'labels': [
290
                    {
291
                        'type': 'primary',
292
                        'text': 'label 5',
293
                        'text_safe': 'label-5'
294
                    }
295
                ]
296
            },
297
            {
298
                'time': 1475691265322,
299
                'title': 'Lorem ipsum',
300
                'content': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam molestias quidem repellendus saepe vero!',
301
                'labels': [
302
                    {
303
                        'type': 'default',
304
                        'text': 'label 1',
305
                        'text_safe': 'label-1'
306
                    }
307
                ],
308
                'checklist': [
309
                    {
310
                        'checked': false,
311
                        'title': 'first item'
312
                    },
313
                    {
314
                        'checked': true,
315
                        'title': 'second item'
316
                    },
317
                    {
318
                        'checked': false,
319
                        'title': 'third item'
320
                    },
321
                    {
322
                        'checked': false,
323
                        'title': 'fourth item'
324
                    }
325
                ]
326
            },
327
            {
328
                'time': 1475691265322,
329
                'color': 'md-bg-green-100',
330
                'title': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
331
                'content': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda fugiat perferendis reiciendis repellat voluptas?',
332
                'labels': [
333
                    {
334
                        'type': 'warning',
335
                        'text': 'label 2',
336
                        'text_safe': 'label-2'
337
                    },
338
                    {
339
                        'type': 'danger',
340
                        'text': 'label 3',
341
                        'text_safe': 'label-3'
342
                    }
343
                ]
344
            },
345
            {
346
                'time': 1475604600000,
347
                'color': 'md-bg-yellow-100',
348
                'title': 'Lorem impsum dolor sit',
349
                'content': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam molestias quidem repellendus saepe vero! Assumenda fugiat perferendis reiciendis repellat voluptas?',
350
                'labels': [
351
                    {
352
                        'type': 'success',
353
                        'text': 'label 4',
354
                        'text_safe': 'label-4'
355
                    },
356
                    {
357
                        'type': 'primary',
358
                        'text': 'label 5',
359
                        'text_safe': 'label-5'
360
                    }
361
                ]
362
            }
363
        ];
364

    
365
        var template = Handlebars.compile($note_template),
366
            html = template(context);
367

    
368
        $grid.prepend(html);
369

    
370
        // initialize iCheck
371
        altair_md.checkbox_radio($grid.find('[data-md-icheck]'));
372

    
373
    }
374
};
(67-67/114)