refactor gallery code into directive